-
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(geom-axidraw): add shape/point ordering support
- add Point/ShapeOrdering types - add pointsByProximity()/shapesByProximity() HOF ordering fns - add AxiDrawAttribs.sort attrib - add/update doc strings - update deps
- Loading branch information
1 parent
c43b6f5
commit 58b3b5d
Showing
6 changed files
with
122 additions
and
12 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./api.js"; | ||
export * from "./as-axidraw.js"; | ||
export * from "./sort.js"; |
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,44 @@ | ||
import { sortByCachedKey } from "@thi.ng/arrays/sort-cached"; | ||
import { compareNumAsc } from "@thi.ng/compare/numeric"; | ||
import type { IShape } from "@thi.ng/geom-api"; | ||
import { bounds2 } from "@thi.ng/geom-poly-utils/bounds"; | ||
import { bounds } from "@thi.ng/geom/bounds"; | ||
import { centroid } from "@thi.ng/geom/centroid"; | ||
import { __collBounds } from "@thi.ng/geom/internal/bounds"; | ||
import { ReadonlyVec, ZERO2 } from "@thi.ng/vectors/api"; | ||
import { distSq2 } from "@thi.ng/vectors/distsq"; | ||
import type { PointOrdering, ShapeOrdering } from "./api.js"; | ||
|
||
/** | ||
* Higher order point sorting fn. Sorts points by proximity to given `ref` point | ||
* (default: bounds min). | ||
* | ||
* @param ref | ||
*/ | ||
export const pointsByProximity = | ||
(ref?: ReadonlyVec): PointOrdering => | ||
(pts: ReadonlyVec[]) => { | ||
ref = ref || bounds2(pts)[0]; | ||
return sortByCachedKey( | ||
pts.slice(), | ||
(p) => distSq2(p, ref!), | ||
compareNumAsc | ||
); | ||
}; | ||
|
||
/** | ||
* Higher order shape sorting fn. Sorts shapes by their centroid's proximity to | ||
* given `ref` point (default: bounds min). | ||
* | ||
* @param ref | ||
*/ | ||
export const shapesByProximity = | ||
(ref?: ReadonlyVec): ShapeOrdering => | ||
(shapes: IShape[]) => { | ||
ref = ref || __collBounds(shapes, bounds)![0]; | ||
return sortByCachedKey( | ||
shapes.slice(), | ||
(s) => distSq2(centroid(s) || ZERO2, ref!), | ||
compareNumAsc | ||
); | ||
}; |
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