Search code examples
delphigeometrycomputational-geometry

Calculating the neighborhood distance


What method would you use to compute a distance that represents the number of "jumps" one has to do to go from one area to another area in a given 2D map?

Let's take the following map for instance:

Map
(source: free.fr)

The end result of the computation would be a triangle like this:

  A B C D E F
A 
B 1
C 2 1
D 2 1 1
E . . . .
F 3 2 2 1 .

Which means that going from A to D, it takes 2 jumps.
However, to go from anywhere to E, it's impossible because the "gap" is too big, and so the value is "infinite", represented here as a dot for simplification.

As you can see on the example, the polygons may share points, but most often they are simply close together and so a maximum gap should be allowed to consider two polygons to be adjacent.

This, obviously, is a simplified example, but in the real case I'm faced with about 60000 polygons and am only interested by jump values up to 4.

As input data, I have the polygon vertices as an array of coordinates, from which I already know how to calculate the centroid.

My initial approach would be to "paint" the polygons on a white background canvas, each with their own color and then walk the line between two candidate polygons centroid. Counting the colors I encounter could give me the number of jumps.
However, this is not really reliable as it does not take into account concave arrangements where one has to walk around the "notch" to go from one polygon to the other as can be seen when going from A to F.

I have tried looking for reference material on this subject but could not find any because I have a hard time figuring what the proper terms are for describing this kind of problem.

My target language is Delphi XE2, but any example would be most welcome.


Solution

  • You can create inflated polygon with small offset for every initial polygon, then check for intersection with neighbouring (inflated) polygons. Offseting is useful to compensate small gaps between polygons.

    Both inflating and intersection problems might be solved with Clipper library.
    Solution of the potential neighbours problem depends on real conditions - for example, simple method - divide plane to square cells, and check for neighbours that have vertices in the same cell and in the nearest cells.

    Every pair of intersecting polygons gives an edge in (unweighted, undirected) graph. You want to find all the path with length <=4 - just execute depth-limited BFS from every vertice (polygon) - assuming that graph is sparse