Skip to content

Commit

Permalink
refactor(geom-resample): update Sampler.closestPoint()
Browse files Browse the repository at this point in the history
- re-use closestPointPolyline()
  • Loading branch information
postspectacular committed Sep 18, 2020
1 parent 3167a3d commit 62472f5
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions packages/geom-resample/src/sampler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { closestPointSegment, closestT } from "@thi.ng/geom-closest-point";
import {
closestPointPolyline,
closestPointSegment,
closestT,
} from "@thi.ng/geom-closest-point";
import { fit01 } from "@thi.ng/math";
import {
dist,
Expand Down Expand Up @@ -59,20 +63,7 @@ export class Sampler {
}

closestPoint(p: ReadonlyVec) {
const pts = this.points;
const tmp: Vec = [];
const closest: Vec = [];
let minD = Infinity;
for (let i = 0, n = pts.length - 1; i < n; i++) {
if (closestPointSegment(p, pts[i], pts[i + 1], tmp)) {
const d = distSq(p, tmp);
if (d < minD) {
minD = d;
set(closest, tmp);
}
}
}
return minD < Infinity ? closest : undefined;
return closestPointPolyline(p, this.points);
}

closestT(p: ReadonlyVec) {
Expand Down

0 comments on commit 62472f5

Please sign in to comment.