[skip ci] Refactor

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
feat/remove-ga
Mark Tolmacs 6 days ago
parent 24ddaeb737
commit e8abddeecf
No known key found for this signature in database

@ -70,13 +70,13 @@ test("unselected bound arrows update when rotating their target elements", async
expect(ellipseArrow.x).toEqual(0);
expect(ellipseArrow.y).toEqual(0);
expect(ellipseArrow.points[0]).toEqual([0, 0]);
expect(ellipseArrow.points[1][0]).toBeCloseTo(56.1, 1);
expect(ellipseArrow.points[1][1]).toBeCloseTo(116.85, 1);
expect(ellipseArrow.points[1][0]).toBeCloseTo(28.49, 1);
expect(ellipseArrow.points[1][1]).toBeCloseTo(155.9, 1);
expect(textArrow.endBinding?.elementId).toEqual(text.id);
expect(textArrow.x).toEqual(360);
expect(textArrow.y).toEqual(300);
expect(textArrow.points[0]).toEqual([0, 0]);
expect(textArrow.points[1][0]).toBeCloseTo(-89, 0);
expect(textArrow.points[1][1]).toBeCloseTo(-120.39, 0);
expect(textArrow.points[1][0]).toBeCloseTo(-95, 0);
expect(textArrow.points[1][1]).toBeCloseTo(-114.59, 0);
});

@ -1,6 +1,5 @@
import { line, lineSegmentIntersectionPoints, linesIntersectAt } from "./line";
import { line, linesIntersectAt } from "./line";
import { pointFrom } from "./point";
import { lineSegment } from "./segment";
describe("line-line intersections", () => {
it("should correctly detect intersection at origin", () => {
@ -30,22 +29,3 @@ describe("line-line intersections", () => {
).toBe(null);
});
});
describe("line-segment intersections", () => {
it("should correctly detect intersection", () => {
expect(
lineSegmentIntersectionPoints(
lineSegment(pointFrom(0, 0), pointFrom(5, 0)),
lineSegment(pointFrom(2, -2), pointFrom(3, 2)),
),
).toEqual(pointFrom(2.5, 0));
});
it("should correctly detect non-intersection", () => {
expect(
lineSegmentIntersectionPoints(
lineSegment(pointFrom(0, 0), pointFrom(5, 0)),
lineSegment(pointFrom(3, 1), pointFrom(4, 4)),
),
).toEqual(null);
});
});

@ -6,14 +6,7 @@ import {
pointRotateRads,
pointsEqual,
} from "./point";
import { pointOnLineSegment } from "./segment";
import type {
GlobalPoint,
Line,
LineSegment,
LocalPoint,
Radians,
} from "./types";
import type { GlobalPoint, Line, LocalPoint, Radians } from "./types";
import { vectorCross, vectorFromPoint } from "./vector";
/**
@ -100,28 +93,6 @@ export function linesIntersectAt<Point extends GlobalPoint | LocalPoint>(
return null;
}
/**
* Returns the intersection point of a segment and a line
*
* @param l
* @param s
* @returns
*/
export function lineSegmentIntersectionPoints<
Point extends GlobalPoint | LocalPoint,
>(l: LineSegment<Point>, s: LineSegment<Point>): Point | null {
const candidate = linesIntersectAt(line(l[0], l[1]), line(s[0], s[1]));
if (
!candidate ||
!pointOnLineSegment(candidate, s) ||
!pointOnLineSegment(candidate, l)
) {
return null;
}
return candidate;
}
export function isPointOnLine<P extends GlobalPoint | LocalPoint>(
l: Line<P>,
p: P,

@ -1,7 +1,11 @@
import { invariant } from "../excalidraw/utils";
import { line, lineSegmentIntersectionPoints, linesIntersectAt } from "./line";
import { line, linesIntersectAt } from "./line";
import { pointFrom } from "./point";
import { distanceToLineSegment, lineSegment } from "./segment";
import {
distanceToLineSegment,
lineSegment,
lineSegmentIntersectionPoints,
} from "./segment";
import type {
GlobalPoint,
Line,

@ -0,0 +1,21 @@
import { pointFrom } from "./point";
import { lineSegment, lineSegmentIntersectionPoints } from "./segment";
describe("line-segment intersections", () => {
it("should correctly detect intersection", () => {
expect(
lineSegmentIntersectionPoints(
lineSegment(pointFrom(0, 0), pointFrom(5, 0)),
lineSegment(pointFrom(2, -2), pointFrom(3, 2)),
),
).toEqual(pointFrom(2.5, 0));
});
it("should correctly detect non-intersection", () => {
expect(
lineSegmentIntersectionPoints(
lineSegment(pointFrom(0, 0), pointFrom(5, 0)),
lineSegment(pointFrom(3, 1), pointFrom(4, 4)),
),
).toEqual(null);
});
});

@ -1,3 +1,4 @@
import { line, linesIntersectAt } from "./line";
import {
isPoint,
pointCenter,
@ -156,3 +157,26 @@ export const distanceToLineSegment = <Point extends LocalPoint | GlobalPoint>(
const dy = y - yy;
return Math.sqrt(dx * dx + dy * dy);
};
/**
* Returns the intersection point of a segment and a line
*
* @param l
* @param s
* @returns
*/
export function lineSegmentIntersectionPoints<
Point extends GlobalPoint | LocalPoint,
>(l: LineSegment<Point>, s: LineSegment<Point>): Point | null {
const candidate = linesIntersectAt(line(l[0], l[1]), line(s[0], s[1]));
if (
!candidate ||
!pointOnLineSegment(candidate, s) ||
!pointOnLineSegment(candidate, l)
) {
return null;
}
return candidate;
}

Loading…
Cancel
Save