简体   繁体   English

使用 x,y,theta 在 python 中旋转椭圆

[英]Rotating ellipse in python using x,y,theta

I'm trying to draw an ellipse in python using the following equations:我正在尝试使用以下等式在 python 中绘制一个椭圆:

xpos = a*np.cos(theta)
ypos = b*np.sin(theta)

椭圆

This works, but when I try to rotate the resulting ellipse using:这可行,但是当我尝试使用以下方法旋转生成的椭圆时:

xpos = xpos*np.cos(np.pi/2)+ypos*np.sin(np.pi/2)
ypos = -xpos*np.sin(np.pi/2)+ypos*np.cos(np.pi/2)

The ellipse becomes a line, rather than just rotated by 90 degrees.椭圆变成了一条线,而不是仅仅旋转了 90 度。 What is causing this?这是什么原因造成的? 旋转椭圆

Your problem is that you're redefining xpos first and using that one for your new ypos , basically you're not doing the transformation of coordinates simultaneously.您的问题是您首先重新定义xpos并将其用于新的ypos ,基本上您没有同时进行坐标转换。

If you create new variables for the points in the new coordinate system them you get the rotated ellipse.如果您为新坐标系中的点创建新变量,则会得到旋转的椭圆。

import numpy as np
import matplotlib.pyplot as plt

theta = np.arange(0, 2*np.pi, 0.01)
a = 1
b = 2

xpos = a*np.cos(theta)
ypos = b*np.cos(theta)

new_xpos = xpos*np.cos(np.pi/2)+ypos*np.sin(np.pi/2)
new_ypos = -xpos*np.sin(np.pi/2)+ypos*np.cos(np.pi/2)

plt.plot(xpos, ypos, 'b-')
plt.plot(new_xpos, new_ypos, 'r-')

plt.show()

For anyone thinking there's something wrong with the code from Ignacio, ypos should be ypos= b*np.sin(theta)对于任何认为 Ignacio 的代码有问题的人,ypos 应该是 ypos= b*np.sin(theta)

hence,因此,

import numpy as np
import matplotlib.pyplot as plt

theta = np.arange(0, 2*np.pi, 0.01)

a = 1
b = 2

xpos = a*np.cos(theta)
ypos = b*np.sin(theta)

new_xpos = xpos*np.cos(np.pi/2)+ypos*np.sin(np.pi/2)
new_ypos = -xpos*np.sin(np.pi/2)+ypos*np.cos(np.pi/2)

plt.plot(xpos,ypos, 'b')
plt.plot(new_xpos,new_ypos, 'r')
plt.axis('equal')
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM