mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
First implementation of element distance functions with failing tests
This commit is contained in:
parent
47cc842415
commit
d9ea7190ec
6 changed files with 275 additions and 173 deletions
|
@ -1,6 +1,8 @@
|
|||
import { invariant } from "../excalidraw/utils";
|
||||
import { cartesian2Polar, radians } from "./angle";
|
||||
import { ellipse, ellipseSegmentInterceptPoints } from "./ellipse";
|
||||
import { point } from "./point";
|
||||
import { point, pointDistance } from "./point";
|
||||
import { segment } from "./segment";
|
||||
import type { GenericPoint, Segment, Radians, Arc } from "./types";
|
||||
import { PRECISION } from "./utils";
|
||||
|
||||
|
@ -42,6 +44,25 @@ export function arcIncludesPoint<P extends GenericPoint>(
|
|||
: startAngle <= angle || endAngle >= angle;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param a
|
||||
* @param p
|
||||
*/
|
||||
export function arcDistanceFromPoint<Point extends GenericPoint>(
|
||||
a: Arc<Point>,
|
||||
p: Point,
|
||||
) {
|
||||
const intersectPoint = arcSegmentInterceptPoint(a, segment(p, a.center));
|
||||
|
||||
invariant(
|
||||
intersectPoint.length !== 1,
|
||||
"Arc distance intersector cannot have multiple intersections",
|
||||
);
|
||||
|
||||
return pointDistance(intersectPoint[0], p);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the intersection point(s) of a line segment represented by a start
|
||||
* point and end point and a symmetric arc.
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { pointsEqual } from "./point";
|
||||
import { segment, segmentIncludesPoint } from "./segment";
|
||||
import type { GenericPoint, Polygon } from "./types";
|
||||
import { segment, segmentIncludesPoint, segmentsIntersectAt } from "./segment";
|
||||
import type { GenericPoint, Polygon, Segment } from "./types";
|
||||
import { PRECISION } from "./utils";
|
||||
|
||||
export function polygon<Point extends GenericPoint>(...points: Point[]) {
|
||||
|
@ -62,3 +62,25 @@ function polygonClose<Point extends GenericPoint>(polygon: Point[]) {
|
|||
function polygonIsClosed<Point extends GenericPoint>(polygon: Point[]) {
|
||||
return pointsEqual(polygon[0], polygon[polygon.length - 1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the points of intersection of a line segment, identified by exactly
|
||||
* one start pointand one end point, and the polygon identified by a set of
|
||||
* ponits representing a set of connected lines.
|
||||
*/
|
||||
export function polygonSegmentIntersectionPoints<Point extends GenericPoint>(
|
||||
polygon: Readonly<Polygon<Point>>,
|
||||
segment: Readonly<Segment<Point>>,
|
||||
): Point[] {
|
||||
return polygon
|
||||
.reduce((segments, current, idx, poly) => {
|
||||
return idx === 0
|
||||
? []
|
||||
: ([
|
||||
...segments,
|
||||
[poly[idx - 1] as Point, current],
|
||||
] as Segment<Point>[]);
|
||||
}, [] as Segment<Point>[])
|
||||
.map((s) => segmentsIntersectAt(s, segment))
|
||||
.filter((point) => point !== null) as Point[];
|
||||
}
|
||||
|
|
|
@ -122,11 +122,11 @@ export const segmentIncludesPoint = <Point extends GenericPoint>(
|
|||
};
|
||||
|
||||
export const segmentDistanceToPoint = <Point extends GenericPoint>(
|
||||
point: Point,
|
||||
line: Segment<Point>,
|
||||
p: Point,
|
||||
s: Segment<Point>,
|
||||
) => {
|
||||
const [x, y] = point;
|
||||
const [[x1, y1], [x2, y2]] = line;
|
||||
const [x, y] = p;
|
||||
const [[x1, y1], [x2, y2]] = s;
|
||||
|
||||
const A = x - x1;
|
||||
const B = y - y1;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue