From b2fcf787e2a366a176c76cb2995ca011983e268c Mon Sep 17 00:00:00 2001 From: Mark Tolmacs Date: Tue, 7 Jan 2025 12:27:29 +0100 Subject: [PATCH] Fix arc negative radians issue --- packages/math/angle.ts | 10 ++++++++-- packages/math/arc.ts | 23 ++++++++++++----------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/packages/math/angle.ts b/packages/math/angle.ts index 0d477a9e1a..6adbc99115 100644 --- a/packages/math/angle.ts +++ b/packages/math/angle.ts @@ -60,8 +60,14 @@ export const normalizeRadians = (angle: Radians): Radians => { export const cartesian2Polar =

([ x, y, -]: P): PolarCoords => - polar(Math.hypot(x, y), normalizeRadians(radians(Math.atan2(y, x)))); +]: P): PolarCoords => { + const [radius, angle] = polar( + Math.hypot(x, y), + normalizeRadians(radians(Math.atan2(y, x))), + ); + + return [radius, normalizeRadians(angle)] as PolarCoords; +}; /** * Convert an angle in degrees into randians diff --git a/packages/math/arc.ts b/packages/math/arc.ts index 47d2434f4f..37f84e7554 100644 --- a/packages/math/arc.ts +++ b/packages/math/arc.ts @@ -25,7 +25,10 @@ export function arc( startAngle: Radians, endAngle: Radians, ) { - return { center, radius, startAngle, endAngle } as Arc; + const start = normalizeRadians(startAngle); + const end = normalizeRadians(endAngle); + + return { center, radius, startAngle: start, endAngle: end } as Arc; } /** @@ -100,11 +103,10 @@ export function arcSegmentInterceptPoints( pointFrom(candidate[0] - a.center[0], candidate[1] - a.center[1]), ); - return a.startAngle < a.endAngle - ? Math.abs(a.radius - candidateRadius) < PRECISION && - a.startAngle <= candidateAngle && - a.endAngle >= candidateAngle - : a.startAngle <= candidateAngle || a.endAngle >= candidateAngle; + return Math.abs(a.radius - candidateRadius) < PRECISION && + a.startAngle > a.endAngle + ? a.startAngle <= candidateAngle || a.endAngle >= candidateAngle + : a.startAngle <= candidateAngle && a.endAngle >= candidateAngle; }); } @@ -128,11 +130,10 @@ export function arcLineInterceptPoints( pointFrom(candidate[0] - a.center[0], candidate[1] - a.center[1]), ); - return a.startAngle < a.endAngle - ? Math.abs(a.radius - candidateRadius) < PRECISION && - a.startAngle <= candidateAngle && - a.endAngle >= candidateAngle - : a.startAngle <= candidateAngle || a.endAngle >= candidateAngle; + return Math.abs(a.radius - candidateRadius) < PRECISION && + a.startAngle > a.endAngle + ? a.startAngle <= candidateAngle || a.endAngle >= candidateAngle + : a.startAngle <= candidateAngle && a.endAngle >= candidateAngle; }); }