ellipse and arc refactors

This commit is contained in:
Mark Tolmacs 2024-09-25 11:03:35 +02:00
parent 3efa8735ef
commit 392dd5b0b8
No known key found for this signature in database
9 changed files with 125 additions and 94 deletions

View file

@ -1,5 +1,7 @@
import { cartesian2Polar } from "./angle";
import type { GenericPoint, Radians, SymmetricArc } from "./types";
import { cartesian2Polar, radians } from "./angle";
import { ellipse, interceptPointsOfLineAndEllipse } from "./ellipse";
import { point } from "./point";
import type { GenericPoint, LineSegment, Radians, SymmetricArc } from "./types";
import { PRECISION } from "./utils";
/**
@ -12,8 +14,13 @@ import { PRECISION } from "./utils";
* @param endAngle The end angle with 0 radians being the "northest" point
* @returns The constructed symmetric arc
*/
export function arc(radius: number, startAngle: Radians, endAngle: Radians) {
return { radius, startAngle, endAngle } as SymmetricArc;
export function arc<Point extends GenericPoint>(
center: Point,
radius: number,
startAngle: Radians,
endAngle: Radians,
) {
return { center, radius, startAngle, endAngle } as SymmetricArc<Point>;
}
/**
@ -21,10 +28,12 @@ export function arc(radius: number, startAngle: Radians, endAngle: Radians) {
* is part of a circle contour centered on 0, 0.
*/
export function isPointOnSymmetricArc<P extends GenericPoint>(
{ radius: arcRadius, startAngle, endAngle }: SymmetricArc,
point: P,
{ center, radius: arcRadius, startAngle, endAngle }: SymmetricArc<P>,
p: P,
): boolean {
const [radius, angle] = cartesian2Polar(point);
const [radius, angle] = cartesian2Polar(
point(p[0] - center[0], p[1] - center[1]),
);
return startAngle < endAngle
? Math.abs(radius - arcRadius) < PRECISION &&
@ -32,3 +41,27 @@ export function isPointOnSymmetricArc<P extends GenericPoint>(
endAngle >= angle
: startAngle <= angle || endAngle >= angle;
}
/**
* Returns the intersection point(s) of a line segment represented by a start
* point and end point and a symmetric arc.
*/
export function interceptOfSymmetricArcAndSegment<Point extends GenericPoint>(
a: Readonly<SymmetricArc<Point>>,
l: Readonly<LineSegment<Point>>,
): Point[] {
return interceptPointsOfLineAndEllipse(
ellipse(a.center, radians(0), a.radius, a.radius),
l,
).filter((candidate) => {
const [candidateRadius, candidateAngle] = cartesian2Polar(
point(candidate[0] - a.center[0], candidate[1] - a.center[1]),
);
return a.startAngle < a.endAngle
? Math.abs(a.radius - candidateRadius) < 0.0000001 &&
a.startAngle <= candidateAngle &&
a.endAngle >= candidateAngle
: a.startAngle <= candidateAngle || a.endAngle >= candidateAngle;
});
}