Page 1 of 1

Calculate the size of grouped 2d objects

Posted: Thu Feb 18, 2016 7:42 pm
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.

Re: Calculate the size of grouped 2d objects

Posted: Thu Feb 18, 2016 7:50 pm
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

Re: Calculate the size of grouped 2d objects

Posted: Thu Feb 18, 2016 7:52 pm
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.

Re: Calculate the size of grouped 2d objects

Posted: Thu Feb 18, 2016 7:54 pm
by hallsofvallhalla
ahh i misunderstood some. Does PHP have a version of Javascripts Element.getBoundingClientRect()?

Re: Calculate the size of grouped 2d objects

Posted: Thu Feb 18, 2016 8:03 pm
by Chris
I think I'm trying to calculate a bounding box.

Re: Calculate the size of grouped 2d objects

Posted: Thu Feb 18, 2016 8:31 pm
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/

Re: Calculate the size of grouped 2d objects

Posted: Thu Feb 18, 2016 10:09 pm
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);
 

Re: Calculate the size of grouped 2d objects

Posted: Thu Feb 18, 2016 10:35 pm
by hallsofvallhalla
crazy stuff