I have a few objects on a 2d field:
I need to calculate the size of the items when grouped together programtically:
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.
Calculate the size of grouped 2d objects
Calculate the size of grouped 2d objects
Fighting for peace is declaring war on war. If you want peace be peaceful.
- hallsofvallhalla
- Site Admin
- Posts: 12023
- Joined: Wed Apr 22, 2009 11:29 pm
Re: Calculate the size of grouped 2d objects
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
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
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.
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.
- hallsofvallhalla
- Site Admin
- Posts: 12023
- Joined: Wed Apr 22, 2009 11:29 pm
Re: Calculate the size of grouped 2d objects
ahh i misunderstood some. Does PHP have a version of Javascripts Element.getBoundingClientRect()?
Re: Calculate the size of grouped 2d objects
I think I'm trying to calculate a bounding box.
Fighting for peace is declaring war on war. If you want peace be peaceful.
Re: Calculate the size of grouped 2d objects
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/
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.
Re: Calculate the size of grouped 2d objects
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.
- hallsofvallhalla
- Site Admin
- Posts: 12023
- Joined: Wed Apr 22, 2009 11:29 pm
Re: Calculate the size of grouped 2d objects
crazy stuff