简体   繁体   English

点是图形内部还是外部(顶点和边缘)?

[英]A point is inside or outside a graph (vertices and edges)?

I have a graph G composed of edges {E} and vertices {V} . 我有一个由边{E}和顶点{V}组成的图G The vertices in {V} are expressed in 2-D coordinates. {V}中的顶点以2-D坐标表示。 The graph is planar, it means that no two edges intersect. 图形是平面的,这意味着没有两条边相交。

Graph G has some loops, and let's say a point is inside the graph if it falls in one of the loops of G . G具有一定的循环,比方说,一个点是在图表上,如果它落入的环路的一个G A loop example may be A---B---C---A , where A , B and C are vertices and --- is an edge. 循环示例可以是A---B---C---A ,其中ABC是顶点, ---是边。

Now given a point (x, y) , how can I determine whether it is is inside the graph or outside ? 现在给出一个点(x, y) ,我如何确定它是在图形内部还是外部 What is the best way or the simplest way of doing so? 最简单的方法或最简单的方法是什么?

I am using Python, if that helps. 我正在使用Python,如果这有帮助的话。

UPDATE: Yes, all the edges are straight lines. 更新:是的,所有边都是直线。

@Peter de Rivaz offers a fundamental insight, albeit with no proof: the point is inside a loop iff it is inside the hull of formed by the outer vertices of the graph. @Peter de Rivaz提供了一个基本的见解,尽管没有证据:如果它位于由图的外顶点形成的船体内部,则该点位于环内。 We can prove this by proving: 我们可以通过证明:

  • Any point inside the hull is inside a loop 船体内的任何一点都在一个环路内
  • Any point outside the hull is not inside any loop 船体外的任何点都不在任何环路内

The first is easy to prove: any point inside the hull is inside a loop, because the hull itself is a loop. 第一个很容易证明:船体内的任何一点都在一个环路内,因为船体本身就是一个环路。

The second can be proved by reductio ad absurdum . 第二个可以通过reductio ad absurdum来证明。 Very informally, for a point outside outside the hull to be inside a loop, there needs to be at least a vertex outside the hull, and for it to form a loop with at least two other vertices inside the loop, such that the point is inside that same loop. 非常非正式地,对于船体外部的一个点在一个环内,需要在船体外面至少有一个顶点,并且它要在环内形成一个至少有两个其他顶点的环,这样点就是在同一个循环里面。 However, there can't be any vertices outside the loop, because, by definition, all vertices are inside it. 但是,循环外不能有任何顶点,因为根据定义,所有顶点都在其中。 Thus, by reductio ad absurdum , there are no points outside the hull that are inside any loop. 因此,通过减少荒谬 ,船体外部没有任何环路内的点。

Now that we are sure we have a correct way to test what we want, we still need an algorithm that can tell whether a point is inside the hull. 既然我们确信我们有正确的方法来测试我们想要的东西,我们仍然需要一种算法来判断一个点是否在船体内部。 This may be accomplished through a simple extension of the ray casting algorithm . 这可以通过光线投射算法的简单扩展来实现

Basically, we start with the list of all vertices, sorted by the vertical coordinate. 基本上,我们从所有顶点的列表开始,按垂直坐标排序。 Then, for each pair of successive vertices, we "create" a horizontal line that goes between them, and check what is the first and last edge that the line intersects . 然后,对于每对连续的顶点,我们“创建”它们之间的水平线,并检查线相交的第一个和最后一个边 Those two edges are part of the hull. 这两条边是船体的一部分。 If the tested point is between any of these two edges, it will be inside the hull. 如果测试点位于这两个边缘中的任何一个之间,则它将位于船体内部。

Here's a graphical representation of the first 3 iterations: 这是前3次迭代的图形表示:

迭代1迭代2迭代3

As the graph is planar you can do this by tracing the outline of each connected set of vertices and then testing to see if your point is inside any of these polygons. 由于图形是平面的,您可以通过跟踪每个连接的顶点集的轮廓然后测试以查看您的点是否位于这些多边形中的任何一个来完成此操作。

This picture illustrates the idea: 这张照片说明了这个想法:

在此输入图像描述

The red line is the polygon representing the outline of the left hand connected component, while the green line is the polygon representing the outline of the right hand component. 红线是表示左手连接组件轮廓的多边形,而绿线是表示右手组件轮廓的多边形。

Your point will be inside a loop if and only if it is inside one of the outlines. 当且仅当它在一个轮廓内时,你的观点将在一个循环中。

First, I would find the set cycles (loops) in my graph. 首先,我会在图表中找到设置周期(循环)。 For this see this SO question Finding all cycles in undirected graphs 为此,请参阅此SO问题查找无向图中的所有循环

Then compute the set of maximal cycles ie those that are not included in another cycle (maybe you can do these first two steps at the same time). 然后计算一组最大循环,即那些未包含在另一个循环中的循环(也许你可以同时执行前两个步骤)。

Once you have the maximal cycles, this is a matter of detecting wether a point in within a polygon. 一旦有了最大周期,就可以检测多边形内的一个点。 Various methods exist, eg - draw a line and the number of edge intersection - sum of angles to the vertices of the polygon from the point (0° -> outside, 360° -> inside). 存在各种方法,例如 - 绘制线和边缘交叉的数量 - 从点(0° - >外部,360° - >内部)到多边形顶点的角度之和。

See How can I determine whether a 2D Point is within a Polygon? 请参阅如何确定2D点是否在多边形内?

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

相关问题 顶点未在网络图中以绘图方式绘制的顶点 - Vertices with edges not drawn with plotly in a network graph 在图形工具中绘制图形时,仅出现顶点,没有边 - When drawing graph in graph-tool only vertices appear, no edges 在 Python 中生成具有平行标记边/顶点的有向图 - Generating Directed Graph With Parallel Labelled Edges/Vertices in Python 如何找到给定输入顶点和边的图的度序列? - How to find degree sequence of graph given input vertices and edges? 无法使用 Python 将顶点或边添加到 CosmosDB Gremlin 图 SDK - Cannot add vertices or edges to CosmosDB Gremlin graph with Python SDK 指向多边形与轴平行的多边形内部 - point inside a polygon with edges parallel to the axes 从边列表计算创建的图形数量和每个图形中的顶点数量 - Calculating the number of graphs created and the number of vertices in each graph from a list of edges 如何使用顶点和边的分类属性在图形工具中绘制颜色? - How to use categorial properties of Vertices and Edges to draw colors in graph-tool? 将顶点数组重新排列为边数组 - Rearranging array of vertices into array of edges 检查地理点是在多边形内部还是外部 - Check if geo-point is inside or outside of polygon
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM