简体   繁体   English

如何使用numpy数组将python中的形状旋转+/- x度?

[英]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. 我是python的新手,我正在尝试旋转一个形状(它是一个带有切口的封闭圆圈)+/- x度。 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. 该功能必须对整个形状(包括切口)进行操作,我想使用numpy数组。

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. 我是python的新手,我不确定从哪里开始,所以我没有任何连贯的代码发布,否则我会。

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. 其中(x',y')是输出中的点, angle =旋转角度, (xr, yr) =旋转的点,然后我想返回旋转(修改)的形状。

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. 如果您有任何疑问,或需要澄清,请告诉我。

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: 使用你的符号并假设x,y都是包含你的曲线的相同大小的列表,这就是我要做的数组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. 这假设您已在脚本中包含from numpy import * The array y_new can be obtained in the same way. 可以以相同的方式获得阵列y_new。 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. 编辑:正如Jaime在下面提到的,如果x,y是numpy数组,则不需要向量化xr,yr和angle。

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

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