简体   繁体   English

将numpy坐标数组旋转45度

[英]Rotate numpy array of coordinates by 45 degrees

I have a 2x32 numpy array of x,y coordinates "A", and I want to rotate it by 45 degrees, around the centre.我有一个 x、y 坐标“A”的 2x32 numpy 数组,我想将它围绕中心旋转 45 度。

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?有没有快速简便的方法来做到这一点?

例如,start 表示我围绕中心旋转

Steps:脚步:

  1. Center your data on the origin using a translation使用翻译将数据集中在原点
  2. Rotate the data about the origin (45 degrees clockwise = -pi/4 radians)围绕原点旋转数据(顺时针 45 度 = -pi/4 弧度)
  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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM