Published
Edited
Nov 10, 2020
4 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
fill = d3
.scaleDiverging()
.domain([.5, 0, -.5])
.interpolator(d3.interpolatePiYG)
Insert cell
mutable debug = null
Insert cell
cells = {
const uniform =
Math.abs(
d3.polygonArea([
[3 + width / 2, 3],
[width - 3, 3],
[width - 3, height - 3],
[3, height - 3]
])
) / points.length;

for (let t = 0; t < 2000; t++) {
const cells = powerDiagram(points);

for (let i = 0; i < cells.length; i++) {
const c = cells[i];
c.area = Math.abs(d3.polygonArea(c));
}

const minA = cells.length === points.length;

const A = d3.median(points, d => d[2]);

for (let i = 0; i < points.length; i++) points[i][2] += 1000;

for (let i = 0; i < cells.length; i++) {
const c = cells[i],
p = c.site.originalObject;

p.cell = c;
p[2] = (p[2] - 1000 - A) * 0.5;

p.stress = Math.log(c.area / uniform);

p[2] -= 400 * p.stress;

// advection?

const [x, y] = d3.polygonCentroid([...c]);
if (isFinite(x) && isFinite(y)) {
p.centroid = [x, y];
const dx = x - p[0],
dy = y - p[1];
p[0] += dx * 0.6;
p[1] += dy * 0.6;
//if (minA === 0) p[2] = 0;
mutable debug = { t, points, centroid: [x, y] };
} else {
mutable debug = p;
}

//p[0] += (Math.random() - Math.random()) * 1;
//p[1] += (Math.random() - Math.random()) * 1;
}

chart.plot();

yield;

if (!running) return yield d3.polygonArea(cells[9]);
}
}
Insert cell
height = 500
Insert cell
random = Math.random
Insert cell
points = (reset,
[].concat(
Array.from({ length: 100 }, () => [
30 + (width - 60) * random(),
30 + (height - 60) * random(),
1 //random()
])
))
Insert cell
powerDiagram(points)
Insert cell
voro = d3
.weightedVoronoi()
.x(d => d[0])
.y(d => d[1])
.weight(d => d[2])
.clip([
[3 + width / 2, 3],
[width - 3, 3],
[width - 3, height - 3],
[3, height - 3]
])
Insert cell
powerDiagram = points => voro(points)
Insert cell
d3 = require("d3@6", "d3-weighted-voronoi@1")
Insert cell
import { checkbox } from "@jashkenas/inputs"
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