你如何计算椭圆的轴对齐边界框? [英] How do you calculate the axis-aligned bounding box of an ellipse?
问题描述
如果椭圆的长轴是垂直的或水平的,那么边界框的计算很容易,但是当椭圆旋转时呢?
If the major axis of the ellipse is vertical or horizontal, it's easy to calculate the bounding box, but what about when the ellipse is rotated?
到目前为止,我能想到的唯一方法是计算周边的所有点并找到最大/最小 x 和 y 值.好像应该有更简单的方法.
The only way I can think of so far is to calculate all the points around the perimeter and find the max/min x and y values. It seems like there should be a simpler way.
如果有一个函数(在数学意义上)以任意角度描述一个椭圆,那么我可以使用它的导数来找到斜率为零或未定义的点,但我似乎找不到.
If there's a function (in the mathematical sense) that describes an ellipse at an arbitrary angle, then I could use its derivative to find points where the slope is zero or undefined, but I can't seem to find one.
澄清一下,我需要轴对齐的边界框,即它不应该与椭圆一起旋转,但要与 x 轴保持对齐,因此无法转换边界框.
推荐答案
您可以尝试使用参数化方程来旋转任意角度的椭圆:
You could try using the parametrized equations for an ellipse rotated at an arbitrary angle:
x = h + a*cos(t)*cos(phi) - b*sin(t)*sin(phi) [1]
y = k + b*sin(t)*cos(phi) + a*cos(t)*sin(phi) [2]
...其中椭圆的中心为 (h,k) 长半轴 a 和短半轴 b,并旋转角度 phi.
...where ellipse has centre (h,k) semimajor axis a and semiminor axis b, and is rotated through angle phi.
然后您可以区分并求解梯度 = 0:
You can then differentiate and solve for gradient = 0:
0 = dx/dt = -a*sin(t)*cos(phi) - b*cos(t)*sin(phi)
=>
tan(t) = -b*tan(phi)/a [3]
这应该为您提供许多 t 解决方案(您对其中两个感兴趣),将其插入 [1] 以获得您的最大值和最小值 x.
Which should give you many solutions for t (two of which you are interested in), plug that back into [1] to get your max and min x.
重复 [2]:
0 = dy/dt = b*cos(t)*cos(phi) - a*sin(t)*sin(phi)
=>
tan(t) = b*cot(phi)/a [4]
让我们尝试一个例子:
考虑在 (0,0) 处的椭圆,a=2,b=1,旋转 PI/4:
Consider an ellipse at (0,0) with a=2, b=1, rotated by PI/4:
[1] =>
x = 2*cos(t)*cos(PI/4) - sin(t)*sin(PI/4)
[3] =>
tan(t) = -tan(PI/4)/2 = -1/2
=>
t = -0.4636 + n*PI
我们对 t = -0.4636 和 t = -3.6052 感兴趣
We are interested in t = -0.4636 and t = -3.6052
所以我们得到:
x = 2*cos(-0.4636)*cos(PI/4) - sin(-0.4636)*sin(PI/4) = 1.5811
和
x = 2*cos(-3.6052)*cos(PI/4) - sin(-3.6052)*sin(PI/4) = -1.5811
这篇关于你如何计算椭圆的轴对齐边界框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!