如何计算椭圆的轴对齐边界框? [英] How do you calculate the axis-aligned bounding box of an ellipse?

查看:256
本文介绍了如何计算椭圆的轴对齐边界框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果椭圆的长轴是垂直或水平的,那么计算边界框很容易,但是当椭圆被旋转时怎么样?

唯一的方法我能想到的是计算周边的所有点并找出最大/最小x和y值。这似乎应该有一个更简单的方法。

如果有一个函数(在数学意义上)描述一个任意角度的椭圆,那么我可以使用它的导数找到斜率为零或未定义的点,但我似乎无法找到一个。



编辑:为了阐明,我需要轴对齐边界框,即它不应该与椭圆一起旋转,而是保持与x轴对齐,因此变换边界框将不起作用。 解决方案

您可以尝试使用以任意角度旋转的椭圆的参数化方程:

  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,并旋转角度φ。

然后,您可以区分并求解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。



重复[2 ]:
$ b $ pre $ code> 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旋转:

[1] =>

  x = 2 * cos(t)* cos(PI / 4) -  sin(t)* sin(PI / 4)


$

$ p $ tan $(t)= -tan(PI / 4)/ 2 = -1/2

=>

  t = -0.4636 + n * PI 

因此我们得到:

pre $ $



$ b code> x = 2 * cos(-0.4636)* cos(PI / 4) - sin(-0.4636)* sin(PI / 4)= 1.5811
$ b $ p

$ $ $ $ $ $ c $ x $ 2 $ cos $ -3.6052 * cos (PI / 4) - sin(-3.6052)* sin(PI / 4)= -1.5811


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?

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.

Edit: to clarify, I need the axis-aligned bounding box, i.e. it should not be rotated with the ellipse, but stay aligned with the x axis so transforming the bounding box won't work.

解决方案

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]

...where ellipse has centre (h,k) semimajor axis a and semiminor axis b, and is rotated through angle phi.

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]

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.

Repeat for [2]:

0 = dy/dt = b*cos(t)*cos(phi) - a*sin(t)*sin(phi)

=>

tan(t) = b*cot(phi)/a  [4]

Lets try an example:

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

We are interested in t = -0.4636 and t = -3.6052

So we get:

x = 2*cos(-0.4636)*cos(PI/4) - sin(-0.4636)*sin(PI/4) = 1.5811

and

x = 2*cos(-3.6052)*cos(PI/4) - sin(-3.6052)*sin(PI/4) = -1.5811

这篇关于如何计算椭圆的轴对齐边界框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆