-
-
Notifications
You must be signed in to change notification settings - Fork 151
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 closestPoint() impls for splines, line, polygons, pol…
…yline
- Loading branch information
1 parent
627e20d
commit eaf1a1b
Showing
2 changed files
with
151 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,68 @@ | ||
import { defmulti } from "@thi.ng/defmulti"; | ||
import { | ||
add2, | ||
add, | ||
normalize, | ||
ReadonlyVec, | ||
sub2, | ||
sub, | ||
Vec | ||
} from "@thi.ng/vectors3"; | ||
import { Circle, IShape, Type } from "../api"; | ||
import { | ||
Circle, | ||
Cubic, | ||
IShape, | ||
Line, | ||
PCLike, | ||
Quadratic, | ||
Type | ||
} from "../api"; | ||
import { | ||
closestPointArray, | ||
closestPointCubic, | ||
closestPointPolyline, | ||
closestPointQuadratic, | ||
closestPointSegment | ||
} from "../internal/closest-point"; | ||
import { dispatch } from "../internal/dispatch"; | ||
import { vertices } from "./vertices"; | ||
|
||
export const closestPoint = defmulti<IShape, ReadonlyVec, Vec>(dispatch); | ||
|
||
closestPoint.addAll({ | ||
|
||
[Type.CIRCLE]: | ||
($: Circle, p) => | ||
add2(null, normalize(null, sub2([], p, $.pos), $.r), $.pos), | ||
add(null, normalize(null, sub([], p, $.pos), $.r), $.pos), | ||
|
||
[Type.CUBIC]: | ||
({ points }: Cubic, p) => | ||
closestPointCubic(p, points[0], points[1], points[2], points[3]), | ||
|
||
[Type.LINE]: | ||
({ points }: Line, p) => | ||
closestPointSegment(p, points[0], points[1]), | ||
|
||
[Type.POLYGON]: | ||
($: PCLike, p) => | ||
closestPointArray(p, $.points), | ||
|
||
[Type.POLYGON]: | ||
($: PCLike, p) => | ||
closestPointPolyline(p, $.points, true), | ||
|
||
[Type.POLYLINE]: | ||
($: PCLike, p) => | ||
closestPointPolyline(p, $.points), | ||
|
||
[Type.QUADRATIC]: | ||
({ points }: Quadratic, p) => | ||
closestPointQuadratic(p, points[0], points[1], points[2]), | ||
|
||
[Type.RECT]: | ||
($, p) => | ||
closestPointPolyline(p, vertices($), true), | ||
|
||
}); | ||
|
||
closestPoint.isa(Type.QUAD, Type.POLYGON); | ||
closestPoint.isa(Type.SPHERE, Type.CIRCLE); | ||
closestPoint.isa(Type.TRIANGLE, Type.POLYGON); |