searchdata-structuresrecursive-datastructureskdtree

Given a kd tree-like grid, how to find two nearest intersecting lines


Say I have a grid structure like the image here:

enter image description here

Give point D, I want to find the nearest points whose line intersects with the line created by D.

I need a way to find C and E, but not A or B.

A is not a candidate because it runs parallel to D.

B is not a candidate because it does not intersect with D.

I'm trying to find the best data structure to store this data, as well as the best way to design an algorithm to support this search. I've looked at KD Trees, but I need to be able to specify the line dimension for each point.

I need to have a data structure also needs to work for an arbitrary combination of vertical and horizontal segments, such as this:

enter image description here

In this example, if we where searching A, we would return just C.

And finally:

enter image description here

In this example, if we where searching A, we would return nothing.


Solution

  • I wound up implementing this as a directional, unweighted graph. Each vertex in the graph is a line, with an X,Y coordinate, and a direction.

    A vertex is connected to it's parent via a directional relationship. Using the X,Y coordinates, and the heirarchy of the graph, we can recreate the layout exactly.

    enter image description here