缩放旋转的矩形以适合边界框内

时间:2016-08-12 06:25:38

标签: javascript math rotation

所以我调整了非旋转矩形的功能,但是我需要让它在旋转的矩形上工作。所以我的想法是调整旋转矩形的边界框,然后将旋转的矩形调整到里面。不幸的是,我无法弄清楚..以下是我所拥有的变量:

cW = currentRotatedRectangleWidth
cH = currentRotatedRectangleHeight
rad = angleOfRotation
cBW = currentRotatedRectangleBoundingWidth
cBW = Math.abs(cH * Math.sin(rad)) + Math.abs(cW * Math.cos(rad))
cBH = currentRotatedRectangleBoundingHeight
cBH = Math.abs(cH * Math.sin(rad)) + Math.abs(cW * Math.cos(rad))
nBW = newBoundingWidth
nBH = newBoundingHeight
dx = differenceWidth = (cBW - cW) / 2
dy = differenceHeight = (cBH - cH) / 2

enter image description here

所以我需要将旋转的矩形放入尺寸为nBW * nBH

的边界框中

1 个答案:

答案 0 :(得分:0)

我认为nBW / nBH比率可能与理想值不同。因此,您必须从垂直/水平系数中选择最小值,以适当地适应旋转的矩形。

 CoeffH = nbW / cBW
 CoeffV = nbH / cBH
 Coeff = Min(CoeffH, CoeffV)

现在用Coeff乘以线性尺寸。