Published
Edited
Nov 12, 2019
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
dc(tree, pts)
Insert cell
function dc(tree, pts) {
const depth = new Int32Array(pts.length).fill(-1),
colorized = new Float32Array(pts.length).fill(NaN);

let recursion_count = 0;

function traverse(i) {
const j = tree.predecessor[i];
if (j < 0) {
colorized[i] = depth[i] = 0; // root
return;
} else if (depth[j] < 0) {
traverse(j); // recurse
recursion_count++;
}
depth[i] = depth[j] + 1;
colorized[i] =
colorized[j] +
Math.atan2(pts[i][1] - pts[j][1], pts[i][0] - pts[j][0]) / 2 ** depth[i];
}

for (let i = 0; i < colorized.length; i++) traverse(i);

return { depth, colorized, recursion_count };
}
Insert cell
Insert cell
Insert cell
src = "https://gist.githubusercontent.com/Fil/450db51a15fdc8bd34a12cb8a9961976/raw/680961da57d2b11a0e3e6993e5f79bd035729831"
Insert cell
nodes = d3.csv(`${src}/node_list.csv`, d3.autoType)
Insert cell
edges = d3.csv(`${src}/edge_list.csv`, d3.autoType)
Insert cell
Insert cell
nodesIndex = new Map(nodes.map((d, i) => [d.osmid, i]))
Insert cell
function createGraph(nodesIndex, edges) {
const graph = { sources: [], targets: [], costs: [] };
edges.forEach(({ u, v, length }) => {
const i = nodesIndex.get(u),
j = nodesIndex.get(v),
cost = length;
graph.sources.push(i);
graph.targets.push(j);
graph.costs.push(cost);

graph.sources.push(j);
graph.targets.push(i);
graph.costs.push(cost);
});

return graph;
}
Insert cell
graph = createGraph(nodesIndex, edges)
Insert cell
import { shortest_tree } from "@fil/dijkstra"
Insert cell
origins = Array.from({ length: 1 }, () => (Math.random() * nodes.length) | 0)
Insert cell
tree = shortest_tree({ graph, origins })
Insert cell
Insert cell
pts = nodes.map(d => projection([d.x, d.y]))
Insert cell
delaunay = d3.Delaunay.from(pts)
Insert cell
projection = d3.geoMercator().fitExtent([[10, 10], [width - 10, height - 10]], {
type: "MultiPoint",
coordinates: nodes.map(d => [d.x, d.y])
})
Insert cell
Insert cell
color = d3.scaleOrdinal(d3.schemeCategory10)
Insert cell
Insert cell
d3 = require("d3@5", "d3-delaunay@5")
Insert cell
height = width * 0.9
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more