Threads by Code Rambler

I was inspired by the Threads series by Code Rambler, who is making these interesting path avoidance images. They resemble circuitry to me and I thought I’d try and give it a try.

What I ended up with started taking on the look of a maze. There’s a base canvas width and height that is then divided into a grid of cells so that I can control the density of the paths. Then the code picks a random cell and starts drawing a line until it meets a cell that was already used. Then it would go on and on until all the cells were used up. I tied them all up with a random entrance and exit to the maze and some coloring.

I think the hardest part for this was the recursive lookups to see if an adjacent cell was free. For every point on a path, it will look all around itself: north, north-east, east, south-east, south, south-west, west & north-west. If there was a cell in one of these directions available it will add that cell to the path and then try again. Only when it would not find any adjacent cells available would it stop the path. For a 20×20 grid that was pretty quick but once it got denser, like in the 100×100 grid, the rendering took a while.