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.
Post Reply
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Calculate the size of grouped 2d objects

Post by Chris »

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
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Calculate the size of grouped 2d objects

Post by hallsofvallhalla »

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
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Calculate the size of grouped 2d objects

Post by Chris »

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
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Calculate the size of grouped 2d objects

Post by hallsofvallhalla »

ahh i misunderstood some. Does PHP have a version of Javascripts Element.getBoundingClientRect()?
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Calculate the size of grouped 2d objects

Post by Chris »

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: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Calculate the size of grouped 2d objects

Post by Chris »

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: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Calculate the size of grouped 2d objects

Post by Chris »

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

Code: Select all

    $layers = array();

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

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

    $layers[2] = new Layer();
    $layers[2]->x = 100;
    $layers[2]->y = 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 * abs( sin( $layer->rotation) ) + $layer->width * abs( cos( $layer->rotation ) );
      $intHeightRotated = $layer->height * abs( cos( $layer->rotation ) ) + $layer->width * abs( sin( $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->x : $minX >= $layer->x ? $layer->x : $minX;
            $maxX = $maxX == null ? $layer->x + $boundingBoxes[$layerKey][0] : $maxX <= $layer->x + $boundingBoxes[$layerKey][0] ? $layer->x + $boundingBoxes[$layerKey][0] : $maxX;

            $minY = $minY == null ? $layer->y : $minY >= $layer->y ? $layer->y : $minY;
            $maxY = $maxY == null ? $layer->y + $boundingBoxes[$layerKey][1] : $maxY <= $layer->y + $boundingBoxes[$layerKey][1] ? $layer->y + $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
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Calculate the size of grouped 2d objects

Post by hallsofvallhalla »

crazy stuff
Post Reply

Return to “Advanced Help and Support”