@johnmorrisdotca/kyogijo-stadiums 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +129 -0
- package/LICENSE +21 -0
- package/NOTICE.md +293 -0
- package/README.md +600 -2
- package/dist/annotate.d.ts +47 -0
- package/dist/annotate.js +349 -0
- package/dist/atlas-entry.d.ts +4 -0
- package/dist/atlas-entry.js +3 -0
- package/dist/atlas.d.ts +131 -0
- package/dist/atlas.js +149 -0
- package/dist/background.d.ts +56 -0
- package/dist/background.js +127 -0
- package/dist/ballpark.d.ts +48 -0
- package/dist/ballpark.js +129 -0
- package/dist/canvas-entry.d.ts +2 -0
- package/dist/canvas-entry.js +2 -0
- package/dist/canvas.d.ts +85 -0
- package/dist/canvas.js +279 -0
- package/dist/charts-entry.d.ts +10 -0
- package/dist/charts-entry.js +8 -0
- package/dist/charts.baseball.d.ts +30 -0
- package/dist/charts.baseball.js +152 -0
- package/dist/charts.curling.d.ts +52 -0
- package/dist/charts.curling.js +196 -0
- package/dist/charts.d.ts +172 -0
- package/dist/charts.js +508 -0
- package/dist/charts.network.d.ts +66 -0
- package/dist/charts.network.js +64 -0
- package/dist/charts.soccer.d.ts +43 -0
- package/dist/charts.soccer.js +191 -0
- package/dist/charts.types.d.ts +396 -0
- package/dist/charts.types.js +1 -0
- package/dist/colour.d.ts +29 -0
- package/dist/colour.js +97 -0
- package/dist/common.d.ts +14 -0
- package/dist/common.js +32 -0
- package/dist/court.d.ts +89 -0
- package/dist/court.js +248 -0
- package/dist/courts.style.d.ts +20 -0
- package/dist/courts.style.js +54 -0
- package/dist/cricket.board.d.ts +18 -0
- package/dist/cricket.board.js +67 -0
- package/dist/curling.board.d.ts +24 -0
- package/dist/curling.board.js +147 -0
- package/dist/d3-entry.d.ts +3 -0
- package/dist/d3-entry.js +2 -0
- package/dist/d3.d.ts +45 -0
- package/dist/d3.js +56 -0
- package/dist/draw.d.ts +52 -0
- package/dist/draw.js +705 -0
- package/dist/element-define.d.ts +2 -0
- package/dist/element-define.js +4 -0
- package/dist/element-entry.d.ts +1 -0
- package/dist/element-entry.js +2 -0
- package/dist/element.d.ts +39 -0
- package/dist/element.js +321 -0
- package/dist/eras-entry.d.ts +2 -0
- package/dist/eras-entry.js +2 -0
- package/dist/eras.d.ts +204 -0
- package/dist/eras.js +396 -0
- package/dist/events-entry.d.ts +3 -0
- package/dist/events-entry.js +3 -0
- package/dist/events.d.ts +54 -0
- package/dist/events.data.d.ts +59 -0
- package/dist/events.data.js +297 -0
- package/dist/events.js +127 -0
- package/dist/features.d.ts +31 -0
- package/dist/features.js +284 -0
- package/dist/field.score.d.ts +5 -0
- package/dist/field.score.js +33 -0
- package/dist/fields/afl.d.ts +11 -0
- package/dist/fields/afl.js +103 -0
- package/dist/fields/cricket.d.ts +9 -0
- package/dist/fields/cricket.js +81 -0
- package/dist/fields/fieldhockey.d.ts +6 -0
- package/dist/fields/fieldhockey.js +81 -0
- package/dist/fields/gaelic.d.ts +10 -0
- package/dist/fields/gaelic.js +86 -0
- package/dist/fields/handball.d.ts +6 -0
- package/dist/fields/handball.js +85 -0
- package/dist/fields/lacrosse.d.ts +17 -0
- package/dist/fields/lacrosse.js +169 -0
- package/dist/fields/rugby.d.ts +10 -0
- package/dist/fields/rugby.js +130 -0
- package/dist/fields.d.ts +12 -0
- package/dist/fields.js +44 -0
- package/dist/fields.kit.d.ts +63 -0
- package/dist/fields.kit.js +108 -0
- package/dist/fields.style.d.ts +4 -0
- package/dist/fields.style.js +19 -0
- package/dist/fields.types.d.ts +155 -0
- package/dist/fields.types.js +1 -0
- package/dist/format.d.ts +10 -0
- package/dist/format.js +28 -0
- package/dist/formations.d.ts +18 -0
- package/dist/formations.js +211 -0
- package/dist/frames-entry.d.ts +8 -0
- package/dist/frames-entry.js +7 -0
- package/dist/frames.d.ts +199 -0
- package/dist/frames.js +271 -0
- package/dist/frames.providers.d.ts +16 -0
- package/dist/frames.providers.js +302 -0
- package/dist/geojson-entry.d.ts +2 -0
- package/dist/geojson-entry.js +2 -0
- package/dist/geojson.d.ts +79 -0
- package/dist/geojson.js +147 -0
- package/dist/geometry-entry.d.ts +7 -0
- package/dist/geometry-entry.js +5 -0
- package/dist/geometry.d.ts +295 -0
- package/dist/geometry.js +361 -0
- package/dist/grounds-entry.d.ts +4 -0
- package/dist/grounds-entry.js +3 -0
- package/dist/grounds.d.ts +134 -0
- package/dist/grounds.data.d.ts +2 -0
- package/dist/grounds.data.js +1849 -0
- package/dist/grounds.js +74 -0
- package/dist/heat.colour.d.ts +37 -0
- package/dist/heat.colour.js +54 -0
- package/dist/heat.d.ts +43 -0
- package/dist/heat.js +614 -0
- package/dist/heat.stats.d.ts +123 -0
- package/dist/heat.stats.js +413 -0
- package/dist/heat.types.d.ts +145 -0
- package/dist/heat.types.js +1 -0
- package/dist/heat.zones.d.ts +54 -0
- package/dist/heat.zones.js +424 -0
- package/dist/homeside.d.ts +16 -0
- package/dist/homeside.js +43 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +29 -0
- package/dist/layers-entry.d.ts +19 -0
- package/dist/layers-entry.js +11 -0
- package/dist/layers.d.ts +41 -0
- package/dist/layers.env.d.ts +72 -0
- package/dist/layers.env.js +79 -0
- package/dist/layers.js +533 -0
- package/dist/layers.marks.d.ts +45 -0
- package/dist/layers.marks.js +168 -0
- package/dist/layers.types.d.ts +226 -0
- package/dist/layers.types.js +1 -0
- package/dist/layout.d.ts +97 -0
- package/dist/layout.js +381 -0
- package/dist/layout.style.d.ts +6 -0
- package/dist/layout.style.js +17 -0
- package/dist/layouts-entry.d.ts +5 -0
- package/dist/layouts-entry.js +4 -0
- package/dist/leagues/aussie.d.ts +57 -0
- package/dist/leagues/aussie.js +36 -0
- package/dist/leagues/baseball.d.ts +58 -0
- package/dist/leagues/baseball.js +171 -0
- package/dist/leagues/basketball.d.ts +67 -0
- package/dist/leagues/basketball.js +128 -0
- package/dist/leagues/courts.d.ts +212 -0
- package/dist/leagues/courts.js +114 -0
- package/dist/leagues/cricket.d.ts +45 -0
- package/dist/leagues/cricket.js +34 -0
- package/dist/leagues/curling.d.ts +64 -0
- package/dist/leagues/curling.js +53 -0
- package/dist/leagues/fieldhockey.d.ts +48 -0
- package/dist/leagues/fieldhockey.js +29 -0
- package/dist/leagues/fields.d.ts +10 -0
- package/dist/leagues/fields.js +6 -0
- package/dist/leagues/football.d.ts +8 -0
- package/dist/leagues/football.js +82 -0
- package/dist/leagues/gaelic.d.ts +50 -0
- package/dist/leagues/gaelic.js +29 -0
- package/dist/leagues/handball.d.ts +45 -0
- package/dist/leagues/handball.js +32 -0
- package/dist/leagues/hockey.d.ts +63 -0
- package/dist/leagues/hockey.js +166 -0
- package/dist/leagues/lacrosse.d.ts +103 -0
- package/dist/leagues/lacrosse.js +175 -0
- package/dist/leagues/rugby.d.ts +59 -0
- package/dist/leagues/rugby.js +72 -0
- package/dist/leagues/sources.d.ts +18 -0
- package/dist/leagues/sources.js +95 -0
- package/dist/leagues/types.d.ts +43 -0
- package/dist/leagues/types.js +5 -0
- package/dist/leagues-entry.d.ts +23 -0
- package/dist/leagues-entry.js +15 -0
- package/dist/leagues.d.ts +172 -0
- package/dist/leagues.js +462 -0
- package/dist/look.d.ts +50 -0
- package/dist/look.js +186 -0
- package/dist/mark.d.ts +35 -0
- package/dist/mark.js +59 -0
- package/dist/mount-entry.d.ts +2 -0
- package/dist/mount-entry.js +2 -0
- package/dist/mount.d.ts +63 -0
- package/dist/mount.js +167 -0
- package/dist/mow.d.ts +36 -0
- package/dist/mow.js +111 -0
- package/dist/names.d.ts +77 -0
- package/dist/names.js +97 -0
- package/dist/outline.d.ts +25 -0
- package/dist/outline.js +112 -0
- package/dist/parks-entry.d.ts +4 -0
- package/dist/parks-entry.js +3 -0
- package/dist/parks.d.ts +200 -0
- package/dist/parks.data.d.ts +2 -0
- package/dist/parks.data.js +4489 -0
- package/dist/parks.js +92 -0
- package/dist/plot-entry.d.ts +2 -0
- package/dist/plot-entry.js +2 -0
- package/dist/plot.d.ts +74 -0
- package/dist/plot.js +68 -0
- package/dist/png-entry.d.ts +2 -0
- package/dist/png-entry.js +2 -0
- package/dist/png.d.ts +26 -0
- package/dist/png.js +46 -0
- package/dist/ranges.d.ts +33 -0
- package/dist/ranges.js +178 -0
- package/dist/ranges.names.d.ts +31 -0
- package/dist/ranges.names.js +65 -0
- package/dist/react-entry.d.ts +2 -0
- package/dist/react-entry.js +2 -0
- package/dist/react.d.ts +53 -0
- package/dist/react.js +58 -0
- package/dist/replay-entry.d.ts +5 -0
- package/dist/replay-entry.js +4 -0
- package/dist/replay.control.d.ts +47 -0
- package/dist/replay.control.js +184 -0
- package/dist/replay.d.ts +170 -0
- package/dist/replay.dom.d.ts +21 -0
- package/dist/replay.dom.js +388 -0
- package/dist/replay.js +579 -0
- package/dist/replay.types.d.ts +152 -0
- package/dist/replay.types.js +1 -0
- package/dist/restyle.d.ts +21 -0
- package/dist/restyle.js +70 -0
- package/dist/road.d.ts +26 -0
- package/dist/road.draw.d.ts +6 -0
- package/dist/road.draw.js +269 -0
- package/dist/road.js +59 -0
- package/dist/road.model.d.ts +63 -0
- package/dist/road.model.js +215 -0
- package/dist/road.style.d.ts +7 -0
- package/dist/road.style.js +26 -0
- package/dist/road.types.d.ts +122 -0
- package/dist/road.types.js +1 -0
- package/dist/scene-entry.d.ts +7 -0
- package/dist/scene-entry.js +5 -0
- package/dist/scene.css.d.ts +44 -0
- package/dist/scene.css.js +296 -0
- package/dist/scene.d.ts +27 -0
- package/dist/scene.fit.d.ts +12 -0
- package/dist/scene.fit.js +18 -0
- package/dist/scene.flat.d.ts +38 -0
- package/dist/scene.flat.js +167 -0
- package/dist/scene.js +420 -0
- package/dist/scene.path.d.ts +40 -0
- package/dist/scene.path.js +357 -0
- package/dist/scene.types.d.ts +172 -0
- package/dist/scene.types.js +1 -0
- package/dist/scene.xml.d.ts +31 -0
- package/dist/scene.xml.js +81 -0
- package/dist/score.d.ts +70 -0
- package/dist/score.js +212 -0
- package/dist/sets.board.d.ts +22 -0
- package/dist/sets.board.js +89 -0
- package/dist/sheet.d.ts +18 -0
- package/dist/sheet.js +38 -0
- package/dist/special.d.ts +44 -0
- package/dist/special.js +204 -0
- package/dist/special.style.d.ts +8 -0
- package/dist/special.style.js +36 -0
- package/dist/strings.d.ts +581 -0
- package/dist/strings.js +411 -0
- package/dist/style.d.ts +35 -0
- package/dist/style.js +177 -0
- package/dist/surfaces/baseball.d.ts +33 -0
- package/dist/surfaces/baseball.js +208 -0
- package/dist/surfaces/baseball.walls.d.ts +16 -0
- package/dist/surfaces/baseball.walls.js +150 -0
- package/dist/surfaces/basketball.d.ts +8 -0
- package/dist/surfaces/basketball.js +199 -0
- package/dist/surfaces/curling.d.ts +23 -0
- package/dist/surfaces/curling.js +128 -0
- package/dist/surfaces/field.d.ts +12 -0
- package/dist/surfaces/field.js +163 -0
- package/dist/surfaces/football.d.ts +12 -0
- package/dist/surfaces/football.js +208 -0
- package/dist/surfaces/hockey.d.ts +10 -0
- package/dist/surfaces/hockey.js +166 -0
- package/dist/surfaces/net.d.ts +7 -0
- package/dist/surfaces/net.js +151 -0
- package/dist/surfaces/soccer-providers.d.ts +124 -0
- package/dist/surfaces/soccer-providers.js +334 -0
- package/dist/surfaces/soccer-spec.d.ts +138 -0
- package/dist/surfaces/soccer-spec.js +175 -0
- package/dist/surfaces/soccer.d.ts +25 -0
- package/dist/surfaces/soccer.js +270 -0
- package/dist/surfaces/surface.d.ts +180 -0
- package/dist/surfaces/surface.js +49 -0
- package/dist/team.common.d.ts +75 -0
- package/dist/team.common.js +97 -0
- package/dist/team.formation.d.ts +24 -0
- package/dist/team.formation.js +121 -0
- package/dist/team.geometry.d.ts +66 -0
- package/dist/team.geometry.js +304 -0
- package/dist/team.layers.d.ts +16 -0
- package/dist/team.layers.js +32 -0
- package/dist/team.motion.d.ts +9 -0
- package/dist/team.motion.js +325 -0
- package/dist/team.network.d.ts +43 -0
- package/dist/team.network.js +215 -0
- package/dist/team.shape.d.ts +12 -0
- package/dist/team.shape.js +217 -0
- package/dist/team.types.d.ts +189 -0
- package/dist/team.types.js +1 -0
- package/dist/text.d.ts +31 -0
- package/dist/text.js +88 -0
- package/dist/themes.d.ts +14 -0
- package/dist/themes.js +24 -0
- package/dist/types.d.ts +565 -0
- package/dist/types.js +1 -0
- package/dist/units.d.ts +45 -0
- package/dist/units.js +90 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/package.json +234 -4
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
// The geometry of a team's shape, written for this package from the plain mathematics and no code of any library: the convex hull of
|
|
2
|
+
// some points (Andrew's monotone chain), their Delaunay triangulation (Bowyer and Watson's incremental method, with the hull kept by
|
|
3
|
+
// "ghost" triangles instead of a super triangle, so that no sliver of the hull is lost), and the cells of their Voronoi diagram, which are
|
|
4
|
+
// the surface's box cut by the bisector of each neighbour in the triangulation. Every function is pure, takes `[x, y]` pairs and gives
|
|
5
|
+
// the same answer for the same input. Degenerate input is an answer and not an error: no points, one, two, all on a line, and the same
|
|
6
|
+
// point twice.
|
|
7
|
+
const same = (a, b) => a[0] === b[0] && a[1] === b[1];
|
|
8
|
+
/** Twice the signed area of the triangle a, b, c: positive when c is to the left of a→b with y up (counter-clockwise), 0 when the three are on a line to within rounding. */
|
|
9
|
+
function orient(a, b, c) {
|
|
10
|
+
const left = (a[0] - c[0]) * (b[1] - c[1]);
|
|
11
|
+
const right = (a[1] - c[1]) * (b[0] - c[0]);
|
|
12
|
+
const det = left - right;
|
|
13
|
+
return Math.abs(det) <= 3.4e-16 * (Math.abs(left) + Math.abs(right)) ? 0 : det;
|
|
14
|
+
}
|
|
15
|
+
/** Positive when d is inside the circle through a, b, c (counter-clockwise), and not within rounding of its edge. */
|
|
16
|
+
function inCircle(a, b, c, d) {
|
|
17
|
+
const adx = a[0] - d[0];
|
|
18
|
+
const ady = a[1] - d[1];
|
|
19
|
+
const bdx = b[0] - d[0];
|
|
20
|
+
const bdy = b[1] - d[1];
|
|
21
|
+
const cdx = c[0] - d[0];
|
|
22
|
+
const cdy = c[1] - d[1];
|
|
23
|
+
const alift = adx * adx + ady * ady;
|
|
24
|
+
const blift = bdx * bdx + bdy * bdy;
|
|
25
|
+
const clift = cdx * cdx + cdy * cdy;
|
|
26
|
+
const bc = bdx * cdy;
|
|
27
|
+
const cb = cdx * bdy;
|
|
28
|
+
const ca = cdx * ady;
|
|
29
|
+
const ac = adx * cdy;
|
|
30
|
+
const ab = adx * bdy;
|
|
31
|
+
const ba = bdx * ady;
|
|
32
|
+
const det = alift * (bc - cb) + blift * (ca - ac) + clift * (ab - ba);
|
|
33
|
+
const permanent = (Math.abs(bc) + Math.abs(cb)) * alift + (Math.abs(ca) + Math.abs(ac)) * blift + (Math.abs(ab) + Math.abs(ba)) * clift;
|
|
34
|
+
return det > 1e-12 * permanent ? det : 0;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The convex hull of some points: its corners in order, counter-clockwise with y up (clockwise on a page, where y is down), starting at the least x. A point on an edge of the hull is
|
|
38
|
+
* not a corner. The same point twice is one. With no points the hull is empty, with one it is that point, and with two or with all on a line it is the two ends.
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* import { convexHull } from "@johnmorrisdotca/kyogijo-stadiums/layers";
|
|
42
|
+
* console.log(convexHull([[0, 0], [4, 0], [2, 0], [4, 3], [1, 1], [0, 3]]));
|
|
43
|
+
* // [ [ 0, 0 ], [ 4, 0 ], [ 4, 3 ], [ 0, 3 ] ]
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export function convexHull(points) {
|
|
47
|
+
const sorted = points.filter((p) => Number.isFinite(p[0]) && Number.isFinite(p[1])).map((p) => [p[0], p[1]]).sort((a, b) => a[0] - b[0] || a[1] - b[1]);
|
|
48
|
+
const unique = sorted.filter((p, i) => i === 0 || !same(p, sorted[i - 1]));
|
|
49
|
+
if (unique.length < 3)
|
|
50
|
+
return unique;
|
|
51
|
+
const build = (list) => {
|
|
52
|
+
const chain = [];
|
|
53
|
+
for (const p of list) {
|
|
54
|
+
while (chain.length >= 2 && orient(chain[chain.length - 2], chain[chain.length - 1], p) <= 0)
|
|
55
|
+
chain.pop();
|
|
56
|
+
chain.push(p);
|
|
57
|
+
}
|
|
58
|
+
chain.pop();
|
|
59
|
+
return chain;
|
|
60
|
+
};
|
|
61
|
+
const lower = build(unique);
|
|
62
|
+
const upper = build([...unique].reverse());
|
|
63
|
+
const hull = [...lower, ...upper];
|
|
64
|
+
// All on a line: both chains are the two ends, which the join has twice.
|
|
65
|
+
return hull.length <= 2 || hull.every((p, i) => i < 2 || orient(hull[0], hull[1], p) === 0) ? [unique[0], unique[unique.length - 1]] : hull;
|
|
66
|
+
}
|
|
67
|
+
/** The area of a polygon given by its corners (any orientation); 0 for fewer than three. */
|
|
68
|
+
export function polygonArea(polygon) {
|
|
69
|
+
let twice = 0;
|
|
70
|
+
for (let i = 0; i < polygon.length; i += 1) {
|
|
71
|
+
const a = polygon[i];
|
|
72
|
+
const b = polygon[(i + 1) % polygon.length];
|
|
73
|
+
twice += a[0] * b[1] - b[0] * a[1];
|
|
74
|
+
}
|
|
75
|
+
return Math.abs(twice) / 2;
|
|
76
|
+
}
|
|
77
|
+
/** The centre of mass of a polygon's area, or the mean of its corners when it has none (a point, a line). `undefined` for no corners. */
|
|
78
|
+
export function polygonCentroid(polygon) {
|
|
79
|
+
if (polygon.length === 0)
|
|
80
|
+
return undefined;
|
|
81
|
+
let twice = 0;
|
|
82
|
+
let cx = 0;
|
|
83
|
+
let cy = 0;
|
|
84
|
+
for (let i = 0; i < polygon.length; i += 1) {
|
|
85
|
+
const a = polygon[i];
|
|
86
|
+
const b = polygon[(i + 1) % polygon.length];
|
|
87
|
+
const cross = a[0] * b[1] - b[0] * a[1];
|
|
88
|
+
twice += cross;
|
|
89
|
+
cx += (a[0] + b[0]) * cross;
|
|
90
|
+
cy += (a[1] + b[1]) * cross;
|
|
91
|
+
}
|
|
92
|
+
if (twice === 0)
|
|
93
|
+
return [polygon.reduce((s, p) => s + p[0], 0) / polygon.length, polygon.reduce((s, p) => s + p[1], 0) / polygon.length];
|
|
94
|
+
return [cx / (3 * twice), cy / (3 * twice)];
|
|
95
|
+
}
|
|
96
|
+
const GHOST = -1;
|
|
97
|
+
function circle(points, v) {
|
|
98
|
+
if (v[2] === GHOST)
|
|
99
|
+
return { v, cx: 0, cy: 0, r2: -1 };
|
|
100
|
+
const [a, b, c] = [points[v[0]], points[v[1]], points[v[2]]];
|
|
101
|
+
const d = 2 * (a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1]) + c[0] * (a[1] - b[1]));
|
|
102
|
+
if (d === 0)
|
|
103
|
+
return { v, cx: 0, cy: 0, r2: -1 };
|
|
104
|
+
const a2 = a[0] * a[0] + a[1] * a[1];
|
|
105
|
+
const b2 = b[0] * b[0] + b[1] * b[1];
|
|
106
|
+
const c2 = c[0] * c[0] + c[1] * c[1];
|
|
107
|
+
const cx = (a2 * (b[1] - c[1]) + b2 * (c[1] - a[1]) + c2 * (a[1] - b[1])) / d;
|
|
108
|
+
const cy = (a2 * (c[0] - b[0]) + b2 * (a[0] - c[0]) + c2 * (b[0] - a[0])) / d;
|
|
109
|
+
return { v, cx, cy, r2: (a[0] - cx) ** 2 + (a[1] - cy) ** 2 };
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The Delaunay triangulation of some points: triangles whose circumcircle holds no other point, which cover the points' convex hull. Bowyer and
|
|
113
|
+
* Watson's method: each point is added by removing the triangles whose circumcircle holds it and joining it to the edge of the hole. The hull is
|
|
114
|
+
* kept by "ghost" triangles (an edge and a point at infinity) in place of a big triangle round everything, so that a point on or beyond the hull
|
|
115
|
+
* is handled exactly and no sliver of the hull is lost. Points are added from the least x, so the answer does not depend on the order given
|
|
116
|
+
* (up to which of four points on one circle are joined). Points that are not finite numbers are not allowed; a repeated point is one point.
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* import { delaunay } from "@johnmorrisdotca/kyogijo-stadiums/layers";
|
|
120
|
+
* const t = delaunay([[0, 0], [10, 0], [5, 8], [5, 3]]);
|
|
121
|
+
* console.log(t.points.length, t.triangles.length / 3);
|
|
122
|
+
* // 4 3
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export function delaunay(input) {
|
|
126
|
+
const points = [];
|
|
127
|
+
const of = [];
|
|
128
|
+
const seen = new Map();
|
|
129
|
+
for (const p of input) {
|
|
130
|
+
const key = `${p[0]},${p[1]}`;
|
|
131
|
+
let j = seen.get(key);
|
|
132
|
+
if (j === undefined) {
|
|
133
|
+
j = points.length;
|
|
134
|
+
seen.set(key, j);
|
|
135
|
+
points.push([p[0], p[1]]);
|
|
136
|
+
}
|
|
137
|
+
of.push(j);
|
|
138
|
+
}
|
|
139
|
+
const result = { points, triangles: [], of };
|
|
140
|
+
if (points.length < 3)
|
|
141
|
+
return result;
|
|
142
|
+
const order = points.map((_, i) => i).sort((i, j) => points[i][0] - points[j][0] || points[i][1] - points[j][1] || i - j);
|
|
143
|
+
const a = order[0];
|
|
144
|
+
const b = order[1];
|
|
145
|
+
let k = 2;
|
|
146
|
+
while (k < order.length && orient(points[a], points[b], points[order[k]]) === 0)
|
|
147
|
+
k += 1;
|
|
148
|
+
// Every point on one line: there are no triangles.
|
|
149
|
+
if (k === order.length)
|
|
150
|
+
return result;
|
|
151
|
+
const c = order[k];
|
|
152
|
+
const start = orient(points[a], points[b], points[c]) > 0 ? [a, b, c] : [a, c, b];
|
|
153
|
+
let tris = [circle(points, start), circle(points, [start[1], start[0], GHOST]), circle(points, [start[2], start[1], GHOST]), circle(points, [start[0], start[2], GHOST])];
|
|
154
|
+
const width = points.length + 2;
|
|
155
|
+
const key = (u, v) => (u + 1) * width + (v + 1);
|
|
156
|
+
const bad = (t, r) => {
|
|
157
|
+
const [u, v, w] = t.v;
|
|
158
|
+
if (w === GHOST) {
|
|
159
|
+
const o = orient(points[u], points[v], r);
|
|
160
|
+
if (o > 0)
|
|
161
|
+
return true;
|
|
162
|
+
// On the hull's edge, strictly between its ends: the edge is split.
|
|
163
|
+
return o === 0 && (r[0] - points[u][0]) * (points[v][0] - points[u][0]) + (r[1] - points[u][1]) * (points[v][1] - points[u][1]) > 0 && (r[0] - points[v][0]) * (points[u][0] - points[v][0]) + (r[1] - points[v][1]) * (points[u][1] - points[v][1]) > 0;
|
|
164
|
+
}
|
|
165
|
+
if (t.r2 >= 0) {
|
|
166
|
+
const d2 = (r[0] - t.cx) ** 2 + (r[1] - t.cy) ** 2;
|
|
167
|
+
if (d2 > t.r2 * (1 + 1e-9))
|
|
168
|
+
return false;
|
|
169
|
+
if (d2 < t.r2 * (1 - 1e-9))
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
return inCircle(points[u], points[v], points[w], r) > 0;
|
|
173
|
+
};
|
|
174
|
+
for (let n = 0; n < order.length; n += 1) {
|
|
175
|
+
const i = order[n];
|
|
176
|
+
if (i === a || i === b || i === c)
|
|
177
|
+
continue;
|
|
178
|
+
const r = points[i];
|
|
179
|
+
const kept = [];
|
|
180
|
+
const edges = new Set();
|
|
181
|
+
const removed = [];
|
|
182
|
+
for (const t of tris) {
|
|
183
|
+
if (bad(t, r)) {
|
|
184
|
+
removed.push(t);
|
|
185
|
+
edges.add(key(t.v[0], t.v[1]));
|
|
186
|
+
edges.add(key(t.v[1], t.v[2]));
|
|
187
|
+
edges.add(key(t.v[2], t.v[0]));
|
|
188
|
+
}
|
|
189
|
+
else
|
|
190
|
+
kept.push(t);
|
|
191
|
+
}
|
|
192
|
+
for (const t of removed) {
|
|
193
|
+
for (let e = 0; e < 3; e += 1) {
|
|
194
|
+
const u = t.v[e];
|
|
195
|
+
const v = t.v[(e + 1) % 3];
|
|
196
|
+
// An edge of the hole is one whose reverse is not an edge of a removed triangle.
|
|
197
|
+
if (edges.has(key(v, u)))
|
|
198
|
+
continue;
|
|
199
|
+
kept.push(circle(points, u === GHOST ? [v, i, GHOST] : v === GHOST ? [i, u, GHOST] : [u, v, i]));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
tris = kept;
|
|
203
|
+
}
|
|
204
|
+
for (const t of tris)
|
|
205
|
+
if (t.v[2] !== GHOST && t.v[0] !== GHOST && t.v[1] !== GHOST)
|
|
206
|
+
result.triangles.push(t.v[0], t.v[1], t.v[2]);
|
|
207
|
+
return result;
|
|
208
|
+
}
|
|
209
|
+
/** For each distinct point of a triangulation, the points it shares a triangle edge with, in increasing order. */
|
|
210
|
+
export function neighbours(t) {
|
|
211
|
+
const sets = t.points.map(() => new Set());
|
|
212
|
+
for (let k = 0; k < t.triangles.length; k += 3) {
|
|
213
|
+
for (let e = 0; e < 3; e += 1) {
|
|
214
|
+
const u = t.triangles[k + e];
|
|
215
|
+
const v = t.triangles[k + ((e + 1) % 3)];
|
|
216
|
+
sets[u].add(v);
|
|
217
|
+
sets[v].add(u);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return sets.map((s) => [...s].sort((x, y) => x - y));
|
|
221
|
+
}
|
|
222
|
+
/** The edges of a triangulation, each once, as pairs of indices into its points with the smaller first, in order. */
|
|
223
|
+
export function triangulationEdges(t) {
|
|
224
|
+
const out = new Map();
|
|
225
|
+
const width = t.points.length + 1;
|
|
226
|
+
for (let k = 0; k < t.triangles.length; k += 3) {
|
|
227
|
+
for (let e = 0; e < 3; e += 1) {
|
|
228
|
+
const u = t.triangles[k + e];
|
|
229
|
+
const v = t.triangles[k + ((e + 1) % 3)];
|
|
230
|
+
const lo = Math.min(u, v);
|
|
231
|
+
const hi = Math.max(u, v);
|
|
232
|
+
out.set(lo * width + hi, [lo, hi]);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return [...out.entries()].sort((x, y) => x[0] - y[0]).map(([, pair]) => pair);
|
|
236
|
+
}
|
|
237
|
+
/** A convex polygon cut to the side of the line that is nearer `a` than `b` (Sutherland and Hodgman's clip against one half plane). */
|
|
238
|
+
export function clipNearer(polygon, a, b) {
|
|
239
|
+
// Points p with |p-a|² <= |p-b|²: (b-a)·p <= (|b|²-|a|²)/2.
|
|
240
|
+
const nx = b[0] - a[0];
|
|
241
|
+
const ny = b[1] - a[1];
|
|
242
|
+
const c = (b[0] * b[0] + b[1] * b[1] - a[0] * a[0] - a[1] * a[1]) / 2;
|
|
243
|
+
const f = (p) => nx * p[0] + ny * p[1] - c;
|
|
244
|
+
const out = [];
|
|
245
|
+
for (let i = 0; i < polygon.length; i += 1) {
|
|
246
|
+
const p = polygon[i];
|
|
247
|
+
const q = polygon[(i + 1) % polygon.length];
|
|
248
|
+
const fp = f(p);
|
|
249
|
+
const fq = f(q);
|
|
250
|
+
if (fp <= 0)
|
|
251
|
+
out.push(p);
|
|
252
|
+
if ((fp < 0 && fq > 0) || (fp > 0 && fq < 0)) {
|
|
253
|
+
const t = fp / (fp - fq);
|
|
254
|
+
out.push([p[0] + (q[0] - p[0]) * t, p[1] + (q[1] - p[1]) * t]);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return out;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* The Voronoi cells of some points inside a box: for each input point, the corners of the part of the box nearer to it than to any other point,
|
|
261
|
+
* in order. The cells tile the box. A point given twice has the cell on its first appearance and an empty one on its second. With one point the
|
|
262
|
+
* cell is the box; with points all on a line the cells are strips. The neighbours that cut a cell are the triangulation's, so a cell is made
|
|
263
|
+
* from a handful of cuts and not from every other point.
|
|
264
|
+
* @example
|
|
265
|
+
* ```ts
|
|
266
|
+
* import { voronoiCells } from "@johnmorrisdotca/kyogijo-stadiums/layers";
|
|
267
|
+
* const cells = voronoiCells([[2, 5], [8, 5]], { x: 0, y: 0, w: 10, h: 10 });
|
|
268
|
+
* console.log(cells[0].length, cells[1].map((p) => p.join(",")).join(" "));
|
|
269
|
+
* // 4 5,0 10,0 10,10 5,10
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
export function voronoiCells(points, box) {
|
|
273
|
+
const frame = [[box.x, box.y], [box.x + box.w, box.y], [box.x + box.w, box.y + box.h], [box.x, box.y + box.h]];
|
|
274
|
+
const t = delaunay(points);
|
|
275
|
+
const near = t.triangles.length > 0 ? neighbours(t) : lineNeighbours(t.points);
|
|
276
|
+
const cells = t.points.map((p, i) => {
|
|
277
|
+
let cell = frame;
|
|
278
|
+
for (const j of near[i]) {
|
|
279
|
+
cell = clipNearer(cell, p, t.points[j]);
|
|
280
|
+
if (cell.length === 0)
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
return cell;
|
|
284
|
+
});
|
|
285
|
+
const first = new Set();
|
|
286
|
+
return t.of.map((j) => {
|
|
287
|
+
if (first.has(j))
|
|
288
|
+
return [];
|
|
289
|
+
first.add(j);
|
|
290
|
+
return cells[j];
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
/** The neighbours of points with no triangles: none for one point; the next and the one before along the line for points on a line. */
|
|
294
|
+
function lineNeighbours(points) {
|
|
295
|
+
const order = points.map((_, i) => i).sort((i, j) => points[i][0] - points[j][0] || points[i][1] - points[j][1]);
|
|
296
|
+
const out = points.map(() => []);
|
|
297
|
+
order.forEach((i, rank) => {
|
|
298
|
+
if (rank > 0)
|
|
299
|
+
out[i].push(order[rank - 1]);
|
|
300
|
+
if (rank < order.length - 1)
|
|
301
|
+
out[i].push(order[rank + 1]);
|
|
302
|
+
});
|
|
303
|
+
return out;
|
|
304
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Env, Made } from "./layers.env.ts";
|
|
2
|
+
import type { TeamTable } from "./team.common.ts";
|
|
3
|
+
import type { DelaunayLayer, FlowLayer, FormationLayer, HullLayer, NetworkLayer, SonarLayer, VoronoiLayer } from "./team.types.ts";
|
|
4
|
+
export type { TeamTable } from "./team.common.ts";
|
|
5
|
+
/** The layers this file draws. */
|
|
6
|
+
export type TeamLayer = NetworkLayer | FormationLayer | VoronoiLayer | DelaunayLayer | HullLayer | FlowLayer | SonarLayer;
|
|
7
|
+
/** The kinds of team layer. */
|
|
8
|
+
export declare const TEAM_KINDS: readonly string[];
|
|
9
|
+
/** True for a layer of one of the team kinds. */
|
|
10
|
+
export declare const isTeamLayer: (layer: {
|
|
11
|
+
kind: string;
|
|
12
|
+
}) => layer is TeamLayer;
|
|
13
|
+
/** Draw a team layer. */
|
|
14
|
+
export declare function drawTeam(layer: TeamLayer, env: Env): Made;
|
|
15
|
+
/** The numbers of a team layer, as tables. */
|
|
16
|
+
export declare function teamTables(layer: TeamLayer, env: Env): TeamTable[];
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { drawFormation, formationTables } from "./team.formation.js";
|
|
2
|
+
import { drawFlow, drawSonar, flowTable, sonarTables } from "./team.motion.js";
|
|
3
|
+
import { drawNetwork, networkTables } from "./team.network.js";
|
|
4
|
+
import { delaunayTable, drawDelaunay, drawHull, drawVoronoi, hullTable, voronoiTable } from "./team.shape.js";
|
|
5
|
+
/** The kinds of team layer. */
|
|
6
|
+
export const TEAM_KINDS = Object.freeze(["network", "formation", "voronoi", "delaunay", "hull", "flow", "sonar"]);
|
|
7
|
+
/** True for a layer of one of the team kinds. */
|
|
8
|
+
export const isTeamLayer = (layer) => TEAM_KINDS.includes(layer.kind);
|
|
9
|
+
/** Draw a team layer. */
|
|
10
|
+
export function drawTeam(layer, env) {
|
|
11
|
+
switch (layer.kind) {
|
|
12
|
+
case "network": return drawNetwork(layer, env);
|
|
13
|
+
case "formation": return drawFormation(layer, env);
|
|
14
|
+
case "voronoi": return drawVoronoi(layer, env);
|
|
15
|
+
case "delaunay": return drawDelaunay(layer, env);
|
|
16
|
+
case "hull": return drawHull(layer, env);
|
|
17
|
+
case "flow": return drawFlow(layer, env);
|
|
18
|
+
default: return drawSonar(layer, env);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** The numbers of a team layer, as tables. */
|
|
22
|
+
export function teamTables(layer, env) {
|
|
23
|
+
switch (layer.kind) {
|
|
24
|
+
case "network": return networkTables(layer, env);
|
|
25
|
+
case "formation": return formationTables(layer, env);
|
|
26
|
+
case "voronoi": return voronoiTable(layer, env);
|
|
27
|
+
case "delaunay": return delaunayTable(layer, env);
|
|
28
|
+
case "hull": return hullTable(layer, env);
|
|
29
|
+
case "flow": return flowTable(layer, env);
|
|
30
|
+
default: return sonarTables(layer, env);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Env, type Made } from "./layers.env.ts";
|
|
2
|
+
import { type TeamTable } from "./team.common.ts";
|
|
3
|
+
import type { FlowLayer, SonarLayer } from "./team.types.ts";
|
|
4
|
+
export declare function drawFlow(layer: FlowLayer, env: Env): Made;
|
|
5
|
+
/** The direction of a vector on the surface, in degrees: 0 toward the right end as the surface is drawn wide, 90 toward the top edge, from 0 up to 360. */
|
|
6
|
+
export declare const degreesOf: (dx: number, dy: number) => number;
|
|
7
|
+
export declare function flowTable(layer: FlowLayer, env: Env): TeamTable[];
|
|
8
|
+
export declare function drawSonar(layer: SonarLayer, env: Env): Made;
|
|
9
|
+
export declare function sonarTables(layer: SonarLayer, env: Env): TeamTable[];
|