Python 绘制椭圆 平移 旋转

前言:使用极坐标系描述起来似乎更加方便。

效果

Python 绘制椭圆 平移 旋转_第1张图片 Python 绘制椭圆 平移 旋转_第2张图片 Python 绘制椭圆 平移 旋转_第3张图片 Python 绘制椭圆 平移 旋转_第4张图片 Python 绘制椭圆 平移 旋转_第5张图片

椭圆公式演变

直角坐标形式
x 2 a 2 + y 2 b 2 = 1 \frac{x^2}{a^2}+\frac{y^2}{b^2}=1 a2x2+b2y2=1
极坐标形式
x = a c o s θ y = b s i n θ x=acos\theta\\y=bsin\theta x=acosθy=bsinθ
纯粹平移:右移 s x s_x sx,上移 s y s_y sy,得到 x x = x + s x y y = y + s y x_x=x+s_x\\y_y=y +s_y xx=x+sxyy=y+sy
纯粹旋转: 旋转 α \alpha α角度
x x x = c o s α ⋅ x + s i n α ⋅ y y y y = − s i n α ⋅ x + c o s α ⋅ y x_{xx}=cos\alpha \cdot x+sin\alpha\cdot y\\y_{yy}=-sin\alpha\cdot x+cos\alpha\cdot y xxx=cosαx+sinαyyyy=sinαx+cosαy
x = r ⋅ c o s t y = r ⋅ s i n t x=r\cdot cos t\\y=r\cdot sint x=rcosty=rsint
x x x = r ⋅ c o s ( t + α ) = r ⋅ ( c o s t c o s α − s i n t s i n α ) = x ⋅ c o s α − y ⋅ s i n α y y y = r ⋅ s i n ( t + α ) = r ⋅ ( c o s t s i n α + s i n t c o s α ) = x ⋅ s i n α + y ⋅ cos ⁡ α x_{xx}=r\cdot cos(t+\alpha)=r\cdot(cost cos\alpha-sint sin\alpha)=x\cdot cos\alpha-y\cdot sin\alpha \\ y_{yy}=r\cdot sin(t+\alpha)=r\cdot(cost sin\alpha+sint cos\alpha)=x\cdot sin\alpha+y\cdot\cos\alpha xxx=rcos(t+α)=r(costcosαsintsinα)=xcosαysinαyyy=rsin(t+α)=r(costsinα+sintcosα)=xsinα+ycosα进而
x x x = a c o s θ c o s α − b s i n θ s i n α y y y = a c o s θ s i n α + b s i n θ c o s α x_{xx}=acos\theta cos\alpha-bsin\theta sin\alpha\\ y_{yy}=acos\theta sin\alpha+bsin\theta cos\alpha xxx=acosθcosαbsinθsinαyyy=acosθsinα+bsinθcosα
旋转+平移:
x x x x = x x x + s x y y y y = y y y + s y x_{xxx}=x_{xx} +s_x\\y_{yyy}=y_{yy}+s_y xxxx=xxx+sxyyyy=yyy+sy
(一定是先旋转在平移,因为这里的旋转是根据旋转矩阵来实现表达的,而旋转矩阵是针对原点旋转的)

Python 代码

import matplotlib.pyplot as plt
import numpy as np

rotAngle = np.pi/6# 旋转角度
shiftX = 1 # x 轴平移量
shiftY = 2 # y 轴平移量

t = np.arange(0,2*np.pi,0.01)
x = np.cos(t)*2
y = np.sin(t)*4
plt.plot(x,y)# 绘制椭圆

# 平移
xxx = x + shiftX
yyy = y + shiftY
plt.plot(xxx,yyy)

# 旋转
xx = np.cos(rotAngle)*x - np.sin(rotAngle)*y
yy = np.sin(rotAngle)*x + np.cos(rotAngle)*y
plt.plot(xx,yy)

# 旋转+平移 
#(一定是先旋转在平移,因为这里的旋转是根据旋转矩阵来实现表达的,而旋转矩阵是针对原点旋转的)
xxxx = np.cos(rotAngle)*x - np.sin(rotAngle)*y+shiftX
yyyy = np.sin(rotAngle)*x + np.cos(rotAngle)*y+shiftY
plt.plot(xxxx,yyyy)

plt.axis('equal')# 调整显示的横纵轴比例
plt.show()# 真正显示出上述的绘图结果

你可能感兴趣的:(Python相关)