More refactor

This commit is contained in:
Mark Tolmacs 2024-09-25 11:59:27 +02:00
parent 392dd5b0b8
commit b697f63cad
No known key found for this signature in database
18 changed files with 206 additions and 199 deletions

View file

@ -2,8 +2,8 @@ import type { Curve, Degrees, GlobalPoint } from "../math";
import {
curve,
degreesToRadians,
lineSegment,
lineSegmentRotate,
segment,
segmentRotate,
point,
pointRotateDegs,
} from "../math";
@ -34,10 +34,10 @@ describe("point and curve", () => {
describe("point and polylines", () => {
const polyline: Polyline<GlobalPoint> = [
lineSegment(point(1, 0), point(1, 2)),
lineSegment(point(1, 2), point(2, 2)),
lineSegment(point(2, 2), point(2, 1)),
lineSegment(point(2, 1), point(3, 1)),
segment(point(1, 0), point(1, 2)),
segment(point(1, 2), point(2, 2)),
segment(point(2, 2), point(2, 1)),
segment(point(2, 1), point(3, 1)),
];
it("point on the line", () => {
@ -68,7 +68,7 @@ describe("point and polylines", () => {
const rotation = (Math.random() * 360) as Degrees;
const rotatedPoint = pointRotateDegs(p, point(0, 0), rotation);
const rotatedPolyline = polyline.map((line) =>
lineSegmentRotate(line, degreesToRadians(rotation), point(0, 0)),
segmentRotate(line, degreesToRadians(rotation), point(0, 0)),
);
expect(pointOnPolyline(rotatedPoint, rotatedPolyline)).toBe(true);
});
@ -79,7 +79,7 @@ describe("point and polylines", () => {
const rotation = (Math.random() * 360) as Degrees;
const rotatedPoint = pointRotateDegs(p, point(0, 0), rotation);
const rotatedPolyline = polyline.map((line) =>
lineSegmentRotate(line, degreesToRadians(rotation), point(0, 0)),
segmentRotate(line, degreesToRadians(rotation), point(0, 0)),
);
expect(pointOnPolyline(rotatedPoint, rotatedPolyline)).toBe(false);
});

View file

@ -2,14 +2,14 @@ import type { Polycurve, Polyline } from "./geometry/shape";
import { type GeometricShape } from "./geometry/shape";
import type { Curve, GenericPoint } from "../math";
import {
lineSegment,
segment,
point,
polygonIncludesPoint,
pointOnLineSegment,
segmentIncludesPoint,
pointOnPolygon,
polygonFromPoints,
pointOnEllipse,
pointInEllipse,
ellipseTouchesPoint,
ellipseIncludesPoint,
} from "../math";
// check if the given point is considered on the given shape's border
@ -22,9 +22,9 @@ export const isPointOnShape = <Point extends GenericPoint>(
case "polygon":
return pointOnPolygon(point, shape.data, tolerance);
case "ellipse":
return pointOnEllipse(point, shape.data, tolerance);
return ellipseTouchesPoint(point, shape.data, tolerance);
case "line":
return pointOnLineSegment(point, shape.data, tolerance);
return segmentIncludesPoint(point, shape.data, tolerance);
case "polyline":
return pointOnPolyline(point, shape.data, tolerance);
case "curve":
@ -49,7 +49,7 @@ export const isPointInShape = <Point extends GenericPoint>(
case "curve":
return false;
case "ellipse":
return pointInEllipse(p, shape.data);
return ellipseIncludesPoint(p, shape.data);
case "polyline": {
const polygon = polygonFromPoints(shape.data.flat());
return polygonIncludesPoint(p, polygon);
@ -96,7 +96,7 @@ const polyLineFromCurve = <Point extends GenericPoint>(
t += increment;
if (t <= 1) {
const nextPoint: Point = point(equation(t, 0), equation(t, 1));
lineSegments.push(lineSegment(startingPoint, nextPoint));
lineSegments.push(segment(startingPoint, nextPoint));
startingPoint = nextPoint;
}
}
@ -117,5 +117,5 @@ export const pointOnPolyline = <Point extends GenericPoint>(
polyline: Polyline<Point>,
threshold = 10e-5,
) => {
return polyline.some((line) => pointOnLineSegment(point, line, threshold));
return polyline.some((line) => segmentIncludesPoint(point, line, threshold));
};

View file

@ -1,20 +1,20 @@
import type { GlobalPoint, LineSegment, Polygon } from "../../math";
import type { GlobalPoint, Segment, Polygon } from "../../math";
import {
point,
lineSegment,
segment,
polygon,
pointOnLineSegment,
segmentIncludesPoint,
pointOnPolygon,
polygonIncludesPoint,
} from "../../math";
describe("point and line", () => {
const s: LineSegment<GlobalPoint> = lineSegment(point(1, 0), point(1, 2));
const s: Segment<GlobalPoint> = segment(point(1, 0), point(1, 2));
it("point on the line", () => {
expect(pointOnLineSegment(point(0, 1), s)).toBe(false);
expect(pointOnLineSegment(point(1, 1), s, 0)).toBe(true);
expect(pointOnLineSegment(point(2, 1), s)).toBe(false);
expect(segmentIncludesPoint(point(0, 1), s)).toBe(false);
expect(segmentIncludesPoint(point(1, 1), s, 0)).toBe(true);
expect(segmentIncludesPoint(point(2, 1), s)).toBe(false);
});
});

View file

@ -16,7 +16,7 @@ import type {
Curve,
Ellipse,
GenericPoint,
LineSegment,
Segment,
Polygon,
Radians,
ViewportPoint,
@ -24,7 +24,7 @@ import type {
import {
curve,
ellipse,
lineSegment,
segment,
point,
pointFromArray,
pointFromVector,
@ -63,7 +63,7 @@ import { invariant } from "../../excalidraw/utils";
// this corresponds to a straight line element in the editor but it could also
// be used to model other elements
export type Polyline<Point extends GlobalPoint | LocalPoint | ViewportPoint> =
LineSegment<Point>[];
Segment<Point>[];
// a polycurve is a curve consisting of ther curves, this corresponds to a complex
// curve on the canvas
@ -73,7 +73,7 @@ export type Polycurve<Point extends GlobalPoint | LocalPoint | ViewportPoint> =
export type GeometricShape<Point extends GenericPoint> =
| {
type: "line";
data: LineSegment<Point>;
data: Segment<Point>;
}
| {
type: "polygon";
@ -242,11 +242,11 @@ const polylineFromPoints = <
points: Point[],
): Polyline<Point> => {
let previousPoint: Point = points[0];
const polyline: LineSegment<Point>[] = [];
const polyline: Segment<Point>[] = [];
for (let i = 1; i < points.length; i++) {
const nextPoint = points[i];
polyline.push(lineSegment<Point>(previousPoint, nextPoint));
polyline.push(segment<Point>(previousPoint, nextPoint));
previousPoint = nextPoint;
}
@ -356,7 +356,7 @@ export const segmentIntersectRectangleElement = <
Point extends LocalPoint | GlobalPoint,
>(
element: ExcalidrawBindableElement,
segment: LineSegment<Point>,
s: Segment<Point>,
gap: number = 0,
): Point[] => {
const bounds = [
@ -371,23 +371,23 @@ export const segmentIntersectRectangleElement = <
);
return [
lineSegment(
segment(
pointRotateRads(point(bounds[0], bounds[1]), center, element.angle),
pointRotateRads(point(bounds[2], bounds[1]), center, element.angle),
),
lineSegment(
segment(
pointRotateRads(point(bounds[2], bounds[1]), center, element.angle),
pointRotateRads(point(bounds[2], bounds[3]), center, element.angle),
),
lineSegment(
segment(
pointRotateRads(point(bounds[2], bounds[3]), center, element.angle),
pointRotateRads(point(bounds[0], bounds[3]), center, element.angle),
),
lineSegment(
segment(
pointRotateRads(point(bounds[0], bounds[3]), center, element.angle),
pointRotateRads(point(bounds[0], bounds[1]), center, element.angle),
),
]
.map((s) => segmentsIntersectAt(segment, s))
.map((l) => segmentsIntersectAt(s, l))
.filter((i): i is Point => !!i);
};