简体   繁体   English

找到两点系列的交集?

[英]Find the intersection of two point series?

A line is here defined as a series of 2D node points. 一条线在此定义为一系列2D节点。 Now, I have two such lines A and B . 现在,我有两条这样的线AB

A=[(0, 0), (1, 1), (2.1, 3), (4,7)]
B=[(2, 0), (2, 6)]

When one draws them on the paper, one can easily see the two lines intersect at a point that is NOT a node member of either A or B . 当一个吸引过来在纸上,可以很容易地看到两条线相交于一点是NOT任一节点构件AB

However, both A and B indeed cross this point . 但是, AB 确实都超过了这一点 That is, the point indeed lies both on A and B , just not collide with the node points. 也就是说,该点确实位于AB ,只是不与节点相撞。

I now wish to find the intersection point. 我现在希望找到相交点。

(a gentle reminder once again: the intersection is on A and B , but it may not be a node) (再次提醒:交点在AB ,但可能不是节点)

What I come up with now is to use a polynomial to fit each point series. 我现在想出的是使用多项式来拟合每个点系列。 In this way, I can solve for the intersection with the equations. 这样,我可以求解方程的交集。 However, it seems to be a quite stupid way to myself. 但是,这对我自己来说似乎是一种非常愚蠢的方式。

Is there any smart way to do so? 有什么聪明的方法吗?

I am speaking Python, but any generic answers are also very much welcomed. 我说的是Python,但也欢迎任何一般性答案。

for i in range(0, len(A), 2):
    line1 = A[i:i+2]
    for j in range(0, len(B), 2):
        line1 = A[j:j+2]
        point_of_intersect = intersection(line1, line2)
        if point_of_intersect:
            print point_of_intersect

where the function intersection is defined according to this Wikipedia entry. 根据该Wikipedia条目定义函数intersection位置

Having 2 points X(x1, x2), Y(y1, y2) , you can determine the line equation such as: 具有2个点X(x1, x2), Y(y1, y2) ,您可以确定线方程,例如:

(x-x1)/(x2-x1) = (y-y1)/(y2-y1)

Do this for line A and line B. 对A行和B行执行此操作。

You will obtain for line 您将获得线

A: y = m1*x+n1
B: y = m2*x+n2

Now, you just need to find the value of y and x which respects both equations above. 现在,您只需要找到遵守上述两个方程的y和x值即可。

If your lines have very few segments, then testing all possible segment pairs is practical, but it doesn't scale very well. 如果您的线段很少,那么测试所有可能的线段对是可行的,但是扩展性不是很好。

The most common solution to this problem is the Bentley-Ottman algorithm. 解决此问题的最常见方法是Bentley-Ottman算法。 It's easy to search the internet for pseudo-code or even libraries which implement it; 在互联网上搜索伪代码甚至是实现它的库都很容易; it's a simple sweep-line algorithm whose execution time is O( (n + k) log n) where n is the number of line segments (the sum of the segments in both lines) and k is the number of intersections. 这是一种简单的扫掠线算法,其执行时间为O( (n + k) log n) ,其中n是线段数(两条线段之和), k是交点数。 If you only want to know if there are any intersections, you can stop at the first intersection found, at which point the algorithm reduces to O(n log n) . 如果只想知道是否有任何路口,则可以在找到的第一个路口处停止,此时算法将减少为O(n log n)

This assumes that you know that A and B do not contain internal crossings. 假定您知道AB不包含内部交叉。 A naive implementation of Bentley-Ottman (where the two sets of line segments are just processed indiscriminately) will report all crossings, including self-crossings. Bentley-Ottman的一个简单的实现(其中两组线段被不加区别地处理)将报告所有交叉点,包括自交叉点。

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

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