
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.
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);