1

I have a 2x32 numpy array of x,y coordinates "A", and I want to rotate it by 45 degrees, around the centre.

x = np.tile([1,2,3,4],8)
y = np.repeat([1,2,3,4,5,6,7,8],4)
A = np.vstack((x,y)) # just a quick example

Is there a quick and easy way to do this?

example, start denotes im rotating around centre

Jack
  • 107
  • 7
  • Does this answer your question? [Rotate point about another point in degrees python](https://stackoverflow.com/questions/34372480/rotate-point-about-another-point-in-degrees-python) – Woodford Jan 11 '23 at 18:03

1 Answers1

4

Steps:

  1. Center your data on the origin using a translation
  2. Rotate the data about the origin (45 degrees clockwise = -pi/4 radians)
  3. Translate your data back to the original center

Using some linear algebra:

import numpy as np

x = np.tile([1,2,3,4],8)
y = np.repeat([1,2,3,4,5,6,7,8],4)
A = np.vstack((x,y)) # just a quick example

# Rotation matrix as in e.g. https://en.wikipedia.org/wiki/Rotation_matrix
theta = -np.pi / 4
rotate = np.array([
    [np.cos(theta), -np.sin(theta)],
    [np.sin(theta),  np.cos(theta)]
])

# Translation vector is the mean of all xs and ys. 
translate = A.mean(axis=1, keepdims=True)

Apply transformations:

out = A - translate    # Step 1
out = rotate @ out     # Step 2
out = out + translate  # Step 3

enter image description here

Chrysophylaxs
  • 5,818
  • 3
  • 10
  • 21