Fix arc negative radians issue

This commit is contained in:
Mark Tolmacs 2025-01-07 12:27:29 +01:00
parent fb113e2054
commit b2fcf787e2
No known key found for this signature in database
2 changed files with 20 additions and 13 deletions

View file

@ -60,8 +60,14 @@ export const normalizeRadians = (angle: Radians): Radians => {
export const cartesian2Polar = <P extends GenericPoint>([
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

View file

@ -25,7 +25,10 @@ export function arc<Point extends GenericPoint>(
startAngle: Radians,
endAngle: Radians,
) {
return { center, radius, startAngle, endAngle } as Arc<Point>;
const start = normalizeRadians(startAngle);
const end = normalizeRadians(endAngle);
return { center, radius, startAngle: start, endAngle: end } as Arc<Point>;
}
/**
@ -100,11 +103,10 @@ export function arcSegmentInterceptPoints<Point extends GenericPoint>(
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<Point extends GenericPoint>(
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;
});
}