Calculate the size of grouped 2d objects

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.

Calculate the size of grouped 2d objects

Postby Chris » Thu Feb 18, 2016 2:42 pm

I have a few objects on a 2d field:
Image

I need to calculate the size of the items when grouped together programtically:
Image

I have the size, position and rotation in radians of each object.

Does anyone know the termanology or a method of doing this?

I need to do it in PHP.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Chris
 
Posts: 1573
Joined: Wed Sep 30, 2009 2:22 pm
Has thanked: 4 times
Been thanked: 5 times
Blog: View Blog (2)

Re: Calculate the size of grouped 2d objects

Postby hallsofvallhalla » Thu Feb 18, 2016 2:50 pm

hmm intersting, in PHP that is going to be tough as you are technically not creating them in PHP

I would think get the corners like so

highest object of the three
object.position.top + object.height

far left most object
object.position.left + object.width

then do the same for all 4 dimensions.

Sorry if that doesn't help. Not sure how you creating the objects with PHP
User avatar
hallsofvallhalla
Site Admin
 
Posts: 11942
Images: 13
Joined: Wed Apr 22, 2009 6:29 pm
Location: mobile, al
Has thanked: 11 times
Been thanked: 158 times
Blog: View Blog (3)

Re: Calculate the size of grouped 2d objects

Postby Chris » Thu Feb 18, 2016 2:52 pm

The biggest problem I'm facing is calculating the bottom and top position when an object has been rotated.

There's bound to be a term for doing this on which i can base my calculation.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Chris
 
Posts: 1573
Joined: Wed Sep 30, 2009 2:22 pm
Has thanked: 4 times
Been thanked: 5 times
Blog: View Blog (2)

Re: Calculate the size of grouped 2d objects

Postby hallsofvallhalla » Thu Feb 18, 2016 2:54 pm

ahh i misunderstood some. Does PHP have a version of Javascripts Element.getBoundingClientRect()?
User avatar
hallsofvallhalla
Site Admin
 
Posts: 11942
Images: 13
Joined: Wed Apr 22, 2009 6:29 pm
Location: mobile, al
Has thanked: 11 times
Been thanked: 158 times
Blog: View Blog (3)

Re: Calculate the size of grouped 2d objects

Postby Chris » Thu Feb 18, 2016 3:03 pm

I think I'm trying to calculate a bounding box.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Chris
 
Posts: 1573
Joined: Wed Sep 30, 2009 2:22 pm
Has thanked: 4 times
Been thanked: 5 times
Blog: View Blog (2)

Re: Calculate the size of grouped 2d objects

Postby Chris » Thu Feb 18, 2016 3:31 pm

I found this article for calcluating the size of a rotated rectangle, now i just need to do this for each object and then add them all together.

http://www.codepuppet.com/2014/01/11/ca ... le-in-php/
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Chris
 
Posts: 1573
Joined: Wed Sep 30, 2009 2:22 pm
Has thanked: 4 times
Been thanked: 5 times
Blog: View Blog (2)

Re: Calculate the size of grouped 2d objects

Postby Chris » Thu Feb 18, 2016 5:09 pm

For anyone who is wondering, this is what I came up with:

Code: Select all

    $layers 
= array();

    
$layers[0] = new Layer();
    
$layers[0]->10;
    
$layers[0]->10;
    
$layers[0]->width 100;
    
$layers[0]->height 200;
    
$layers[0]->rotation 0.3;

    
$layers[1] = new Layer();
    
$layers[1]->50;
    
$layers[1]->60;
    
$layers[1]->width 200;
    
$layers[1]->height 300;
    
$layers[1]->rotation 0.1;

    
$layers[2] = new Layer();
    
$layers[2]->100;
    
$layers[2]->300;
    
$layers[2]->width 200;
    
$layers[2]->height 150;
    
$layers[2]->rotation 0.3;

    
print_r($layers);

    
$boundingBoxes = array();
    foreach(
$layers as $layerKey => $layer) {
      
$intWidthRotated $layer->height abssin$layer->rotation) ) + $layer->width abscos$layer->rotation ) );
      
$intHeightRotated $layer->height abscos$layer->rotation ) ) + $layer->width abssin$layer->rotation ) );
      
$boundingBoxes[$layerKey] = array( $intWidthRotated$intHeightRotated );
    }
    
print_r($boundingBoxes);

    
$boundingBox = array();
    
$minX null;
    
$maxX null;
    
$minY null;
    
$maxY null;
    foreach(
$layers as $layerKey => $layer) {
            
$minX $minX == null $layer->$minX >= $layer->$layer->$minX;
            
$maxX $maxX == null $layer->$boundingBoxes[$layerKey][0] : $maxX <= $layer->$boundingBoxes[$layerKey][0] ? $layer->$boundingBoxes[$layerKey][0] : $maxX;

            
$minY $minY == null $layer->$minY >= $layer->$layer->$minY;
            
$maxY $maxY == null $layer->$boundingBoxes[$layerKey][1] : $maxY <= $layer->$boundingBoxes[$layerKey][1] ? $layer->$boundingBoxes[$layerKey][1] : $maxY;
    }

    
$boundingBox = array(
            
'x' => $minX,
            
'y' => $minY,
            
'width' => $maxX,
            
'height' => $maxY
    
);

    
print_r($boundingBox);
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Chris
 
Posts: 1573
Joined: Wed Sep 30, 2009 2:22 pm
Has thanked: 4 times
Been thanked: 5 times
Blog: View Blog (2)

Re: Calculate the size of grouped 2d objects

Postby hallsofvallhalla » Thu Feb 18, 2016 5:35 pm

crazy stuff
User avatar
hallsofvallhalla
Site Admin
 
Posts: 11942
Images: 13
Joined: Wed Apr 22, 2009 6:29 pm
Location: mobile, al
Has thanked: 11 times
Been thanked: 158 times
Blog: View Blog (3)


Return to Advanced Help and Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron

x