Skip to content

Commit

Permalink
feat(geom-clip-line): add clipLineSegmentPoly()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jul 13, 2020
1 parent 1f38d92 commit bec7b93
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion packages/geom-clip-line/src/clip-poly.ts
@@ -1,4 +1,8 @@
import { intersectRayPolylineAll } from "@thi.ng/geom-isec";
import {
intersectLinePolylineAll,
intersectRayPolylineAll,
pointInPolygon2,
} from "@thi.ng/geom-isec";
import { direction, ReadonlyVec, Vec } from "@thi.ng/vectors";

/**
Expand All @@ -24,3 +28,23 @@ export const clipLinePoly = (
}
return segments;
};

export const clipLineSegmentPoly = (
a: ReadonlyVec,
b: ReadonlyVec,
pts: ReadonlyVec[]
) => {
const isecs = intersectLinePolylineAll(a, b, pts, true).isec;
const isAInside = pointInPolygon2(a, pts);
const isBInside = pointInPolygon2(b, pts);
if (!isecs) {
return isAInside && isBInside ? [[a, b]] : undefined;
}
isAInside && (<Vec[]>isecs).unshift(a);
isBInside && (<Vec[]>isecs).push(b);
const segments: Vec[][] = [];
for (let i = 0, n = isecs.length - 1; i < n; i += 2) {
segments.push([<Vec>isecs[i], <Vec>isecs[i + 1]]);
}
return segments;
};

0 comments on commit bec7b93

Please sign in to comment.