繁体   English   中英

需要找到在多个线段上迭代的两个线段的交点

[英]Need to find the intersection point of two line segments iterated over multiple segments

好,这是问题所在。 我正在尝试通过比较从一系列csv文件中读取的多个线段来计算两条线的交点。 我已经将每个线段的x,y坐标对放入元组内的元组列表中,如下所示:

continuousLine = [((x1,y1),(x2,y2)), ...]
crossingLines = [((x1,y1),(x2,y2)), ...]

线路问题

我正在尝试找出如何沿实线和交叉线进行迭代,以找出每条交叉线沿连续线相交的位置。

基本上我想要(下面列出的伪代码):

for segment in continuousLine:
    if segment in crossingLines intersect == True:
       return intersection
    else:
       move on to next crossing line segment and repeat test

我讨厌在此方面寻求帮助,因为我太陌生了,无法编写代码来帮助其他人,但是希望有人可以与我一起解决这个问题。

我确实有一种方法,一旦我弄清楚了迭代器,就可以计算交点:

line1 = ()
line2 = ()
def line_intersection(line1, line2):
    xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
    ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1]) #Typo was here

    def det(a, b):
        return a[0] * b[1] - a[1] * b[0]

    div = det(xdiff, ydiff)
    if div == 0:
       raise Exception('lines do not intersect')

    d = (det(*line1), det(*line2))
    x = det(d, xdiff) / div
    y = det(d, ydiff) / div
    return x, y

print line_intersection((A, B), (C, D))

假设函数line_intersection将在div == 0上返回False而不是增加异常。

简单的方法:

filter(None, [intersection(a, b) for a in continuousLine for b in crossingLines])

但是,使用嵌套循环时,在crossingLines中有许多段时,速度很慢。

一种更有效的方法:

为了提高性能,请尝试一下intervaltree ,它将为您提供相交的候选对象进行测试。 在您的情况下,首先在crossingLines上构建一个间隔树,然后遍历continuousLine在该树中找到相交的候选对象,然后进行测试以获取最终结果。

由于您没有提供任何示例输入和预期输出,因此我将使用下面所示的line_intersection()函数的虚拟版本。 它所做的只是打印其输入并返回一个硬编码的结果,但是它将向您展示如何遍历输入数据并将其传递给实函数。

从输出中应该清楚它在做什么。

def line_intersection(line1, line2):
    print('intersecting:')
    print('  ({}. {}), ({}, {})'.format(line1[0][0], line1[0][1],
                                        line1[1][0], line1[1][1]))
    print('  ({}. {}), ({}, {})'.format(line2[0][0], line2[0][1],
                                        line2[1][0], line2[1][1]))
    print('')
    return 100, 200

所有循环都已放入名为find_intersections()的生成器函数中,该函数将返回找到的连续交集,并跳过所有未找到的交集。

def find_intersections(continuous_line, crossing_lines):
    for cl_segment in continuous_line:
        for xl_segment in crossing_lines:
            try:
                x, y = line_intersection(cl_segment, xl_segment)
            except Exception:
                pass
            else:
                yield x, y

这是一个包含输入数据的用法示例:

continuous_line = [((1,2),(3,4)), ((5,6),(7,8)), ((9,10),(11,12))]
crossing_lines = [((21,22),(23,24)), ((25,26),(27,28)), ((29,30),(31,32))]
intersections = [(x, y) for x, y in
                    find_intersections(continuous_line, crossing_lines)]
print('intersections found:')
print(intersections)

输出:

intersecting:
  (1. 2), (3, 4)
  (21. 22), (23, 24)

intersecting:
  (1. 2), (3, 4)
  (25. 26), (27, 28)

intersecting:
  (1. 2), (3, 4)
  (29. 30), (31, 32)

intersecting:
  (5. 6), (7, 8)
  (21. 22), (23, 24)

intersecting:
  (5. 6), (7, 8)
  (25. 26), (27, 28)

intersecting:
  (5. 6), (7, 8)
  (29. 30), (31, 32)

intersecting:
  (9. 10), (11, 12)
  (21. 22), (23, 24)

intersecting:
  (9. 10), (11, 12)
  (25. 26), (27, 28)

intersecting:
  (9. 10), (11, 12)
  (29. 30), (31, 32)

intersections found:
[(100, 200), (100, 200), (100, 200), (100, 200), (100, 200), (100, 200), (100,
200), (100, 200), (100, 200)]

暂无
暂无

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

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