Master merge

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
This commit is contained in:
Mark Tolmacs 2024-10-02 12:09:01 +02:00
parent b4d8b04d9e
commit 336fa9d002
No known key found for this signature in database
21 changed files with 189 additions and 157 deletions

View file

@ -1,12 +1,12 @@
import { cartesian2Polar, polar, radians } from "./angle";
import { point } from "./point";
import { pointFrom } from "./point";
describe("cartesian to polar coordinate conversion", () => {
it("converts values properly", () => {
expect(cartesian2Polar(point(12, 5))).toEqual(
expect(cartesian2Polar(pointFrom(12, 5))).toEqual(
polar(13, radians(Math.atan(5 / 12))),
);
expect(cartesian2Polar(point(5, 5))).toEqual(
expect(cartesian2Polar(pointFrom(5, 5))).toEqual(
polar(5 * Math.sqrt(2), radians(Math.PI / 4)),
);
});

View file

@ -5,7 +5,7 @@ import {
ellipseLineIntersectionPoints,
ellipseSegmentInterceptPoints,
} from "./ellipse";
import { point, pointDistance } from "./point";
import { pointFrom, pointDistance } from "./point";
import type { GenericPoint, Segment, Radians, Arc, Line } from "./types";
import { PRECISION } from "./utils";
@ -37,7 +37,7 @@ export function arcIncludesPoint<P extends GenericPoint>(
p: P,
): boolean {
const [radius, angle] = cartesian2Polar(
point(p[0] - center[0], p[1] - center[1]),
pointFrom(p[0] - center[0], p[1] - center[1]),
);
return startAngle < endAngle
@ -69,14 +69,14 @@ export function arcDistanceFromPoint<Point extends GenericPoint>(
return Math.min(
pointDistance(
p,
point(
pointFrom(
a.center[0] + a.radius + Math.cos(a.startAngle),
a.center[1] + a.radius + Math.sin(a.startAngle),
),
),
pointDistance(
p,
point(
pointFrom(
a.center[0] + a.radius + Math.cos(a.endAngle),
a.center[1] + a.radius + Math.sin(a.endAngle),
),
@ -97,7 +97,7 @@ export function arcSegmentInterceptPoints<Point extends GenericPoint>(
s,
).filter((candidate) => {
const [candidateRadius, candidateAngle] = cartesian2Polar(
point(candidate[0] - a.center[0], candidate[1] - a.center[1]),
pointFrom(candidate[0] - a.center[0], candidate[1] - a.center[1]),
);
return a.startAngle < a.endAngle
@ -125,7 +125,7 @@ export function arcLineInterceptPoints<Point extends GenericPoint>(
l,
).filter((candidate) => {
const [candidateRadius, candidateAngle] = cartesian2Polar(
point(candidate[0] - a.center[0], candidate[1] - a.center[1]),
pointFrom(candidate[0] - a.center[0], candidate[1] - a.center[1]),
);
return a.startAngle < a.endAngle

View file

@ -6,105 +6,121 @@ import {
ellipseLineIntersectionPoints,
} from "./ellipse";
import { line } from "./line";
import { point } from "./point";
import { pointFrom } from "./point";
import { segment } from "./segment";
import type { Ellipse, GlobalPoint } from "./types";
describe("point and ellipse", () => {
it("point on ellipse", () => {
const target: Ellipse<GlobalPoint> = ellipse(point(1, 2), 2, 1);
[point(1, 3), point(1, 1), point(3, 2), point(-1, 2)].forEach((p) => {
const target: Ellipse<GlobalPoint> = ellipse(pointFrom(1, 2), 2, 1);
[
pointFrom(1, 3),
pointFrom(1, 1),
pointFrom(3, 2),
pointFrom(-1, 2),
].forEach((p) => {
expect(ellipseTouchesPoint(p, target)).toBe(true);
});
expect(ellipseTouchesPoint(point(-0.4, 2.7), target, 0.1)).toBe(true);
expect(ellipseTouchesPoint(point(-0.4, 2.71), target, 0.01)).toBe(true);
expect(ellipseTouchesPoint(pointFrom(-0.4, 2.7), target, 0.1)).toBe(true);
expect(ellipseTouchesPoint(pointFrom(-0.4, 2.71), target, 0.01)).toBe(true);
expect(ellipseTouchesPoint(point(2.4, 2.7), target, 0.1)).toBe(true);
expect(ellipseTouchesPoint(point(2.4, 2.71), target, 0.01)).toBe(true);
expect(ellipseTouchesPoint(pointFrom(2.4, 2.7), target, 0.1)).toBe(true);
expect(ellipseTouchesPoint(pointFrom(2.4, 2.71), target, 0.01)).toBe(true);
expect(ellipseTouchesPoint(point(2, 1.14), target, 0.1)).toBe(true);
expect(ellipseTouchesPoint(point(2, 1.14), target, 0.01)).toBe(true);
expect(ellipseTouchesPoint(pointFrom(2, 1.14), target, 0.1)).toBe(true);
expect(ellipseTouchesPoint(pointFrom(2, 1.14), target, 0.01)).toBe(true);
expect(ellipseTouchesPoint(point(0, 1.14), target, 0.1)).toBe(true);
expect(ellipseTouchesPoint(point(0, 1.14), target, 0.01)).toBe(true);
expect(ellipseTouchesPoint(pointFrom(0, 1.14), target, 0.1)).toBe(true);
expect(ellipseTouchesPoint(pointFrom(0, 1.14), target, 0.01)).toBe(true);
expect(ellipseTouchesPoint(point(0, 2.8), target)).toBe(false);
expect(ellipseTouchesPoint(point(2, 1.2), target)).toBe(false);
expect(ellipseTouchesPoint(pointFrom(0, 2.8), target)).toBe(false);
expect(ellipseTouchesPoint(pointFrom(2, 1.2), target)).toBe(false);
});
it("point in ellipse", () => {
const target: Ellipse<GlobalPoint> = ellipse(point(0, 0), 2, 1);
[point(0, 1), point(0, -1), point(2, 0), point(-2, 0)].forEach((p) => {
const target: Ellipse<GlobalPoint> = ellipse(pointFrom(0, 0), 2, 1);
[
pointFrom(0, 1),
pointFrom(0, -1),
pointFrom(2, 0),
pointFrom(-2, 0),
].forEach((p) => {
expect(ellipseIncludesPoint(p, target)).toBe(true);
});
expect(ellipseIncludesPoint(point(-1, 0.8), target)).toBe(true);
expect(ellipseIncludesPoint(point(1, -0.8), target)).toBe(true);
expect(ellipseIncludesPoint(pointFrom(-1, 0.8), target)).toBe(true);
expect(ellipseIncludesPoint(pointFrom(1, -0.8), target)).toBe(true);
// Point on outline
expect(ellipseIncludesPoint(point(2, 0), target)).toBe(true);
expect(ellipseIncludesPoint(pointFrom(2, 0), target)).toBe(true);
expect(ellipseIncludesPoint(point(-1, 1), target)).toBe(false);
expect(ellipseIncludesPoint(point(-1.4, 0.8), target)).toBe(false);
expect(ellipseIncludesPoint(pointFrom(-1, 1), target)).toBe(false);
expect(ellipseIncludesPoint(pointFrom(-1.4, 0.8), target)).toBe(false);
});
});
describe("segment and ellipse", () => {
it("detects outside segment", () => {
const e = ellipse(point(0, 0), 2, 2);
const e = ellipse(pointFrom(0, 0), 2, 2);
expect(
ellipseSegmentInterceptPoints(
e,
segment<GlobalPoint>(point(-100, 0), point(-10, 0)),
segment<GlobalPoint>(pointFrom(-100, 0), pointFrom(-10, 0)),
),
).toEqual([]);
expect(
ellipseSegmentInterceptPoints(
e,
segment<GlobalPoint>(point(-10, 0), point(10, 0)),
segment<GlobalPoint>(pointFrom(-10, 0), pointFrom(10, 0)),
),
).toEqual([point(-2, 0), point(2, 0)]);
).toEqual([pointFrom(-2, 0), pointFrom(2, 0)]);
expect(
ellipseSegmentInterceptPoints(
e,
segment<GlobalPoint>(point(-10, -2), point(10, -2)),
segment<GlobalPoint>(pointFrom(-10, -2), pointFrom(10, -2)),
),
).toEqual([point(0, -2)]);
).toEqual([pointFrom(0, -2)]);
expect(
ellipseSegmentInterceptPoints(
e,
segment<GlobalPoint>(point(0, -1), point(0, 1)),
segment<GlobalPoint>(pointFrom(0, -1), pointFrom(0, 1)),
),
).toEqual([]);
});
});
describe("line and ellipse", () => {
const e = ellipse(point(0, 0), 2, 2);
const e = ellipse(pointFrom(0, 0), 2, 2);
it("detects outside line", () => {
expect(
ellipseLineIntersectionPoints(
e,
line<GlobalPoint>(point(-10, -10), point(10, -10)),
line<GlobalPoint>(pointFrom(-10, -10), pointFrom(10, -10)),
),
).toEqual([]);
});
it("detects line intersecting ellipse", () => {
expect(
ellipseLineIntersectionPoints(e, line<GlobalPoint>(point(0, -1), point(0, 1))),
).toEqual([point(0, 2), point(0, -2)]);
ellipseLineIntersectionPoints(
e,
line<GlobalPoint>(pointFrom(0, -1), pointFrom(0, 1)),
),
).toEqual([pointFrom(0, 2), pointFrom(0, -2)]);
expect(
ellipseLineIntersectionPoints(
e,
line<GlobalPoint>(point(-100, 0), point(-10, 0)),
).map(([x, y]) => point(Math.round(x), Math.round(y))),
).toEqual([point(2, 0), point(-2, 0)]);
line<GlobalPoint>(pointFrom(-100, 0), pointFrom(-10, 0)),
).map(([x, y]) => pointFrom(Math.round(x), Math.round(y))),
).toEqual([pointFrom(2, 0), pointFrom(-2, 0)]);
});
it("detects line touching ellipse", () => {
expect(
ellipseLineIntersectionPoints(e, line<GlobalPoint>(point(-2, -2), point(2, -2))),
).toEqual([point(0, -2)]);
ellipseLineIntersectionPoints(
e,
line<GlobalPoint>(pointFrom(-2, -2), pointFrom(2, -2)),
),
).toEqual([pointFrom(0, -2)]);
});
});

View file

@ -1,4 +1,9 @@
import { point, pointDistance, pointFromVector, pointsEqual } from "./point";
import {
pointFrom,
pointDistance,
pointFromVector,
pointsEqual,
} from "./point";
import type { Ellipse, GenericPoint, Line, Segment } from "./types";
import { PRECISION } from "./utils";
import {
@ -119,7 +124,7 @@ export const ellipseDistanceFromPoint = <Point extends GenericPoint>(
b * ty * Math.sign(translatedPoint[1]),
];
return pointDistance(pointFromVector(translatedPoint), point(minX, minY));
return pointDistance(pointFromVector(translatedPoint), pointFrom(minX, minY));
};
/**
@ -151,7 +156,7 @@ export function ellipseSegmentInterceptPoints<Point extends GenericPoint>(
if (0 <= t_a && t_a <= 1) {
intersections.push(
point(
pointFrom(
s[0][0] + (s[1][0] - s[0][0]) * t_a,
s[0][1] + (s[1][1] - s[0][1]) * t_a,
),
@ -160,7 +165,7 @@ export function ellipseSegmentInterceptPoints<Point extends GenericPoint>(
if (0 <= t_b && t_b <= 1) {
intersections.push(
point(
pointFrom(
s[0][0] + (s[1][0] - s[0][0]) * t_b,
s[0][1] + (s[1][1] - s[0][1]) * t_b,
),
@ -170,7 +175,7 @@ export function ellipseSegmentInterceptPoints<Point extends GenericPoint>(
const t = -b / a;
if (0 <= t && t <= 1) {
intersections.push(
point(
pointFrom(
s[0][0] + (s[1][0] - s[0][0]) * t,
s[0][1] + (s[1][1] - s[0][1]) * t,
),
@ -204,8 +209,8 @@ export function ellipseLineIntersectionPoints<Point extends GenericPoint>(
const t1 = (-b + Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
const t2 = (-b - Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
const candidates = [
point<Point>(x1 + t1 * (x2 - x1) + cx, y1 + t1 * (y2 - y1) + cy),
point<Point>(x1 + t2 * (x2 - x1) + cx, y1 + t2 * (y2 - y1) + cy),
pointFrom<Point>(x1 + t1 * (x2 - x1) + cx, y1 + t1 * (y2 - y1) + cy),
pointFrom<Point>(x1 + t2 * (x2 - x1) + cx, y1 + t2 * (y2 - y1) + cy),
].filter((p) => !isNaN(p[0]) && !isNaN(p[1]));
if (candidates.length === 2 && pointsEqual(candidates[0], candidates[1])) {

View file

@ -1,31 +1,35 @@
import { line, lineLineIntersectionPoint, lineSegmentIntersectionPoints } from "./line";
import { point } from "./point";
import {
line,
lineLineIntersectionPoint,
lineSegmentIntersectionPoints,
} from "./line";
import { pointFrom } from "./point";
import { segment } from "./segment";
describe("line-line intersections", () => {
it("should correctly detect intersection at origin", () => {
expect(
lineLineIntersectionPoint(
line(point(-5, -5), point(5, 5)),
line(point(5, -5), point(-5, 5)),
line(pointFrom(-5, -5), pointFrom(5, 5)),
line(pointFrom(5, -5), pointFrom(-5, 5)),
),
).toEqual(point(0, 0));
).toEqual(pointFrom(0, 0));
});
it("should correctly detect intersection at non-origin", () => {
expect(
lineLineIntersectionPoint(
line(point(0, 0), point(10, 10)),
line(point(10, 0), point(0, 10)),
line(pointFrom(0, 0), pointFrom(10, 10)),
line(pointFrom(10, 0), pointFrom(0, 10)),
),
).toEqual(point(5, 5));
).toEqual(pointFrom(5, 5));
});
it("should correctly detect parallel lines", () => {
expect(
lineLineIntersectionPoint(
line(point(0, 0), point(0, 10)),
line(point(10, 0), point(10, 10)),
line(pointFrom(0, 0), pointFrom(0, 10)),
line(pointFrom(10, 0), pointFrom(10, 10)),
),
).toBe(null);
});
@ -35,16 +39,16 @@ describe("line-segment intersections", () => {
it("should correctly detect intersection", () => {
expect(
lineSegmentIntersectionPoints(
line(point(0, 0), point(5, 0)),
segment(point(2, -2), point(3, 2)),
line(pointFrom(0, 0), pointFrom(5, 0)),
segment(pointFrom(2, -2), pointFrom(3, 2)),
),
).toEqual(point(2.5, -0));
).toEqual(pointFrom(2.5, -0));
});
it("should correctly detect non-intersection", () => {
expect(
lineSegmentIntersectionPoints(
line(point(0, 0), point(5, 0)),
segment(point(3, 1), point(4, 4)),
line(pointFrom(0, 0), pointFrom(5, 0)),
segment(pointFrom(3, 1), pointFrom(4, 4)),
),
).toEqual(null);
});

View file

@ -1,5 +1,5 @@
import { ellipseLineIntersectionPoints } from "./ellipse";
import { point, pointCenter, pointRotateRads } from "./point";
import { pointFrom, pointCenter, pointRotateRads } from "./point";
import { segmentIncludesPoint } from "./segment";
import type { GenericPoint, Line, Radians, Segment } from "./types";
@ -73,7 +73,7 @@ export function lineLineIntersectionPoint<Point extends GenericPoint>(
const xnum = a * (x3 - x4) - (x1 - x2) * c;
const ynum = a * (y3 - y4) - (y1 - y2) * c;
return point<Point>(xnum / den, ynum / den);
return pointFrom<Point>(xnum / den, ynum / den);
}
/**

View file

@ -1,38 +1,38 @@
import { point } from "./point";
import { pointFrom } from "./point";
import { rectangle, rectangleDistanceFromPoint } from "./rectangle";
describe("rectangle distance", () => {
it("finds the shortest distance", () => {
expect(
rectangleDistanceFromPoint(
rectangle(point(-1, -1), point(1, 1)),
point(2, 0),
rectangle(pointFrom(-1, -1), pointFrom(1, 1)),
pointFrom(2, 0),
),
).toBe(1);
expect(
rectangleDistanceFromPoint(
rectangle(point(-1, -1), point(1, 1)),
point(0, 2),
rectangle(pointFrom(-1, -1), pointFrom(1, 1)),
pointFrom(0, 2),
),
).toBe(1);
expect(
rectangleDistanceFromPoint(
rectangle(point(-1, -1), point(1, 1)),
point(-2, 0),
rectangle(pointFrom(-1, -1), pointFrom(1, 1)),
pointFrom(-2, 0),
),
).toBe(1);
expect(
rectangleDistanceFromPoint(
rectangle(point(-1, -1), point(1, 1)),
point(0, -2),
rectangle(pointFrom(-1, -1), pointFrom(1, 1)),
pointFrom(0, -2),
),
).toBe(1);
});
it("finds the corner as closest point", () => {
expect(
rectangleDistanceFromPoint(
rectangle(point(-1, -1), point(1, 1)),
point(2, 2),
rectangle(pointFrom(-1, -1), pointFrom(1, 1)),
pointFrom(2, 2),
),
).toBe(Math.sqrt(2));
});

View file

@ -1,5 +1,5 @@
import { invariant } from "../excalidraw/utils";
import { point } from "./point";
import { pointFrom } from "./point";
import { segment, segmentDistanceToPoint } from "./segment";
import type { GenericPoint, Rectangle } from "./types";
@ -32,10 +32,10 @@ export function rectangleDistanceFromPoint<Point extends GenericPoint>(
p: Point,
): number {
const sides = [
segment(point(r[0][0], r[0][1]), point(r[1][0], r[0][1])),
segment(point(r[1][0], r[0][1]), point(r[1][0], r[1][1])),
segment(point(r[1][0], r[1][1]), point(r[0][0], r[1][1])),
segment(point(r[0][0], r[1][1]), point(r[0][0], r[0][1])),
segment(pointFrom(r[0][0], r[0][1]), pointFrom(r[1][0], r[0][1])),
segment(pointFrom(r[1][0], r[0][1]), pointFrom(r[1][0], r[1][1])),
segment(pointFrom(r[1][0], r[1][1]), pointFrom(r[0][0], r[1][1])),
segment(pointFrom(r[0][0], r[1][1]), pointFrom(r[0][0], r[0][1])),
];
return Math.min(...sides.map((side) => segmentDistanceToPoint(p, side)));

View file

@ -1,15 +1,15 @@
import { point } from "./point";
import { pointFrom } from "./point";
import { segment, segmentsIntersectAt } from "./segment";
import type { GlobalPoint, Segment } from "./types";
describe("segment intersects segment", () => {
const lineA: Segment<GlobalPoint> = segment(point(1, 4), point(3, 4));
const lineB: Segment<GlobalPoint> = segment(point(2, 1), point(2, 7));
const lineC: Segment<GlobalPoint> = segment(point(1, 8), point(3, 8));
const lineD: Segment<GlobalPoint> = segment(point(1, 8), point(3, 8));
const lineE: Segment<GlobalPoint> = segment(point(1, 9), point(3, 9));
const lineF: Segment<GlobalPoint> = segment(point(1, 2), point(3, 4));
const lineG: Segment<GlobalPoint> = segment(point(0, 1), point(2, 3));
const lineA: Segment<GlobalPoint> = segment(pointFrom(1, 4), pointFrom(3, 4));
const lineB: Segment<GlobalPoint> = segment(pointFrom(2, 1), pointFrom(2, 7));
const lineC: Segment<GlobalPoint> = segment(pointFrom(1, 8), pointFrom(3, 8));
const lineD: Segment<GlobalPoint> = segment(pointFrom(1, 8), pointFrom(3, 8));
const lineE: Segment<GlobalPoint> = segment(pointFrom(1, 9), pointFrom(3, 9));
const lineF: Segment<GlobalPoint> = segment(pointFrom(1, 2), pointFrom(3, 4));
const lineG: Segment<GlobalPoint> = segment(pointFrom(0, 1), pointFrom(2, 3));
it("intersection", () => {
expect(segmentsIntersectAt(lineA, lineB)).toEqual([2, 4]);