
Rotate a vector by angle (degree, radian) in NumPy
How to rotate the 2D vector by degree in Python:
from math import cos, sin
import numpy as np
theta = np.deg2rad(30)
rot = np.array([[cos(theta), -sin(theta)], [sin(theta), cos(theta)]])
v = np.array([1, 0])
w = np.array([3, 4])
v2 = np.dot(rot, v)
w2 = np.dot(rot, w)
print(v2) # [0.8660254 0.5 ]
print(w2) # [0.59807621 4.96410162]
rot
is a rotation matrix.
\[ \rm{Rot} = \left( \begin{array}{ccc} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{array} \right) \]
The image of rotation by 30 degrees

from math import cos, sin
import numpy as np
from matplotlib import pyplot
theta = np.deg2rad(30)
rot = np.array([[cos(theta), -sin(theta)], [sin(theta), cos(theta)]])
v = np.array([1, 0])
w = np.array([3, 4])
v2 = np.dot(rot, v)
w2 = np.dot(rot, w)
print(v2) # [0.8660254 0.5 ]
print(w2) # [0.59807621 4.96410162]
axes = pyplot.gca()
axes.set_xlim([-6, 6])
axes.set_ylim([-6, 6])
c1 = pyplot.Circle((0, 0), 1, fill=False)
c2 = pyplot.Circle((0, 0), 5, fill=False)
axes.set_aspect(1)
axes.add_artist(c1)
axes.add_artist(c2)
x = np.array([v[0], w[0], v2[0], w2[0]])
y = np.array([v[1], w[1], v2[1], w2[1]])
pyplot.scatter(x, y)
pyplot.savefig('rotate.jpg')
Comments
Powered by Markdown