First implementation of element distance functions with failing tests

This commit is contained in:
Mark Tolmacs 2024-09-25 18:30:53 +02:00
parent 47cc842415
commit d9ea7190ec
No known key found for this signature in database
6 changed files with 275 additions and 173 deletions

View file

@ -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.