-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(geom): add ICollate & ICopy impls, re-add/update convexHull2
- Loading branch information
1 parent
2ba9931
commit 3b1bf64
Showing
8 changed files
with
140 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Vec2 } from "@thi.ng/vectors/vec2"; | ||
import { corner } from "./corner"; | ||
|
||
/** | ||
* Returns array of points defining the Convex Hull of `pts` using | ||
* Graham Scan method. | ||
* | ||
* https://en.wikipedia.org/wiki/Graham_scan | ||
* | ||
* @param pts | ||
*/ | ||
export const convexHull2 = (pts: ReadonlyArray<Vec2>) => { | ||
const num = pts.length; | ||
const res: Vec2[] = []; | ||
let h = 0, i; | ||
pts = pts.slice().sort(Vec2.comparator(0, 1)); | ||
const scan = (p: Vec2, thresh: number) => { | ||
while (h >= thresh && corner(res[h - 2], res[h - 1], p) >= 0) { | ||
res.pop(); | ||
h--; | ||
} | ||
res[h++] = p; | ||
}; | ||
for (i = 0; i < num; i++) { | ||
scan(pts[i], 2); | ||
} | ||
res.pop(); | ||
h--; | ||
const h2 = h + 2; | ||
for (i = num - 1; i >= 0; i--) { | ||
scan(pts[i], h2); | ||
} | ||
res.pop(); | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { EPS, sign1 } from "@thi.ng/vectors/math"; | ||
import { Vec2 } from "@thi.ng/vectors/vec2"; | ||
|
||
export const corner = (a: Readonly<Vec2>, b: Readonly<Vec2>, c: Readonly<Vec2>) => { | ||
const ax = a.x, | ||
ay = a.y; | ||
return (b.x - ax) * (c.y - ay) - (c.x - ax) * (b.y - ay); | ||
}; | ||
|
||
export const classify = (a: Readonly<Vec2>, b: Readonly<Vec2>, c: Readonly<Vec2>, eps = EPS) => | ||
sign1(corner(a, b, c), eps); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { wrap } from "@thi.ng/transducers/iter/wrap"; | ||
import { partition } from "@thi.ng/transducers/xform/partition"; | ||
|
||
export const edges = <T>(vertices: T[], closed = false) => { | ||
return partition(2, 1, closed ? wrap(vertices, 1, false, true) : vertices); | ||
export const edges = <T>(vertices: ReadonlyArray<T>, closed = false) => { | ||
return partition<T>(2, 1, closed ? wrap(vertices, 1, false, true) : vertices); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters