“Ellipse”的版本间差异
|  (创建页面,内容为“<nowiki> import numpy as np from matplotlib import pyplot as plt from math import pi, cos, sin  u=1.       #x-position of the center v=0.5      #y-position of the ce…”) | 无编辑摘要 | ||
| 第1行: | 第1行: | ||
|  python code | |||
| <nowiki> | |||
| import numpy as np | |||
| 	import numpy as np | |||
| from  | 	from matplotlib import pyplot as plt | ||
| 	from math import pi, cos, sin | |||
| u=1.       #x-position of the center | 	u=1.       #x-position of the center | ||
| v=0.5      #y-position of the center | 	v=0.5      #y-position of the center | ||
| a=2.       #radius on the x-axis | 	a=2.       #radius on the x-axis | ||
| b=1.5      #radius on the y-axis | 	b=1.5      #radius on the y-axis | ||
| t_rot=pi/4 #rotation angle | 	t_rot=pi/4 #rotation angle | ||
| t = np.linspace(0, 2*pi, 100) | 	t = np.linspace(0, 2*pi, 100) | ||
| Ell = np.array([a*np.cos(t) , b*np.sin(t)])   | 	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)]])   | 	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])) | 	Ell_rot = np.zeros((2,Ell.shape[1])) | ||
| for i in range(Ell.shape[1]): | 	for i in range(Ell.shape[1]): | ||
| 		Ell_rot[:,i] = np.dot(R_rot,Ell[:,i]) | |||
| plt.plot( u+Ell[0,:] , v+Ell[1,:] )     #initial ellipse | 	plt.plot( u+Ell[0,:] , v+Ell[1,:] )     #initial ellipse | ||
| plt.plot( u+Ell_rot[0,:] , v+Ell_rot[1,:],'darkorange' )    #rotated ellipse | 	plt.plot( u+Ell_rot[0,:] , v+Ell_rot[1,:],'darkorange' )    #rotated ellipse | ||
| plt.grid(color='lightgray',linestyle='--') | 	plt.grid(color='lightgray',linestyle='--') | ||
| plt.show() | 	plt.show() | ||
| </nowiki> | |||
2019年6月27日 (四) 03:31的版本
python code
import numpy as np from matplotlib import pyplot as plt from math import pi, cos, sin
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
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])
plt.plot( u+Ell[0,:] , v+Ell[1,:] ) #initial ellipse plt.plot( u+Ell_rot[0,:] , v+Ell_rot[1,:],'darkorange' ) #rotated ellipse plt.grid(color='lightgray',linestyle='--') plt.show()