@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 +1 -1
- package/graham-scan.js +50 -78
- package/package.json +8 -5
package/CHANGELOG.md
CHANGED
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
|
|
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.
|
|
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
|
|
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.
|
|
37
|
-
"@thi.ng/vectors": "^7.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": "
|
|
80
|
+
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
|
|
78
81
|
}
|