我在2d字段上有几个对象: https://i.imgsafe.org/4d9f0a2.png
我需要在程序化分组时计算项目的大小: https://i.imgsafe.org/7697e03.png
我有每个物体的弧度大小,位置和旋转。
有人知道这种做法或做法吗?
我需要在PHP中完成。
使用一些代码进行更新:
class Layer {
/**
* @var float The X position
*/
public $x;
/**
* @var float The Y position
*/
public $y;
/**
* @var float The width of the rectanlge
*/
public $width;
/**
*
* @var float The height of the rectangle
*/
public $height;
/**
*
* @var float The rotation in radians
*/
public $rotation;
}
$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 = 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;
我现在需要一种循环遍历$ layers来计算边界框的方法。
答案 0 :(得分:0)
我认为这是正确的计算:
$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);