Skip to content

Commit

Permalink
fix(geom-isec): fix #84, update pointInSegment, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed May 22, 2019
1 parent 40a8bff commit 2bef312
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
15 changes: 8 additions & 7 deletions packages/geom-isec/src/point.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { EPS, sign } from "@thi.ng/math";
import { closestT } from "@thi.ng/geom-closest-point";
import { clamp01, EPS, sign } from "@thi.ng/math";
import {
clockwise2,
distSq,
magSq,
mixN,
ReadonlyVec,
signedArea2
} from "@thi.ng/vectors";

export const pointInSegment2 = (
export const pointInSegment = (
p: ReadonlyVec,
a: ReadonlyVec,
b: ReadonlyVec,
eps = EPS
) => {
const ax = a[0];
const ay = a[1];
const tx = (p[0] - ax) / (b[0] - ax);
const ty = (p[1] - ay) / (b[1] - ay);
return Math.abs(tx - ty) <= eps && !(tx < 0 || tx > 1 || ty < 0 || ty > 1);
const t = closestT(p, a, b);
return t !== undefined
? distSq(p, mixN([], a, b, clamp01(t))) < eps * eps
: false;
};

export const pointInCircle = (p: ReadonlyVec, pos: ReadonlyVec, r: number) =>
Expand Down
6 changes: 0 additions & 6 deletions packages/geom-isec/test/index.ts

This file was deleted.

30 changes: 30 additions & 0 deletions packages/geom-isec/test/point-segment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as assert from "assert";
import { pointInSegment } from "../src";

describe("pointInSegment", () => {
it("2d", () => {
assert(pointInSegment([0, 0], [-10, -10], [10, 10]), "1");
assert(pointInSegment([-5, -5], [-10, -10], [10, 10]), "2");
assert(pointInSegment([5, 5], [-10, -10], [10, 10]), "3");
assert(!pointInSegment([5, 5.01], [-10, -10], [10, 10]), "4");
assert(pointInSegment([5, 5.01], [-10, -10], [10, 10], 0.01), "4.1");
assert(pointInSegment([5, 4.99], [-10, -10], [10, 10], 0.01), "4.2");
assert(!pointInSegment([5, 5.02], [-10, -10], [10, 10], 0.01), "4.3");
assert(!pointInSegment([5, 4.98], [-10, -10], [10, 10], 0.01), "4.4");
});

it("2d axis aligned", () => {
assert(pointInSegment([9, 10], [5, 10], [10, 10]), "1");
assert(pointInSegment([9, 10], [10, 10], [5, 10]), "1.1");
assert(pointInSegment([10, 9], [10, 5], [10, 10]), "2");
assert(pointInSegment([10, 9], [10, 10], [10, 5]), "2.1");
assert(pointInSegment([4.9, 10], [5, 10], [10, 10], 0.1), "3");
assert(!pointInSegment([4.89, 10], [5, 10], [10, 10], 0.1), "3.1");
assert(pointInSegment([10.1, 10], [5, 10], [10, 10], 0.1), "3.2");
assert(!pointInSegment([10.11, 10], [5, 10], [10, 10], 0.1), "3.3");
assert(pointInSegment([9, 10.1], [5, 10], [10, 10], 0.1), "4");
assert(!pointInSegment([9, 10.11], [5, 10], [10, 10], 0.1), "4.1");
assert(pointInSegment([9, 9.9], [5, 10], [10, 10], 0.1), "4.2");
assert(!pointInSegment([9, 9.89], [5, 10], [10, 10], 0.1), "4.3");
});
});

0 comments on commit 2bef312

Please sign in to comment.