I already checked below questions:
Plot Ellipse with matplotlib.pyplot (Python)
How to find the point on ellipse given the angle
I'm trying to find a point coordinates on rotated ellipse, actually I need the point coordinates that can be shown on image.
Here is the code, most of the them from the first SO question.
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()
If I change the angle I got a point on not-rotated ellipse(black point).
What I want to do is find the points on the rotated ellipse that I tagged artificially with red. Basically these are the points on ellipse which have minimum&maximum xy values.
Any suggestions?
Thanks a lot!
This, like many questions about ellipses, can easily be answered by representing an ellipse as the set of points p such that
p'*inv(C)*p = 1
where C is a symmetric (indeed positive definite) 2x2 matrix
In the case of an (unrotated) matrix with semi-major axis a and semi-minor axis b we have
C = (a*a 0 )
( 0 b*b)
The rotated (via a rotation matrix R) ellipse is represented by
p'*inv(D)*p = 1
where D = R*C*R'
If p is a point on the ellipse, the vector
n = inv(D)*p
is at right angles to the tangent to the ellipse at p. In your case, the points at maximum and minimum x have normals to their tangents that are vertical; similarly the points at maximum and minimum y have normals to their tangents that are horizontal.
So, for example to find the point at maximum y, we:
put n = (0,1)
compute p = D*n (ie solve n = inv(D)*p for p)
But we need to scale this point so it is on the ellipse; a little algebra shows the appropriate point p is
p = D*n / sqrt( n'*D*n)
The point at minimum y is -p. For the points at minimum/maximum x, we start with n = (1,0)
I was able to draw the bounding boxes that Robert suggest and it worked
Here is what I've added to calculate the lines
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))
And here is the full code:
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()
Here is the visual:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.