【问题标题】:How to find a point coordinates on rotated ellipse?如何在旋转椭圆上找到点坐标?
【发布时间】:2021-05-05 15:39:22
【问题描述】:

我已经检查了以下问题:

Plot Ellipse with matplotlib.pyplot (Python)

How to find the point on ellipse given the angle

我正在尝试在旋转的椭圆上找到一个点坐标,实际上我需要可以在图像上显示的点坐标。

这是代码,其中大部分来自第一个 SO 问题。

u=1.       #x-position of the center
v=0.5      #y-position of the center
a=2.       #radius on the x-axis
b=1.5      #radius on the y-axis
t_rot=pi/4 #rotation angle

angle = 15 # always on blue circle

t = np.linspace(0, 2*pi, 100)
Ell = np.array([a*np.cos(t) , b*np.sin(t)])  
     #u,v removed to keep the same center location
R_rot = np.array([[cos(t_rot) , -sin(t_rot)],[sin(t_rot) , cos(t_rot)]])  
     #2-D rotation matrix

Ell_rot = np.zeros((2,Ell.shape[1]))
for i in range(Ell.shape[1]):
    Ell_rot[:,i] = np.dot(R_rot,Ell[:,i])

#ellipses
plt.plot( u+Ell[0,:] , v+Ell[1,:] )     #initial ellipse
plt.plot( u+Ell_rot[0,:] , v+Ell_rot[1,:],'magenta' )    #rotated ellipse
#points
plt.scatter(u,v,s=10,color='black') # center
plt.scatter(u + a*cos(angle),v + b*sin(angle),s=50,color='black') #point on blue ellipse

plt.grid(color='lightgray',linestyle='--')
plt.show()

如果我改变角度,我会在未旋转的椭圆(黑点)上得到一个点。

我想要做的是找到我用红色人工标记的旋转椭圆上的点。基本上这些是椭圆上具有最小和最大 x-y 值的点。

有什么建议吗?

非常感谢!

【问题讨论】:

  • 一般来说,您要查找的内容称为边界框;对该词的网络搜索可能会找到一些资源。顺便说一句,您是否试图在不实际计算所有点的情况下找到边界框?
  • @Robert Dodier,感谢“边界框”概念,我一定会调查的。
  • @Pranav Hosangadi,我不知道如何回复您的评论。我没明白你的意思。先生,您能分享您的专业知识并解释一下吗?

标签: python math linear-algebra ellipse


【解决方案1】:

这就像许多关于椭圆的问题一样,可以通过将椭圆表示为点 p 的集合来轻松回答

p'*inv(C)*p = 1
where C is a symmetric (indeed positive definite) 2x2 matrix

对于具有半长轴 a 和半短轴 b 的(未旋转的)矩阵,我们有

C = (a*a  0  )
    (  0  b*b)

旋转的(通过旋转矩阵 R)椭圆表示为

p'*inv(D)*p = 1
where D = R*C*R'

如果p是椭圆上的一个点,向量

n = inv(D)*p 

与椭圆在 p 处的切线成直角。在您的情况下,最大和最小 x 处的点具有垂直切线的法线;类似地,最大值和最小值 y 处的切线的法线是水平的。

因此,例如要找到最大 y 处的点,我们:

put n = (0,1)
compute p = D*n (ie solve n = inv(D)*p for p)

但是我们需要缩放这个点,使它在椭圆上;一点代数显示适当的点 p 是

p = D*n / sqrt( n'*D*n)

最小y的点是-p。 对于最小/最大 x 处的点,我们从 n = (1,0) 开始

【讨论】:

  • 对符号有点困惑,因为我在代数方面有点生疏,而且我以前从未见过这种表示(矩阵)。你如何计算旋转矩阵? '最大和最小 x 的点对其切线有法线' 我不明白这句话中的'法线',你能解释一下吗?这有点多,但如果你能提供一个例子,我认为它会更好地理解。
  • @Tyr 旋转矩阵是[cos(a) -sin(a); sin(a) cos(a)](按行),其中a 是旋转角度。有关“旋转矩阵”,请参阅 Wikipedia 文章。 “正常”= 垂直。
【解决方案2】:

我能够画出 Robert 建议的边界框,而且效果很好

这是我添加的用于计算线条的内容

xa = np.sqrt((a**2*np.cos(t_rot)**2)+(b**2*np.sin(t_rot)**2))
ya = np.sqrt((a**2*np.sin(t_rot)**2)+(b**2*np.cos(t_rot)**2))

这是完整的代码:

u=3       #x-position of the center
v=6      #y-position of the center
a=np.sqrt(2)       #radius on the x-axis
b=1      #radius on the y-axis
t_rot=pi/6 #rotation angle    

t = np.linspace(0, 2*pi, 100)
Ell = np.array([a*np.cos(t) , b*np.sin(t)])  
     #u,v removed to keep the same center location
R_rot = np.array([[cos(t_rot) , -sin(t_rot)],[sin(t_rot) , cos(t_rot)]])  
     #2-D rotation matrix

Ell_rot = np.zeros((2,Ell.shape[1]))
for i in range(Ell.shape[1]):
    Ell_rot[:,i] = np.dot(R_rot,Ell[:,i])


xa = np.sqrt((a**2*np.cos(t_rot)**2)+(b**2*np.sin(t_rot)**2))
ya = np.sqrt((a**2*np.sin(t_rot)**2)+(b**2*np.cos(t_rot)**2))

#ellipses
plt.plot( u+Ell_rot[0,:] , v+Ell_rot[1,:],'magenta')    #rotated ellipse
#points
plt.scatter(u,v,s=10,color='black') # center 

#bounding box lines
plt.axhline(y=ya+v,color='r',label=str(ya+v))
plt.axhline(y=-ya+v,color='b',label=str(-ya+v))
plt.axvline(x=xa+u, color='g',label=str(xa+u))
plt.axvline(x=-xa+u, color='y',label=str(-xa+u))
plt.legend()

plt.grid(color='lightgray',linestyle='--')
plt.show()

这是视觉效果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2015-05-08
    • 2021-12-24
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    相关资源
    最近更新 更多