Public
Edited
Nov 13, 2024
Importers
Insert cell
Insert cell
Insert cell
function Polygons() {
const polygonList = [];
const vec = ([x1, y1], [x2, y2]) => [x2 - x1, y2 - y1];
const Polygon = class {
constructor() {
this.cp = []; // clip path: array of [x,y] pairs
this.dp = []; // 2d lines [x0,y0],[x1,y1] to draw
this.aabb = []; // AABB bounding box
}
addPoints(...points) {
// add point to clip path and update bounding box
let xmin = 1e5,
xmax = -1e5,
ymin = 1e5,
ymax = -1e5;
(this.cp = [...this.cp, ...points]).forEach((p) => {
(xmin = Math.min(xmin, p[0])), (xmax = Math.max(xmax, p[0]));
(ymin = Math.min(ymin, p[1])), (ymax = Math.max(ymax, p[1]));
});
this.aabb = [
(xmin + xmax) / 2,
(ymin + ymax) / 2,
(xmax - xmin) / 2,
(ymax - ymin) / 2
];
}
addSegments(...points) {
// add segments (each a pair of points)
points.forEach((p) => this.dp.push(p));
}
addOutline() {
for (let i = 0, l = this.cp.length; i < l; i++) {
this.dp.push(this.cp[i], this.cp[(i + 1) % l]);
}
}
draw(plot) {
const start = [0, 0];
let pos = start;
for (let i = 0, l = this.dp.length; i < l; i += 2) {
plot.move(...vec(pos, (pos = this.dp[i])));
plot.draw(...vec(pos, (pos = this.dp[i + 1])));
}
plot.move(...vec(pos, start));
}
addHatching(a, d) {
const tp = new Polygon();
tp.cp.push([-1e5, -1e5], [1e5, -1e5], [1e5, 1e5], [-1e5, 1e5]);
const dx = Math.sin(a) * d,
dy = Math.cos(a) * d;
const cx = Math.sin(a) * 200,
cy = Math.cos(a) * 200;
for (let i = 0.5; i < 150 / d; i++) {
tp.dp.push([dx * i + cy, dy * i - cx], [dx * i - cy, dy * i + cx]);
tp.dp.push([-dx * i + cy, -dy * i - cx], [-dx * i - cy, -dy * i + cx]);
}
tp.boolean(this, false);
this.dp = [...this.dp, ...tp.dp];
}
inside(p) {
let int = 0; // find number of i ntersection points from p to far away
for (let i = 0, l = this.cp.length; i < l; i++) {
if (
this.segment_intersect(
p,
[0.1, -1000],
this.cp[i],
this.cp[(i + 1) % l]
)
) {
int++;
}
}
return int & 1; // if even your outside
}
boolean(p, diff = true) {
// bouding box optimization by ge1doot.
if (
Math.abs(this.aabb[0] - p.aabb[0]) - (p.aabb[2] + this.aabb[2]) >= 0 &&
Math.abs(this.aabb[1] - p.aabb[1]) - (p.aabb[3] + this.aabb[3]) >= 0
)
return this.dp.length > 0;

// polygon diff algorithm (narrow phase)
const ndp = [];
for (let i = 0, l = this.dp.length; i < l; i += 2) {
const ls0 = this.dp[i];
const ls1 = this.dp[i + 1];
// find all intersections with clip path
const int = [];
for (let j = 0, cl = p.cp.length; j < cl; j++) {
const pint = this.segment_intersect(
ls0,
ls1,
p.cp[j],
p.cp[(j + 1) % cl]
);
if (pint !== false) {
int.push(pint);
}
}
if (int.length === 0) {
// 0 intersections, inside or outside?
if (diff === !p.inside(ls0)) {
ndp.push(ls0, ls1);
}
} else {
int.push(ls0, ls1);
// order intersection points on line ls.p1 to ls.p2
const cmpx = ls1[0] - ls0[0];
const cmpy = ls1[1] - ls0[1];
int.sort(
(a, b) =>
(a[0] - ls0[0]) * cmpx +
(a[1] - ls0[1]) * cmpy -
(b[0] - ls0[0]) * cmpx -
(b[1] - ls0[1]) * cmpy
);

for (let j = 0; j < int.length - 1; j++) {
if (
(int[j][0] - int[j + 1][0]) ** 2 +
(int[j][1] - int[j + 1][1]) ** 2 >=
0.001
) {
if (
diff ===
!p.inside([
(int[j][0] + int[j + 1][0]) / 2,
(int[j][1] + int[j + 1][1]) / 2
])
) {
ndp.push(int[j], int[j + 1]);
}
}
}
}
}
return (this.dp = ndp).length > 0;
}
//port of http://paulbourke.net/geometry/pointlineplane/Helpers.cs
segment_intersect(l1p1, l1p2, l2p1, l2p2) {
const d =
(l2p2[1] - l2p1[1]) * (l1p2[0] - l1p1[0]) -
(l2p2[0] - l2p1[0]) * (l1p2[1] - l1p1[1]);
if (d === 0) return false;
const n_a =
(l2p2[0] - l2p1[0]) * (l1p1[1] - l2p1[1]) -
(l2p2[1] - l2p1[1]) * (l1p1[0] - l2p1[0]);
const n_b =
(l1p2[0] - l1p1[0]) * (l1p1[1] - l2p1[1]) -
(l1p2[1] - l1p1[1]) * (l1p1[0] - l2p1[0]);
const ua = n_a / d;
const ub = n_b / d;
if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1) {
return [
l1p1[0] + ua * (l1p2[0] - l1p1[0]),
l1p1[1] + ua * (l1p2[1] - l1p1[1])
];
}
return false;
}
};
return {
list: () => polygonList,
create: () => new Polygon(),
draw: (plot, p, addToVisList = true) => {
for (let j = 0; j < polygonList.length && p.boolean(polygonList[j]); j++);
p.draw(plot);
if (addToVisList) polygonList.push(p);
}
};
}
Insert cell
{
const plot = Plot();
const polygons = new Polygons();
const { random, cos, sin, PI } = Math;

function drawStar(turtle) {
const [x, y] = [200 * random() - 100, 200 * random() - 100];
const [a, s] = [random() * PI, 10 + 10 * random()];

const polar = (i) => [a + (i / 5) * PI, s * (i % 2 ? 1 : 0.45)];
const cart = ([a, dist]) => [x + cos(a) * dist, y + sin(a) * dist];
const cp = [...Array(10)].map((_, i) => polar(i)).map(cart);

const p1 = polygons.create();
p1.addPoints(...cp);
p1.addOutline(); // polygon 1, star with outline
polygons.draw(plot, p1, true); // draw polygon with outline and add to polygon list

const p2 = polygons.create();
p2.addPoints(...cp.map(([x, y]) => [x + 1, y + 3])); // shadow offset
p2.addHatching(-PI / 4, 0.5); // polygon 2, star shadow
polygons.draw(plot, p2, false); // draw shadow, don't add to polygon list
}
for (let i = 0; i < 1000; i++) drawStar(plot);
return viewer([width, 400]).view(plot, { "stroke-width": 0.2 });
}
Insert cell
{
const plot = Plot();
const polygons = new Polygons();
const p1 = polygons.create();
p1.addPoints([0, 0], [100, 0], [100, 100], [0, 100]);
p1.addOutline();
polygons.draw(plot, p1, true);
const p2 = polygons.create();
p2.addPoints([10, 10], [110, 10], [110, 60], [10, 60]);
p2.addOutline();
polygons.draw(plot, p2, true);
return viewer([width, 400]).view(plot, { "stroke-width": 0.2 });
}
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