I have two rectangles and I'm trying to find a visually pleasing route between them that is purely rectilinear.
To do that I would like to generate the following lattice / mesh as a graph structure (list of vertices connected to other vertices) so I can run a pathfinding algorithm like Djikstra's on it:
This is gotten by padding each of the rectangles to get a new rectangle (red and blue above), taking the bounding box of these new rectangles (green). Then casting rays from the center of each side of the rectangles and subdividing the lines that these touch.
However I'm having trouble working out how to generate this lattice as a graph structure in an efficient manner.
Ideally I'm looking for an algorithm and a data structure that allows me to build the graph line segment by line segment, subdividing existing line segments (edges) where new line segments intersect them.
I found a solution by tackling this a different way. Instead of thinking in lines instead I gathered all the points from both the start and end of my lines and the points of their intersections.
I then added each unique point as a node to the graph.
Then I made a set for each of all the unique X values in all my nodes and another for the Y values.
I sorted these sets in ascending order.
I then iterated through each possible combination of X and Y values (a small search space because all the lines that created the intersections are orthogonal).
When I found a node, I iterated backwards in the X values until I hit another node and linked them and did the same for the Y values.
Here's my resulting code: