Published
Edited
Apr 5, 2022
50 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// https://observablehq.com/@fil/chord-area-inverse
chordAreaInverse = x => Math.pow(x, 0.6) - Math.pow(1 - x, 0.6)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function discUniformTransport1(points, radius) {
const n = points.length / 2,
indices = d3.range(n);

const a = 2 * Math.PI * Math.random(),
sa = Math.sin(a),
ca = Math.cos(a);

const projection = Array.from({ length: indices.length });
for (let i = 0; i < projection.length; i++)
projection[i] = ca * points[2 * i] + sa * points[2 * i + 1];

indices.sort((i, j) => projection[i] - projection[j]);

for (let k = 0; k < n; k++) {
const i = indices[k],
ideal = radius * chordAreaInverse((k + 1) / (n + 1)),
delta = ideal - projection[i];
points[2 * i] += ca * delta;
points[2 * i + 1] += sa * delta;
}
}
Insert cell
discUniformTransportBatched = {
let indices = [],
projection,
deltas;

// batched
return (
points,
radius,
{ dirs = 3, strength = 1, profile = chordAreaInverse } = {}
) => {
const n = points.length / 2;
if (n !== indices.length) {
indices = Uint32Array.from(d3.range(n));
projection = new Float32Array(n);
deltas = new Float32Array(2 * n);
}

const a = 2 * Math.PI * Math.random();
deltas.fill(0);
for (let d = 0; d < dirs; d++) {
const ap = a + (Math.PI * d) / dirs,
sa = Math.sin(ap),
ca = Math.cos(ap);
for (let i = 0; i < n; i++)
projection[i] = ca * points[2 * i] + sa * points[2 * i + 1];

indices.sort((i, j) => projection[i] - projection[j]);

for (let k = 0; k < n; k++) {
const i = indices[k],
ideal = radius * profile(k / (n + 1)),
delta = ideal - projection[i];
deltas[2 * i] += ca * delta;
deltas[2 * i + 1] += sa * delta;
}
}
for (let i = 0; i < points.length; i++)
points[i] += (deltas[i] / dirs) * strength;
};
}
Insert cell
function rectangleUniformTransport(points, x, y, { strength = 1 } = {}) {
const n = points.length / 2,
indices = d3.range(n);

const projection = Array.from({ length: indices.length });

// horizontal
for (let i = 0; i < projection.length; i++) projection[i] = points[2 * i];
indices.sort((i, j) => projection[i] - projection[j]);

for (let k = 0; k < n; k++) {
const i = indices[k],
ideal = x * ((k + 1) / (n + 1)),
delta = ideal - projection[i];
points[2 * i] += delta * strength;
}

// vertical
for (let i = 0; i < projection.length; i++) projection[i] = points[2 * i + 1];
indices.sort((i, j) => projection[i] - projection[j]);

for (let k = 0; k < n; k++) {
const i = indices[k],
ideal = y * ((k + 1) / (n + 1)),
delta = ideal - projection[i];
points[2 * i + 1] += delta * strength;
}
}
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more