Adam Shandi
Adam Shandi

Reputation: 21

Rotate a matrix

I am trying to rotate a given figure 90 degree.

fig = plt.figure()
points = [[0.3036, 0.1960], [0.6168, 0.2977], [0.7128, 0.4169], [0.7120, 0.1960],[0.9377,0.2620],\
          [0.7120,0.5680],[0.3989,0.6697],[0.3028,0.7889],[0.3036,0.5680],[0.5293,0.5020]]

bird = matplotlib.patches.Polygon(points, facecolor='blue')

fig, ax = plt.subplots()
ax.set_aspect("equal")
ax.add_patch(bird)

ax.set_xlim(0.2,1)
ax.set_ylim(0.2,0.9)
plt.show() 

Upvotes: 2

Views: 119

Answers (1)

qmeeus
qmeeus

Reputation: 2402

To rotate a matrix, you basically multiply your coordinates with a rotation matrix, that is given by

[[cos(theta), -sin(theta)], [sin(theta), cos(theta)]]

theta being the angle of rotation (so in your case [[0, -1], [1, 0]]).

So you just calculate the dot product like this:

points = np.array(points)
rotation_matrix = np.array([[0, -1], [1, 0]])
new_points = points.dot(rotation_matrix)

and then you can plot your new set of coordinates. This is the results (after adding (0, 1) to the coordinates so that the bird is in the frame... enter image description here

Upvotes: 4

Related Questions