如何找到两个线段的交点? [英] How do I find the intersection of two line segments?

查看:65
本文介绍了如何找到两个线段的交点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有两个有限的线段,每个线段由两个点(在两个空间中)定义.我想找到一种方法来获得这两条线的交点.最终,我想将其扩展到适用于连接的线段集.

Suppose we have two finite line segments defined each by two points (in two space). I would like to find a way to get the intersection point of those two lines. Eventually, I would like to extend this to work on sets of connected line segments.

我在这里找到了一个很好的解决方案: Python-matplotlib:找到线图的交集.但是,这依赖于scipy,我认为这需要BLAS,出于不同的原因,我想避免使用BLAS.

I have found a good solution here: Python - matplotlib: find intersection of lineplots. However, this relies on scipy, which I believe requires BLAS, which for separate reasons I would like to avoid.

matplotlib 有一个名为 Path 的模块,它有一个 intersects_path() 函数(http://matplotlib.org/api/path_api.html#matplotlib.path.Path.intersects_path) 返回 true 或 false 是否存在交叉点,但不是我需要的特定位置.

matplotlib has a module called Path, which has an intersects_path() function (http://matplotlib.org/api/path_api.html#matplotlib.path.Path.intersects_path) which returns true or false for the existence of an intersection, but not the specific location, which I require.

有没有人知道一个干净的方法?

Does anyone know of a clean approach to this?

我想出的任何解决方案都是冗长的,如果已经存在解决方案,我真的不希望重新发明轮子.

Any solution I am coming up with is lengthy, and if a solution already exists I would really prefer not to re-invent the wheel.

谢谢!

推荐答案

为了完整起见,我想我会发布最终使用的解决方案.

For the sake of completion, I thought I would post the final solution which I used.

使用Shapely( https://pypi.python.org/pypi/Shapely ),代码看起来很简单这样:

Using Shapely (https://pypi.python.org/pypi/Shapely) the code can look as simple as this:

from shapely.geometry import LineString

line1 = LineString([(0,0), (1,0), (1,1)])
line2 = LineString([(0,1), (1,1)])

print(line1.intersection(line2))

返回:

POINT (1 1)

这样做的好处是,它可以无缝处理单点相交和线段相交,并且可以将相同的技术应用于更复杂的对象.

The nice thing about this is that it will handle single point intersection, and intersection of segments seamlessly, and the same technique can be applied to much more complicated objects.

这篇关于如何找到两个线段的交点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆