Public
Edited
Feb 13, 2023
Importers
20 stars
Hello, PGLiteINSEE ParquetHello, apcachDruidJS workerHello, OrbitWord Tour: 40k words and their friendsHello, spectral.jsHello, petite-vueHello, @thi.ng/grid-iteratorsHello, thumbhashHello, SwissGLHello, QOI (and glitches)Hello, orbHello, cosmographHello, TabulatorUsing d3.blur with PlotMath.cbrtHello debounceColorcetHello, gliiHello, Open MeteoHello, PyWaveletsHello, typesenseHello, libgifHello, kmeans-engineHappy anniversary, Project Gutenberg!Hello, fflateHello, ArchieML!Hello, d3-bboxCollideHello, jsgeoda!Hello, EDTF!Hello, protovis!Hello, placekeyHello, fuse.jsHello, Reorder.jsHello, shadow DOMjszipHello, procedural-glHello, qhullHello, genetic-jsDruidJSHello, Tippy.jsHello, paintWorkletBig πHello, AutoencoderHello, Shakespearean UUIDsHello, ccwt.jsHello, constrainautorHello, talismanHello, polygon-offsetHello p-queueHello async-poolHello rollup-plugin-asciiHello, algebra.jsHello, pixi.jsHello, d3-renderHello zip/unzipCumulative Wikipedia DonationsHTML <details>regl textureHello, npyjsHello protobufHello, pencil touchHello, LOESSHello html2canvaslegra mapscolor2cssHello, ecsy
2D point distributions
Hello, delatinThe gpu.js loopDijkstra’s shortest-path treeHello nojacko/Dijkstras-jsHello, tcort/dijkstrajsHello, lambdabaa/dijkstraHello, gpu.js v2Hello jsqrHello qrcodeHello SharedArrayBufferHello GamePad APIHello vtk.jsHello nd4jsHello BiofabricTravelling with a self-organizing mapHello glitchHello UMAP-jsHello pandemoniumHello iocaneHello JSON-editorHello d3-griddingHello mljs/knnWorkerHello lalolibImage to GPU.jsImage to blink.jsTissot's indicatrixVega projectionsHello WebCLGLUsing d3-inertia with observableVideo contouring 3ngraph: pagerank, louvain…Union-FindPerceptron (simple statistics)mljsHello h3-jsEmoji FlagsHello, poisson-disk-sampling
Also listed in…
Algorithms
Insert cell
Insert cell
Insert cell
N = 4000
Insert cell
pick2d(width, height, N, "random")
Insert cell
Insert cell
{
await visibility();
const context = DOM.context2d(width, height);
yield context.canvas;

let i = 0;
const radius = Math.sqrt((0.63 * (width * height)) / N);
for (const p of random2d(width, height, N)) {
context.beginPath();
context.arc(p[0], p[1], radius / 2, 0, 2 * Math.PI);
context.stroke();
if (i++ % 20 === 0) yield context.canvas;
}
}
Insert cell
random2d(width, height, N)
Insert cell
function random2d(width, height, N) {
const points = [];
for (let i = 0; i < N; i++) points.push([width * rng(), height * rng()]);
return points;
}
Insert cell
Insert cell
Insert cell
function poissonDisc2d(width, height, N) {
const radius = Math.sqrt((0.63 * (width * height)) / N);
return DiscSampler(width, height, radius);
}
Insert cell
// Code by Jason Davies, Mike Bostock, Martin Roberts, and Jacob Rus
// see https://observablehq.com/@fil/poisson-distribution-generators for details
function* DiscSampler(width, height, radius) {
const k = 13; // maximum number of samples before rejection
const m = 4; // a number mutually prime to k
const radius2 = radius * radius;
const cellSize = 1 / (radius * Math.SQRT1_2);
const gridWidth = Math.ceil(width * cellSize) + 4; // pad grid by 2 on each side
const gridHeight = Math.ceil(height * cellSize) + 4;
const grid = new Float64Array(2 * gridWidth * gridHeight).fill(Infinity);
const queue = [];
const rotx = Math.cos((2 * Math.PI * m) / k);
const roty = Math.sin((2 * Math.PI * m) / k);

// Pick the first sample.
yield sample(0.1 * width * (4.5 + rng()), 0.1 * height * (4.5 + rng()), null);

// Pick a random existing sample from the queue.
pick: while (queue.length) {
const i = (rng() * queue.length) | 0;
const parent = queue[i];
const t = tanpi_2(2 * rng() - 1),
q = 1 / (1 + t * t);
const epsilon = 1e-6;
let dw, dx, dy;
dx = q ? (1 - t * t) * q : -1; // [dx, dy] = random unit vector
dy = q ? 2 * t * q : 0;

// Make a new candidate.
for (let j = 0; j < k; ++j) {
dw = dx * rotx - dy * roty; // temporary name for dx
dy = dx * roty + dy * rotx;
dx = dw;
const rand0 = rng();
const rand1 = rng();
const r = (radius + epsilon) * (1 + 0.65 * rand0 * rand1);
const x = parent[0] + r * dx;
const y = parent[1] + r * dy;

// Accept candidates that are inside the allowed extent
// and farther than 2 * radius to all existing samples.
if ((0 <= x) & (x < width) & (0 <= y) & (y < height) & far(x, y)) {
yield sample(x, y);
continue pick;
}
}

// If none of k candidates were accepted, remove it from the queue.
const r = queue.pop();
if (i < queue.length) queue[i] = r;
// yield {remove: parent};
}

// fast approximation of tan(πx/2)
function tanpi_2(a) {
let b = 1 - a * a;
return a * (-0.0187108 * b + 0.31583526 + 1.27365776 / b);
}

function far(x, y) {
const j0 = (y * cellSize) | 0;
const i0 = (x * cellSize) | 0;
for (let j = j0; j < j0 + 5; ++j) {
const index0 = 2 * (j * gridWidth + i0);
for (let i = index0; i < index0 + 10; i += 2) {
const dx = grid[i] - x;
const dy = grid[i + 1] - y;
if (dx * dx + dy * dy < radius2) return false;
}
}
return true;
}

function sample(x, y, parent) {
const s = [x, y];
// offset grid cell by 2 in each direction to account for border
const i = (x * cellSize + 2) | 0;
const j = (y * cellSize + 2) | 0;
const index = 2 * (gridWidth * j + i);
grid[index] = x;
grid[index + 1] = y;
queue.push(s);
return s;
}
}
Insert cell
// keep for ascending compatibility
function poissonDiscSampler(width, height, radius) {
return Array.from(poissonDisc2d(width, height, N));
}
Insert cell
Insert cell
{
await visibility();
const context = DOM.context2d(width, height);
yield context.canvas;

let i = 0;
const radius = Math.sqrt((width * height) / N / 2.5);
for (const p of hexgrid2d(width, height, N)) {
context.beginPath();
context.arc(p[0], p[1], radius / 2, 0, 2 * Math.PI);
context.stroke();
if (i++ % 20 === 0) yield context.canvas;
}
}
Insert cell
function hexgrid2d(width, height, N) {
const h = Math.sqrt((width * height * (Math.sqrt(5) / 2)) / N),
v = h * (2 / Math.sqrt(5)),
a = [];
for (let i = 0; i < width / h; i++)
for (let j = 0.5; j < height / v; j++)
a.push([(i - (j % 2) / 2) * h, j * v]);
return a;
}
Insert cell
Insert cell
{
await visibility();
const context = DOM.context2d(width, height);
yield context.canvas;

let i = 0;
const radius = Math.sqrt((0.3 * (width * height)) / N);
for (const p of blue2d(width, height, N)) {
context.beginPath();
context.arc(p[0], p[1], radius / 2, 0, 2 * Math.PI);
context.stroke();
if (i++ % 20 === 0) yield context.canvas;
}
}
Insert cell
Insert cell
{
await visibility();
const context = DOM.context2d(width, height);
yield context.canvas;

let i = 0;
const radius = Math.sqrt((0.3 * (width * height)) / N);
for (const p of pseudoblue2d(width, height, N)) {
context.beginPath();
context.arc(p[0], p[1], radius / 2, 0, 2 * Math.PI);
context.stroke();
if (i++ % 20 === 0) yield context.canvas;
}
}
Insert cell
function pseudoblue2d(width, height, N) {
const a = [];
const r = N / (width * height);
for (let i = 0; i < width; i++)
for (let j = 0.5; j < height; j++) if (pseudoblue(i, j) < r) a.push([i, j]);
return a;
}
Insert cell
function blue2d(width, height, N) {
const a = [];
const r = N / (width * height);
for (let i = 0; i < width; i++)
for (let j = 0.5; j < height; j++) if (blue(i, j) < r) a.push([i, j]);
return a;
}
Insert cell
import { sorted_1024 as blue, pseudoblue } from "@fil/blue"
Insert cell
Insert cell
{
await visibility();
const context = DOM.context2d(width, height);
yield context.canvas;

let i = 0;
const radius = Math.sqrt((width * height) / N / 2.5);
for (const p of grid2d(width, height, N)) {
context.beginPath();
context.arc(p[0], p[1], radius / 2, 0, 2 * Math.PI);
context.stroke();
if (i++ % 20 === 0) yield context.canvas;
}
}
Insert cell
function grid2d(width, height, N) {
const radius = Math.sqrt((width * height) / N);
const points = [];
for (let i = 0; i < width / radius; i++)
for (let j = 0; j < height / radius; j++)
points.push([i * radius, j * radius]);
return points;
}
Insert cell
Insert cell
{
await visibility();
const context = DOM.context2d(width, height);
yield context.canvas;

let i = 0;
const radius = Math.sqrt((width * height) / N / 2.5);
for (const p of normal2d(width, height, N)) {
context.beginPath();
context.arc(p[0], p[1], radius / 2, 0, 2 * Math.PI);
context.stroke();
if (i++ % 20 === 0) yield context.canvas;
}
}
Insert cell
function normal2d(width, height, N) {
const random = d3.randomNormal(0, Math.min(width, height) / 6);
const points = [];
for (let i = 0; i < N; i++)
points.push([width / 2 + random(), height / 2 + random()]);
return points;
}
Insert cell
Insert cell
function pick2d(width, height, N, type) {
switch (type) {
case "poisson":
case "poissonDisc":
return poissonDisc2d(width, height, N);
case "pseudoblue":
return pseudoblue2d(width, height, N);
case "hex":
case "hexagons":
case "hexgrid":
return hexgrid2d(width, height, N);
case "square":
case "grid":
return grid2d(width, height, N);
case "normal":
return normal2d(width, height, N);
case "random":
case "uniform":
default:
return random2d(width, height, N);
}
}
Insert cell
rng = Math.random
Insert cell
height = 500
Insert cell
// adapt this code for your application
viewof distributionType = Inputs.select([
"poisson",
"pseudoblue",
"hex",
"grid",
"random",
"normal"
])
Insert cell
pick2d(width, height, N, distributionType)
Insert cell
d3 = require("d3-random@3")
Insert cell
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