2017-08-02 119 views
1

2D座標列表我的2D在Python座標對(這實際上表示的路徑)以下列表的多個序列:旋轉(和調整大小)在Python

a = [[0, 0], [1, 0.4], [1.2, 0.6], [1.6, 0.9], [1.9, 2.1]] 
b = [[-0.3, 0.5], [1.33, 0.46], [2.34, 0.6], [2.6, 1]] 
c = [[10, 0.2], [10.1, 0.3], [10.2, 0.4], [11.6, 0.5], [13.9, 0.77]] 

的清單的長度不同,而不是每個的它們從開始[0,0]

現在,我想「正常化」它們,使得它們中的每從[0, 0]開始和旋轉它們按比例,以使最終點是在X軸[+something, 0]的正方向。我怎樣才能實現這種轉變,從而保持原來的比例?

獎金問題:我怎麼解決這個問題,以便最後一點總是在[1, 0],所以路徑不僅是旋轉的,而且是調整大小的?

+0

這實在不是一個編程的問題,而是一個線性代數的問題。 –

+1

你嘗試了什麼?第一步有點明顯 –

回答

1

的第一步是直接的。

對於第二,可以使用atan2來計算X軸和最後點之間的角度。對每個點應用rotation-theta,就完成了。

它不應該是現在很難寫的第三函數。

from math import cos, sin, atan2 

a = [[0, 0], [1, 0.4], [1.2, 0.6], [1.6, 0.9], [1.9, 2.1]] 
b = [[-0.3, 0.5], [1.33, 0.46], [2.34, 0.6], [2.6, 1]] 
c = [[10, 0.2], [10.1, 0.3], [10.2, 0.4], [11.6, 0.5], [13.9, 0.77]] 

def move_to_origin(l): 
    x0, y0 = l[0] 
    return [(x - x0, y - y0) for x, y in l] 

def rotate_to_x_axis(l): 
    xn, yn = l[-1] 
    theta = atan2(-yn, xn) 
    return [(x*cos(theta) - y*sin(theta), x*sin(theta) + y*cos(theta)) for x, y in l] 

print(move_to_origin(a)) 
# [(0, 0), (1, 0.4), (1.2, 0.6), (1.6, 0.9), (1.9, 2.1)] 
print(move_to_origin(b)) 
# [(0.0, 0.0), (1.6300000000000001, -0.03999999999999998), (2.6399999999999997, 0.09999999999999998), (2.9, 0.5)] 
print(move_to_origin(c)) 
# [(0, 0.0), (0.09999999999999964, 0.09999999999999998), (0.1999999999999993, 0.2), (1.5999999999999996, 0.3), (3.9000000000000004, 0.5700000000000001)] 
print(rotate_to_x_axis(move_to_origin(a))) 
# [(0.0, 0.0), (0.9675276356186346, -0.47317044953612064), (1.250017456237214, -0.4872949405670496), (1.740843519561996, -0.5826352550258203), (2.831960451701259, 0.0)] 

這裏有一個小matplotlib圖與步驟b

import matplotlib.pyplot as plt 
plt.plot(*zip(*b)) 
plt.plot(*zip(*move_to_origin(b))) 
plt.plot(*zip(*rotate_to_x_axis(move_to_origin(b)))) 
plt.gca().set_aspect('equal', adjustable='box') 
plt.show() 

enter image description here

+0

很好的解決方法,謝謝。我想知道爲了將所有座標值擠壓(標準化)到0-1(或至少-1-1)的範圍,如何進一步調節。 – Hendrik

+0

@亨德里克:你嘗試了什麼?你需要哪個值?你會如何得到它?這個函數與'move_to_origin'不同 –