Skip to content

Commit

Permalink
feat(geom-isec): add intersectLinePolylineAll()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jul 13, 2020
1 parent 817b54d commit 1f38d92
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/geom-isec/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from "./point";

export * from "./circle-circle";
export * from "./line-line";
export * from "./line-poly";
export * from "./plane-plane";
export * from "./ray-circle";
export * from "./ray-line";
Expand Down
37 changes: 37 additions & 0 deletions packages/geom-isec/src/line-poly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { IntersectionResult, IntersectionType } from "@thi.ng/geom-api";
import { maddN2, mag, normalize, ReadonlyVec, sub, Vec } from "@thi.ng/vectors";
import { NONE } from "./api";
import { intersectRayLine } from "./ray-line";

export const intersectLinePolylineAll = (
a: ReadonlyVec,
b: ReadonlyVec,
pts: ReadonlyVec[],
closed = false
): IntersectionResult => {
const dir = sub([], b, a);
const maxD = mag(dir);
normalize(null, dir);
const n = pts.length - 1;
let i, j;
if (closed) {
i = pts[n];
j = pts[0];
} else {
i = pts[0];
j = pts[1];
}
const res: [number, Vec][] = [];
for (let k = 0; k <= n; i = j, j = pts[++k]) {
const d = intersectRayLine(a, dir, i, j).alpha;
if (d !== undefined && d >= 0 && d <= maxD) {
res.push([d, maddN2([], dir, d, a)]);
}
}
return res.length
? {
type: IntersectionType.INTERSECT,
isec: res.sort((a, b) => a[0] - b[0]).map((x) => x[1]),
}
: NONE;
};

0 comments on commit 1f38d92

Please sign in to comment.