Posted on Jul 7
The original can be found here.
The algorithm is quite simple yet the eventual city-like structure is amazing. At the beginning it just plants some random seeds on a canvas from which cracks grow. If a crack reaches the canvas border or touches another crack, a new crack perpendicularly branches out from an existing crack.
The script can be tested on the demo page and you can see the rendering in motion.
index.html: HTML file to display rendering.
I made a derivative work that simply change the branch angle from 90 degrees to about 45 degrees.
In this variation, cracks slightly change their angle of moving direction along the way.
maxnum: The maximum number of cracks. An instance of crack is spawned by a function
maxpal: The maximum number of colors taken from the
cgrid: An array linearized in such a way that a point (x,y) is translated into an index (dimx*y + x) in the array. An element corresponding to a (x, y) position contains an integral value of the angle (0-360) between the x-axis and the direction in which a crack on that point grows.
makeCrackfunction: Spawns an instance of crack if there are less than
maxnuminstances. A crack stops growing when encountering another crack or the canvas boundary and at the same time it spawn a new crack with this function.
Crackconstructor: Creates an instance of crack. The initial position of a crack is determined by the
Crack.findStartmethod: As suggested by the name, it looks for a pixel of an existing crack or a "seed" pixel planted on the canvas and then employ the position of the pixel as a starting point of the crack instance.
Crack.movemethod: Advances the x-y position of the crack instance. If the crack is found to be on another crack or out of bounds, it repositions itself by
findStartand calls the
makeCrackfunction to spawn new one.
2015 My gh-pages