Arc point testing
Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
This commit is contained in:
parent
233511317f
commit
11af75ee10
|
@ -1,4 +1,9 @@
|
|||
import { rangeIntersection, rangesOverlap, rotate } from "./math";
|
||||
import {
|
||||
isPointOnSymmetricArc,
|
||||
rangeIntersection,
|
||||
rangesOverlap,
|
||||
rotate,
|
||||
} from "./math";
|
||||
|
||||
describe("rotate", () => {
|
||||
it("should rotate over (x2, y2) and return the rotated coordinates for (x1, y1)", () => {
|
||||
|
@ -53,3 +58,42 @@ describe("range intersection", () => {
|
|||
expect(rangeIntersection([1, 4], [5, 7])).toEqual(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("point on arc", () => {
|
||||
it("should detect point on simple arc", () => {
|
||||
expect(
|
||||
isPointOnSymmetricArc(
|
||||
{
|
||||
radius: 1,
|
||||
startAngle: -Math.PI / 4,
|
||||
endAngle: Math.PI / 4,
|
||||
},
|
||||
[0.92291667, 0.385],
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
it("should not detect point outside of a simple arc", () => {
|
||||
expect(
|
||||
isPointOnSymmetricArc(
|
||||
{
|
||||
radius: 1,
|
||||
startAngle: -Math.PI / 4,
|
||||
endAngle: Math.PI / 4,
|
||||
},
|
||||
[-0.92291667, 0.385],
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
it("should not detect point with good angle but incorrect radius", () => {
|
||||
expect(
|
||||
isPointOnSymmetricArc(
|
||||
{
|
||||
radius: 1,
|
||||
startAngle: -Math.PI / 4,
|
||||
endAngle: Math.PI / 4,
|
||||
},
|
||||
[-0.5, 0.5],
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -667,3 +667,38 @@ export const aabbForElement = (
|
|||
|
||||
return bounds;
|
||||
};
|
||||
|
||||
type PolarCoords = [number, number];
|
||||
|
||||
/**
|
||||
* Return the polar coordinates for the given carthesian point represented by
|
||||
* (x, y) for the center point 0,0 where the first number returned is the radius,
|
||||
* the second is the angle in radians.
|
||||
*/
|
||||
export const carthesian2Polar = ([x, y]: Point): PolarCoords => [
|
||||
Math.hypot(x, y),
|
||||
Math.atan2(y, x),
|
||||
];
|
||||
|
||||
/**
|
||||
* Angles are in radians and centered on 0, 0. Zero radians on a 1 radius circle
|
||||
* corresponds to (1, 0) carthesian coordinates (point), i.e. to the "right".
|
||||
*/
|
||||
type SymmetricArc = { radius: number; startAngle: number; endAngle: number };
|
||||
|
||||
/**
|
||||
* Determines if a carthesian point lies on a symmetric arc, i.e. an arc which
|
||||
* is part of a circle contour centered on 0, 0.
|
||||
*/
|
||||
export const isPointOnSymmetricArc = (
|
||||
{ radius: arcRadius, startAngle, endAngle }: SymmetricArc,
|
||||
point: Point,
|
||||
): boolean => {
|
||||
const [radius, angle] = carthesian2Polar(point);
|
||||
|
||||
return startAngle < endAngle
|
||||
? Math.abs(radius - arcRadius) < 0.0000001 &&
|
||||
startAngle <= angle &&
|
||||
endAngle >= angle
|
||||
: startAngle <= angle || endAngle >= angle;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue