Skip to content

Latest commit

 

History

History
201 lines (146 loc) · 11.4 KB

points.mdx

File metadata and controls

201 lines (146 loc) · 11.4 KB
title Points
description Even distributions of points, their refinement, and the cells around them.

A distribution is a material of loose points. scatter makes one from a density field. relax and settle improve it. voronoi reads the cells around a set of points. All of them return material, so the rest of the vocabulary applies.

t.scatter()

/_sig/Toolkit.scatter.2.mdx /_sig/Toolkit.scatter.3.mdx /_sig/Toolkit.scatter.4.mdx

Poisson-disk points over the drawable, at spacing where the field is 1 and further apart where it is lower. Where the field is 0, there are no points. Each point carries a density column with the field's value. With no field, the points are even. Give an area first, t.scatter(area, { spacing }), as t.throw takes one, or give it as within. Each call of a point word draws new values. Two t.scatter calls make two different clouds, and the same seed makes the same two clouds again.

import { sketch, circle, distance } from 'occlude';

export default sketch({ aspect: [1, 1], seed: 2 }, (t) => {
  const tone = (x, y) => Math.max(0, 1 - distance([x, y], [50, 50]) / 45);
  return t.scatter(tone, { spacing: 3 }).points.map((p) => circle(p, 0.4 + p.density * 1.2));
});

t.throw()

/_sig/Toolkit.throw.mdx

Independent random points, count of them. Give an area and the points land inside it. Give a field and each point stays with the chance the field gives at that place. A field of 0 has no points. This is the random counterpart of t.scatter. Nothing keeps the points apart, and two can land on one spot. It writes no column.

import { sketch, circle } from 'occlude';

export default sketch({ aspect: [1, 1], seed: 5 }, (t) => {
  const dish = circle(50, 50, 42);
  const shade = (x) => Math.pow(x / 100, 2);
  return [dish, t.throw(shade, { count: 900, within: dish }).points.map((p) => circle(p, 0.7))];
});

:::note attempts is the termination rule. A field that is nearly 0 everywhere makes most throws miss. After that many misses for one point, the throw returns the points it has. :::

t.relax()

/_sig/Toolkit.relax.mdx

Lloyd relaxation: each point moves to the centroid of its Voronoi cell, weighted by density if you give one. The count, the edges and every column stay. It writes no column. within is the area that the points relax in. Outside the area the density is zero, so each cell uses only its part in the area. t.relax keeps every point. A cloud from t.scatter or t.throw with an area keeps that area. t.relax and t.settle use it when you give no within. A point selection relaxes its members, as a material of their own. step is the cell of the density grid, and the default is the long side of the bounds divided by 256.

import { sketch, circle } from 'occlude';

export default sketch({ aspect: [1, 1], seed: 9 }, (t) => {
  const rough = t.scatter({ spacing: 8 });
  const even = t.relax(rough, { iterations: 6 });
  return [rough.points.map((p) => circle(p, 0.6)), even.points.map((p) => circle(p, 1.4, { pen: 'stabilo-88-blue' }))];
});

t.settle()

/_sig/Toolkit.settle.mdx

Weighted Linde-Buzo-Gray settling: relaxation with population control. density is the demand at a place, 0 to 1. spacing is the distance between points in a full-demand area. A point whose cell holds more than one point's share splits in two. settle removes a point whose cell holds almost nothing. The count converges to what the density can support. Survivors keep their columns, and each one gets a demand column, where 1 is a full share. step is the cell of the density grid, as in t.relax.

import { sketch, circle } from 'occlude';

export default sketch({ aspect: [1, 1], seed: 4 }, (t) => {
  const tone = (x, y) => Math.max(0, 1 - Math.hypot(x - 54, (y - 48) * 1.4) / 44);
  const settled = t.settle(t.scatter({ spacing: 5 }), { density: tone, spacing: 3, iterations: 12 });
  return settled.points.map((p) => circle(p, 0.5 + p.demand * 0.6));
});

Each settled point has source: the input point that it comes from. A survivor comes from its own input point. The two children of a split come from the same input point, because a child copies its parent. So source is one point of the value that you gave, with the columns of that point. A point selection gives points of its material. Below, a blue line joins each settled point to its source. A fan shows the splits of one point.

import { sketch, circle, line } from 'occlude';

export default sketch({ aspect: [1, 1], seed: 4 }, (t) => {
  const tone = (x, y) => Math.max(0, 1 - Math.hypot(x - 55, y - 50) / 42);
  const rough = t.scatter({ spacing: 9 });
  const settled = t.settle(rough, { density: tone, spacing: 4, iterations: 6 });
  return [
    rough.points.map((p) => circle(p, 0.8)),
    settled.points.map((p) => [line(p.source, p, { pen: 'stabilo-88-blue' }), circle(p, 0.4, { pen: 'stabilo-88-blue' })]),
  ];
});

Snap to a field

To move each point to the best place within reach, try places around it and move it by the best offset: m.move((p) => best(p)). A spiral of offsets over a disc looks around evenly. The point's own place is one of the offers, so a point at the best place stays. Edges, columns and row order stay. To seek the least, negate the field.

import { sketch, circle, distance } from 'occlude';

export default sketch({ aspect: [1, 1], seed: 7 }, (t) => {
  const ring = (x, y) => -Math.abs(distance([x, y], [50, 50]) - 30);
  // 48 offsets on a golden-angle spiral over a disc of radius 6.
  const around = t.times(48, (k) => { const r = 6 * Math.sqrt((k + 0.5) / 48); const a = k * Math.PI * (3 - Math.sqrt(5)); return [Math.cos(a) * r, Math.sin(a) * r]; });
  const best = (p) => around.reduce((b, o) => (ring(p.x + o[0], p.y + o[1]) > ring(p.x + b[0], p.y + b[1]) ? o : b), [0, 0]);
  const snapped = t.scatter({ spacing: 7 }).move(best);
  return [circle(50, 50, 30, { pen: 'stabilo-88-blue' }), snapped.points.map((p) => circle(p, 1))];
});

t.quadtree()

/_sig/Toolkit.quadtree.mdx

Divides the drawable, or the within area, into cells. A cell splits where the points are dense, until no cell holds more than capacity points. The result is a material of cell walls. Where a wall meets a bigger cell's wall at a T, the two share a vertex, so strokes draws each wall once.

The faces are every cell, from the root to the leaves, one level at a time: the root is faces.at(0). A cell that split holds its four quarters. f.children gives them, and f.parent gives the cell that holds f. depth counts the splits from the root, and leaf is true for a cell that did not split. The leaves divide the area with no overlap: faces.filter((f) => f.leaf). A cell that holds others is a face too, so a drawing that fills every face fills a parent over its children. Filter to the leaves first. The source of a cell is the points it holds, as a selection of the input.

Below, the hatched cells are the last splits: each one split once, and its four children are all leaves. Blue lines join the centroid of each cell to the centroid of its parent. So the tree draws on top of its own cells.

import { sketch, circle, line, strokes, polygon, fill, mm, distance } from 'occlude';

export default sketch({ aspect: [1, 1], seed: 3 }, (t) => {
  const tone = (x, y) => Math.max(0, 1 - distance([x, y], [35, 60]) / 40);
  const pts = t.scatter(tone, { spacing: 4 });
  const tree = t.quadtree(pts, { capacity: 4 });
  const last = tree.faces.filter((f) => !f.leaf && f.children.every((c) => c.leaf));
  return [
    last.map((f) => polygon(f, { stroke: false, fill: fill('hatch', { angle: 0, spacing: mm(1.2) }) })),
    strokes(tree),
    tree.faces.filter((f) => f.parent).map((f) => line(f.parent.centroid, f.centroid, { pen: 'stabilo-88-blue' })),
    pts.points.map((p) => circle(p, 0.5)),
  ];
});

t.voronoi()

/_sig/Toolkit.voronoi.mdx

The Voronoi cells of a set of points, as material. The vertices are the cell corners and the edges are the walls. faces holds the cells, one for each site that has a cell, in the order of the sites. The source of a cell is its site, a point of the sites. For the sites of a face selection, write sel.map((f) => f.source). The other way, the cell of one site is cells.faces.find((f) => f.source === site), and the cells of a selection of sites are cells.faces.filter((f) => chosen.has(f.source)). A write that changes the walls reads the faces from the drawing again, and those faces have no source. The cells stop at the drawable. within cuts them at an area: a rect, t.bounds(), a face or a shape. A cut cell closes along the edge of the area, and it keeps its site. The cells do not follow a site that moves. Build them again. Below, a hatch fills the big cells, and a blue line joins each one to its site.

import { sketch, circle, line, strokes, polygon, fill, mm } from 'occlude';

export default sketch({ aspect: [1, 1], seed: 5 }, (t) => {
  const sites = t.scatter({ spacing: 14 });
  const cells = t.voronoi(sites);
  const big = cells.faces.filter((f) => f.area > 160);
  return [
    strokes(cells),
    big.map((f) => polygon(f, { stroke: false, fill: fill('hatch', { angle: 45, spacing: mm(1.5) }) })),
    sites.points.map((p) => circle(p, 0.8, { pen: 'stabilo-88-blue' })),
    big.map((f) => line(f.source, f.centroid, { pen: 'stabilo-88-blue' })),
  ];
});

Below, the sites fill a disc, t.relax keeps them in it, and within cuts the cells at the same disc.

import { sketch, circle, strokes } from 'occlude';

export default sketch({ aspect: [1, 1], seed: 6 }, (t) => {
  const dish = circle(50, 50, 40);
  const sites = t.relax(t.scatter(dish, { spacing: 9 }), { iterations: 4 });
  return [strokes(t.voronoi(sites, { within: dish })), sites.points.map((p) => circle(p, 0.8, { pen: 'stabilo-88-blue' }))];
});

hull()

/_sig/hull.mdx

The outline of a point cloud, as closed contours an area consumer reads. alpha sets the largest circle the outline may bite out of the cloud. At alpha: 0 no circle is too large, nothing is bitten out, and the answer is the convex hull. Raise it and the outline follows the cloud's own hollows. Raise it further and the cloud falls into clumps, each clump with its own contour. Nested contours are holes, read even-odd the way polygon reads them. Fewer than three points, or every point on one line, gives no contour at all. Pure: no seed and no paper.

Three canopies over one wood. The thin black line is the convex hull of every leaf, and the green lines are the same leaves at alpha: 1 / 5.

import { sketch, strokes, hull, circle } from 'occlude';

export default sketch({ aspect: [1, 1], seed: 4 }, (t) => {
  const leaves = [[22, 30], [78, 26], [50, 72]].flatMap(([cx, cy]) =>
    t.times(90, () => [cx + t.gaussian(0, 8.5), cy + t.gaussian(0, 8.5)]));
  return [
    strokes(hull(leaves), { pen: 'pigma-005-black' }),
    strokes(hull(leaves, { alpha: 1 / 5 }), { pen: 'stabilo-88-green' }),
    leaves.map((p) => circle(p, 0.45, { pen: 'pigma-005-black' })),
  ];
});