繁体   English   中英

查找每个连续线段之间的所有角度

[英]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.

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