@thi.ng/geom-hull 2.1.87 → 2.1.88

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-12-09T19:12:03Z
3
+ - **Last updated**: 2023-12-11T10:07:09Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
package/graham-scan.js CHANGED
@@ -1,86 +1,58 @@
1
1
  import { EPS } from "@thi.ng/math/api";
2
2
  const atan2 = Math.atan2;
3
- /**
4
- * Returns array of points defining the 2D Convex Hull of `pts` using the Graham
5
- * Scan method.
6
- *
7
- * - https://en.wikipedia.org/wiki/Graham_scan
8
- * - http://c.thi.ng/
9
- *
10
- * @param pts - input points
11
- * @param eps - tolerance for colinear neighbor detection
12
- */
13
- export const grahamScan2 = (pts, eps = EPS) => {
14
- const num = pts.length;
15
- if (num <= 3)
16
- return pts.slice();
17
- let h = 1;
18
- let i;
19
- let p;
20
- let q;
21
- let r;
22
- let rx;
23
- let ry;
24
- // find min YX index
25
- const min = findMin(pts);
26
- [rx, ry] = pts[min];
27
- const sorted = [];
28
- // compute & sort by polar ordering relative to min
29
- for (i = 0; i < num; i++) {
30
- p = pts[i];
31
- sorted[i] = { p, t: atan2(p[1] - ry, p[0] - rx) };
3
+ const grahamScan2 = (pts, eps = EPS) => {
4
+ const num = pts.length;
5
+ if (num <= 3)
6
+ return pts.slice();
7
+ let h = 1;
8
+ let i;
9
+ let p;
10
+ let q;
11
+ let r;
12
+ let rx;
13
+ let ry;
14
+ const min = findMin(pts);
15
+ [rx, ry] = pts[min];
16
+ const sorted = [];
17
+ for (i = 0; i < num; i++) {
18
+ p = pts[i];
19
+ sorted[i] = { p, t: atan2(p[1] - ry, p[0] - rx) };
20
+ }
21
+ sorted.sort((a, b) => a.t !== b.t ? a.t - b.t : a.p[0] - b.p[0]);
22
+ const hull = [sorted[0].p];
23
+ for (i = 1; i < num; i++) {
24
+ p = hull[h - 2];
25
+ q = hull[h - 1];
26
+ r = sorted[i].p;
27
+ rx = r[0];
28
+ ry = r[1];
29
+ while (h > 1 && notCCW(p[0], p[1], q[0], q[1], rx, ry, eps) || h === 1 && q[0] === rx && q[1] === ry) {
30
+ h--;
31
+ q = p;
32
+ p = hull[h - 2];
32
33
  }
33
- sorted.sort((a, b) => (a.t !== b.t ? a.t - b.t : a.p[0] - b.p[0]));
34
- const hull = [sorted[0].p];
35
- for (i = 1; i < num; i++) {
36
- p = hull[h - 2];
37
- q = hull[h - 1];
38
- r = sorted[i].p;
39
- rx = r[0];
40
- ry = r[1];
41
- while ((h > 1 && notCCW(p[0], p[1], q[0], q[1], rx, ry, eps)) ||
42
- (h === 1 && q[0] === rx && q[1] === ry)) {
43
- h--;
44
- q = p;
45
- p = hull[h - 2];
46
- }
47
- hull[h++] = r;
48
- }
49
- hull.length = h;
50
- return hull;
34
+ hull[h++] = r;
35
+ }
36
+ hull.length = h;
37
+ return hull;
51
38
  };
52
- /**
53
- * Returns true, if triangle defined by ABC is NOT counter clockwise,
54
- * i.e. clockwise or colinear.
55
- *
56
- * [`signedArea2()`](https://docs.thi.ng/umbrella/vectors/functions/signedArea2.html)
57
- *
58
- * @param ax -
59
- * @param ay -
60
- * @param bx -
61
- * @param by -
62
- * @param cx -
63
- * @param cy -
64
- */
65
39
  const notCCW = (ax, ay, bx, by, cx, cy, eps) => (by - ay) * (cx - ax) >= (bx - ax) * (cy - ay) - eps;
66
- /**
67
- * Returns index of point with lowest YX coords.
68
- *
69
- * @param pts -
70
- */
71
40
  const findMin = (pts) => {
72
- let n = pts.length - 1;
73
- let minID = n;
74
- let [minX, minY] = pts[n];
75
- let p, y;
76
- for (; n-- > 0;) {
77
- p = pts[n];
78
- y = p[1];
79
- if (y < minY || (y === minY && p[0] < minX)) {
80
- minX = p[0];
81
- minY = y;
82
- minID = n;
83
- }
41
+ let n = pts.length - 1;
42
+ let minID = n;
43
+ let [minX, minY] = pts[n];
44
+ let p, y;
45
+ for (; n-- > 0; ) {
46
+ p = pts[n];
47
+ y = p[1];
48
+ if (y < minY || y === minY && p[0] < minX) {
49
+ minX = p[0];
50
+ minY = y;
51
+ minID = n;
84
52
  }
85
- return minID;
53
+ }
54
+ return minID;
55
+ };
56
+ export {
57
+ grahamScan2
86
58
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/geom-hull",
3
- "version": "2.1.87",
3
+ "version": "2.1.88",
4
4
  "description": "Fast 2D convex hull (Graham Scan)",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -24,7 +24,9 @@
24
24
  "author": "Karsten Schmidt (https://thi.ng)",
25
25
  "license": "Apache-2.0",
26
26
  "scripts": {
27
- "build": "yarn clean && tsc --declaration",
27
+ "build": "yarn build:esbuild && yarn build:decl",
28
+ "build:decl": "tsc --declaration --emitDeclarationOnly",
29
+ "build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
28
30
  "clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
29
31
  "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
30
32
  "doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
@@ -33,11 +35,12 @@
33
35
  "test": "bun test"
34
36
  },
35
37
  "dependencies": {
36
- "@thi.ng/math": "^5.7.6",
37
- "@thi.ng/vectors": "^7.8.8"
38
+ "@thi.ng/math": "^5.7.7",
39
+ "@thi.ng/vectors": "^7.8.9"
38
40
  },
39
41
  "devDependencies": {
40
42
  "@microsoft/api-extractor": "^7.38.3",
43
+ "esbuild": "^0.19.8",
41
44
  "rimraf": "^5.0.5",
42
45
  "tools": "^0.0.1",
43
46
  "typedoc": "^0.25.4",
@@ -74,5 +77,5 @@
74
77
  "parent": "@thi.ng/geom",
75
78
  "year": 2013
76
79
  },
77
- "gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n"
80
+ "gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
78
81
  }