I tried to explain how it works in the comments in the code. Probably worth reading them in detail as they do explain the algorithm used.
My thought process was something like this (over a number of days grabbing time when I could):
Emulate a human selecting pieces and clicking them together. Model pieces as straight lines of the right length. There are two important things: how long the line is and how much it modifies the angle at which the next piece will join. Thus all I need is to keep track of the position of the end of the last piece added and the angle.
Spend a while figuring out the trigonometry and getting that code working correctly. To generate the permutations I realized that the process could be split into three parts:
1. Always place the bridge. Since there's one just add it at the end.
2. Separate the straights and curves. Generate all the permutations of clockwise and anticlockwise curved pieces. To do that I used the trick of all 12-bit binary numbers between 0 and 2^12-1.
3. Then insert the straight pieces between the curved pieces in all places possible.
Feed each possible track layout into code that builds the track and check that the end pieces match up. Add code to eliminate end pieces that join impossibly (come from the wrong angle), add code to remove duplicates caused by rotation and reflection.
Once I got that working I then wrote code in Processing to draw a pretty picture.
You might be able to speed it up considerably by adding
2a. Continue with next iteration unless the turning number of the sequence of curves is a whole number.
You add 1/8 to the turning number for each clockwise curve and subtract 1/8 for each CCW (for numerical stability you would rather add of subtract 1 and check for multiples of 8).
My thought process was something like this (over a number of days grabbing time when I could):
Emulate a human selecting pieces and clicking them together. Model pieces as straight lines of the right length. There are two important things: how long the line is and how much it modifies the angle at which the next piece will join. Thus all I need is to keep track of the position of the end of the last piece added and the angle.
Spend a while figuring out the trigonometry and getting that code working correctly. To generate the permutations I realized that the process could be split into three parts:
1. Always place the bridge. Since there's one just add it at the end.
2. Separate the straights and curves. Generate all the permutations of clockwise and anticlockwise curved pieces. To do that I used the trick of all 12-bit binary numbers between 0 and 2^12-1.
3. Then insert the straight pieces between the curved pieces in all places possible.
Feed each possible track layout into code that builds the track and check that the end pieces match up. Add code to eliminate end pieces that join impossibly (come from the wrong angle), add code to remove duplicates caused by rotation and reflection.
Once I got that working I then wrote code in Processing to draw a pretty picture.