Published
Edited
Oct 5, 2022
25 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function drawPolygons() {
const margin = (marginUV * minSize) / 5;
const minMaxes = [margin, width - margin, margin, height - margin];
const stroke = colors.stroke || colors.background;
return polygons.reduce((acc, p) => {
const { p1, p2, p3, p4, fill } = p;
return `${acc}
<polygon
points="${uv2xy(...p1, ...minMaxes)} ${uv2xy(...p2, ...minMaxes)} ${uv2xy(
...p3,
...minMaxes
)} ${uv2xy(...p4, ...minMaxes)}"
fill="${fill}"
stroke="${hasStroke ? stroke : fill}" stroke-width="${
hasStroke ? strokeWidth : 1
}" />`;
}, "");
}
Insert cell
uv2xy = (u, v, xMin, xMax, yMin, yMax) => {
return [CSMath.lerp(xMin, xMax, u), CSMath.lerp(yMin, yMax, v)];
}
Insert cell
polygons = {
const first = {
p1: [0, 0],
p2: [1, 0],
p3: [1, 1],
p4: [0, 1]
};
return divideRect(first.p1, first.p2, first.p3, first.p4, passes);
}
Insert cell
function divideRect(p1, p2, p3, p4, n) {
if (n <= 1) {
return [
{
p1,
p2,
p3,
p4,
fill: CSRandom.pick(colors.colors)
}
];
} else {
const w = distance(...p1, ...p2) + distance(...p3, ...p4);
const h = distance(...p1, ...p4) + distance(...p2, ...p3);
const t = 2 + 8 * skewness;
const r1 = (1 / t) * CSRandom.rangeFloor(1, t);
const r2 = (1 / t) * CSRandom.rangeFloor(1, t);

if (w < h) {
const v1 = divideLine(...p1, ...p4, r1);
const v2 = divideLine(...p2, ...p3, r2);
return [
...divideRect(p1, p2, v2, v1, n - 1),
...divideRect(v1, v2, p3, p4, n - 1)
];
} else {
const v1 = divideLine(...p1, ...p2, r1);
const v2 = divideLine(...p3, ...p4, r2);
return [
...divideRect(p1, v1, v2, p4, n - 1),
...divideRect(v1, p2, p3, v2, n - 1)
];
}
}
}
Insert cell
distance = (x1, y1, x2, y2) =>
Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))
Insert cell
divideLine = (x1, y1, x2, y2, ratio) => [
x1 + ratio * (x2 - x1),
y1 + ratio * (y2 - y1)
]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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