Maze Tree
bl.ocks.org
bl.ocks.org
d3.selectAll(nodes).transition()
.duration(2500)
.delay(function() { return this.depth * 50; })
.ease("quad-in-out")
.tween("position", function() {
var d = this, i = d3.interpolate([d[0], d[1]], [d.y, d.x]);
return function(t) { var p = i(t); d.t = t; d[0] = p[0]; d[1] = p[1]; };
});
The tree layout auto-magically gives every node a depth property, we delay the transition by an amount proportional to the depth, and then over the course of two-and-a-half seconds tween the line segment into the new position. Simple and effective. The hard part is generating the maze.I wonder if this works backwards - given a tree could you construct a maze? efficiently?
http://bl.ocks.org/mbostock/11357811
I made the paths thin (1px instead of 4px) to make it easier to see the tree on top of the maze. But to avoid the path/wall ambiguity, maybe it’d be better to start the paths 4px wide, transition to 1px, and then transition to the tree layout…
Your idea sounds fantastic - the problem really was with how I perceived the maze (black paths, white walls)
It would be great if you could add more comments to the code, or a simple separate guide for each of the projects. They are all amazing, and I'm sure many would want to learn more.
I guess making a guide for every Gist Mike posts would be quite demanding regarding his output, in the end even his day only 24 has hours.
There are trees for which no rectangular maze exists - for example, any tree with a vertex of degree greater than 4. Or the tree with 4 vertices arranged in a T, which can't be fit into a 2x2 space. If there is such an algorithm, it'll have to account for the possibility that there is no solution, so I wouldn't expect it to be very efficient.
If you want to write D3 code bost.ocks.org is an incredible resource. It has a broader set of example code than any other library I have seen, bar none.
[1] http://www.aha.io/assets/screenshots/benefits/product-planni...
[2] http://www.aha.io/assets/screenshots/benefits/product-roadma...
I couldn't see how the tree could possibly handle loops.
The algorithm uses "loop-erased" random walks.
It starts at a node that isn't in the tree and randomly walks.
If it hits itself while randomly walking (remember it isn't part of the tree yet), it erases the loop that it created and continues walking from that node again.
If the walk hits the tree, connect the walked path to the tree, pick another node and start again.
The same code that generates the walls inside the maze must also generate the walls on the edge of the maze - you can't just put a solid border and declare the corners the start and end.
(Well you can, but then there's not a single path.)
http://bl.ocks.org/mbostock/11167589
Amazing examples all over that site.