简体   繁体   中英

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

I have a graph G composed of edges {E} and vertices {V} . The vertices in {V} are expressed in 2-D coordinates. 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 . A loop example may be A---B---C---A , where A , B and C are vertices and --- is an edge.

Now given a point (x, y) , how can I determine whether it is is inside the graph or outside ? What is the best way or the simplest way of doing so?

I am using Python, if that helps.

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. 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 . 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:

迭代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

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).

See How can I determine whether a 2D Point is within a Polygon?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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