簡體   English   中英

圍繞另一個點旋轉一組2D點

[英]Rotating a set of 2D points about another point

我想圍繞給定點旋轉坐標數組。 為了認識到我通過使用for循環旋轉每個坐標並將旋轉后的坐標放回數組中。 當執行代碼時,我得到IndexError: index 1 is out of bounds for axis 0 with size 1因此循環中一定存在一個我無法識別的錯誤。

import math
import numpy as np

airfoil = np.array([[3349.67075, 2138.     ],
       [3225.86375, 2137.77425],
       [3060.79325, 2137.757  ],
       [2901.63575, 2136.89675],
       [2803.16825, 2136.89   ],
       [2728.33625, 2136.719  ],
       [2687.33225, 2136.89   ],
       [2611.475  , 2136.377  ],
       [2600.     , 2138.     ],
       [2602.24925, 2146.457  ],
       [2605.66625, 2152.2665 ],
       [2611.475  , 2158.7585 ],
       [2618.65025, 2164.39625],
       [2638.12775, 2176.0145 ],
       [2680.49825, 2193.95375],
       [2725.0895 , 2208.134  ],
       [2786.08325, 2220.2645 ],
       [2853.398  , 2227.61075]])


theta = 1.5708  # 90 degree
ox, oy = 2000, 2000  # point to rotate about

for i in range(airfoil.shape[0]-1):
    qx = ox + math.cos(theta) * (airfoil[i][0] - ox) - math.sin(theta) * 
        (airfoil[i][1] - oy)
    qy = oy + math.sin(theta) * (airfoil[i][0] - ox) + math.cos(theta) * 
        (airfoil[i][1] - oy)
    airfoil = np.column_stack((qx, qy))

可以使用airfoil[0][0]airfoil[0][1]將元素(x和y坐標)調用為airfoil[17][0]airfoil[17][1]而不會出現任何問題。 因此,錯誤一定在其他地方。

我已經讀過類似的問題,這些問題對我沒有幫助。

在循環內部使用airfoil = np.column_stack((qx, qy))並不是一個好主意,因為它在每次迭代時都會修改airfoil數組。 實際上,通過執行numpy.column_stack您可以在第一次迭代后覆蓋具有形狀(18,2)的原始airfoil ,使翼型具有形狀(1,2) (18,2) (因此,在第二次迭代中,它會給您形狀誤差)。

將旋轉點存儲在另一個變量上會更好。 甚至更好的是,使用簡單的A v = w一次執行所有旋轉,其中A是您的旋轉矩陣, v您的機翼坐標, w是旋轉的坐標。

這是您可以使用旋轉矩陣A

theta = 1.5708  # 90 degree
ox, oy = 2000, 2000  # point to rotate about
A = np.matrix([[np.cos(theta), -np.sin(theta)],
               [np.sin(theta), np.cos(theta)]])

w = np.zeros(airfoil.shape)
airfoil_shifted = airfoil-np.array([ox,oy])
for i,v in enumerate(airfoil_shifted):
  w[i] = A @ v

其中w將包含旋轉的坐標。

我修改了代碼,現在可以使用了。

theta = 1.5708
ox, oy = 2000, 2000

qx = []
qy = []

for i in range(airfoil.shape[0]):
    qx.append(ox + math.cos(theta) * (airfoil[i][0] - ox) - math.sin(theta) * (airfoil[i][1] - oy))
    qy.append(oy + math.sin(theta) * (airfoil[i][0] - ox) + math.cos(theta) * (airfoil[i][1] - oy))

airfoil = np.column_stack((qx, qy))

不過,我同意使用矩陣乘法比我的解決方案更優雅。 謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM