user2785878
user2785878

Reputation: 75

How do I rotate a shape in python by +/- x degrees using numpy arrays?

I am new to python and I am trying to rotate a shape (it is a closed circle with cutouts inside) by +/- x degrees. The degree value is given by user interaction (user chooses the amount of degrees to rotate the figure by).

The function has to operate on the entire shape (including the cut outs) and I want to do it using numpy arrays.

I am new to python, and I am not really sure where to even start with it, so I don't have any coherent code to post, otherwise I would.

For every curve in the shape, I am trying to convert it to the following:

x' = xr + (x-xr)cos(angle) - (y - yr) sin(angle)

and

y' = yr + (x-xr)sin(angle) + (y - yr) cos(angle)

where (x',y') is the point in the output, angle = angle of rotation, (xr, yr) = point being rotated about, and then I want to return the rotated (modified) shape.

I am going to keep working on it, and see if I can come up with something better to post, but any help would be greatly appreciated.

If you have any questions, or if it needs clarification, let me know.

Upvotes: 4

Views: 4191

Answers (1)

JAC
JAC

Reputation: 601

Using your notation and assuming that both x,y are lists of the same size containing your curve, this is what I would do to get an array x_new:

xr_arr = xr * ones(len(x))
yr_arr = yr * ones(len(y))    
angle_arr = angle * ones(len(x))
x_new = xr_arr + (x - xr_arr)*cos(angle_arr) - (y - yr_arr)*sin(angle_arr)

This assumes you have included from numpy import * in your script. The array y_new can be obtained in the same way. If you had multiple curves you could do similarly using two-dimensional arrays.

EDIT: As Jaime mentions below, if x,y are numpy arrays, there is no need to vectorize xr,yr, and angle.

Upvotes: 3

Related Questions