[英]Finding all angles between every sequential line segment
我需要一個 function 來將每個點視為原點,從最接近 (0,0) 的點開始,然后計算從該點到每個其他點的每個線段的角度和長度。 它應該跳過任何會創建之前已經計算過的相同線段的東西
但是,我的 function 並沒有考慮到我的第一個點到第二個點的線段。 請幫忙......這是function
import math
def compute_vectors(points):
vectors = []
previous = points[0]
for point in points:
x = point[0] - previous[0]
y = point[1] - previous[1]
angle = math.degrees(math.atan2(y, x))
if angle < 0:
angle += 360
length = math.sqrt(x**2 + y**2)
vectors.append((angle, length))
previous = point
return vectors[1:]
points = [[1,1],[2,2],[3,1]]
compute_vectors(points)
>>> [(45.0, 1.4142135623730951), (315.0, 1.4142135623730951)]
我需要它到 output
>>> [(45.0, 1.4142135623730951),(0.0, 2),(315.0, 1.4142135623730951)]
我認為您想使用嵌套的 for 循環來創建要比較的對。 考慮一個矩陣,其中的值是輸入列表的索引列表與其自身的笛卡爾積:
00 01 02 03 04 05
10 11 12 13 14 15
20 21 22 24 24 25
30 31 32 33 34 35
40 41 42 43 44 45
50 51 52 53 54 55
我把對角線分割放在那里是因為你想要的優化(只為尚未考慮的對生成值)可以通過只選擇矩陣上半部分的索引對(不包括主對角線)來實現。
這樣的事情可能會起作用:
for idx, pt1 in enumerate(pts):
for idx2 in range(idx+1, len(pts)):
pt2 = pts[idx2]
<do computations with pt1 and pt2>
.....
像這樣使用:
>>> l=[(1, 1), (2, 2), (3, 3), (4, 4)]
>>> for idx, val in enumerate(l):
... for idx2 in range(idx+1, len(l)):
... val2 = l[idx2]
... print(f"point 1 = {val}", f"point2 = {val2}")
...
point 1 = (1, 1) point2 = (2, 2)
point 1 = (1, 1) point2 = (3, 3)
point 1 = (1, 1) point2 = (4, 4)
point 1 = (2, 2) point2 = (3, 3)
point 1 = (2, 2) point2 = (4, 4)
point 1 = (3, 3) point2 = (4, 4)
import itertools
import math
def compute_angles_and_lengths(points):
angles_and_lengths = []
for i in itertools.combinations(points, 2):
x = i[1][0] - i[0][0]
y = i[1][1] - i[0][1]
lengths = math.sqrt(x**2+y**2)
angle = math.degrees(math.atan2(y,x))
angles_and_lengths.append((angle, lengths))
return angles_and_lengths
points = [[1,1],[2,2],[3,1]]
compute_angles_and_lengths(points)
>>> [(45.0, 1.4142135623730951), (0.0, 2.0), (-45.0, 1.4142135623730951)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.