// Generated by CoffeeScript 1.10.0 (function() { var base10_to_base7, base7_n, data, diagonal, distance, get_children, height, hex_generate_svg_path, i, link, links, make_tree, max_depth, n, node, nodes, radius, svg, tree, vis, width; width = 960; height = 500; distance = 16; max_depth = 10; radius = 8; svg = d3.select('svg').attr({ width: width, height: height }); vis = svg.append('g').attr({ transform: "translate(50, 100)" }); get_children = function() { return [ { name: '', type: '', children: [] }, { name: '', type: '', children: [] }, { name: '', type: '', children: [] }, { name: '', type: '', children: [] }, { name: '', type: '', children: [] }, { name: '', type: '', children: [] }, { name: '', type: '', children: [] } ]; }; base10_to_base7 = function(n) { var base7; base7 = []; while (n > 0) { base7.unshift(n % 7); n = Math.floor(n / 7); } return base7; }; make_tree = function(node, base10_n, base7_n) { var child, i, j, len, ref; ref = node.children; for (i = j = 0, len = ref.length; j < len; i = ++j) { child = ref[i]; if (i < parseInt(base7_n[0])) { child.type = 'full'; } else if (i === parseInt(base7_n[0]) && base7_n.length > 1 && !((base7_n.reduce(function(t, s) { return t + s; })) === base7_n[0])) { child.type = base10_n % Math.pow(7, base7_n.length) === 0 || base7_n.length === 1 ? 'full' : 'partially full'; if (!(Math.pow(7, base7_n.length) === 0)) { child.children = get_children(); make_tree(child, base10_n / 7, base7_n.slice(1)); } } else { child.type = 'empty'; } } return node; }; /* Tree construction */ n = Math.floor(Math.random() * 1000); base7_n = base10_to_base7(n); tree = { name: 'root', type: ((base7_n.reduce(function(t, s) { return t + s; })) === 1 ? 'full' : 'partially full'), children: get_children() }; data = make_tree(tree, n, ((base7_n.reduce(function(t, s) { return t + s; })) === 1 ? base7_n.slice(1) : base7_n)); svg.append('text').html(n + " → " + (base7_n.join(''))).attr({ "class": 'number', x: 40, y: 40 }); svg.selectAll('.order').data((function() { var j, ref, results; results = []; for (i = j = 0, ref = base7_n.length; 0 <= ref ? j <= ref : j >= ref; i = 0 <= ref ? ++j : --j) { results.push(i); } return results; })()).enter().append('text').text(function(d, i) { return "" + (base7_n.length - i); }).attr({ "class": 'order', x: function(d, i) { return 45 + 96 * i; }, y: 75 }); /* Tree visualization */ tree = d3.layout.tree().size([0, 0]); nodes = tree.nodes(data); links = tree.links(nodes); height = 0; nodes.forEach(function(n) { if ((n.parent != null) && n.parent.children[0] !== n) { height += distance; } n.x = height; return n.y = n.depth * (width / max_depth); }); diagonal = d3.svg.diagonal().projection(function(d) { return [d.y, d.x]; }); link = vis.selectAll('path.link').data(links).enter().append('path').attr({ "class": 'link', d: diagonal }); node = vis.selectAll('g.node').data(nodes).enter().append('g').attr({ "class": 'node', transform: function(d) { return "translate(" + d.y + "," + d.x + ")"; } }); hex_generate_svg_path = function(scale) { var a, r; a = scale / 2; r = a / Math.sin(Math.PI / 3); return "M" + r + " 0 L" + (r / 2) + " " + a + " L" + (-r / 2) + " " + a + " L" + (-r) + " 0 L" + (-r / 2) + " " + (-a) + " L" + (r / 2) + " " + (-a) + " Z"; }; node.append('path').attr({ "class": 'hex', d: function(d) { return hex_generate_svg_path(12); }, fill: function(d) { if (d.type === 'full') { return 'steelblue'; } else if (d.type === 'empty') { return 'white'; } else { return 'lightsteelblue'; } } }).append('title').text(function(d) { return d.type; }); }).call(this);