Arc code updated

This commit is contained in:
Mark Tolmacs 2025-02-11 15:17:04 +01:00
parent fcb3060388
commit 85b4b315c7
No known key found for this signature in database
2 changed files with 71 additions and 16 deletions

View file

@ -1,4 +1,3 @@
import { radians } from "./angle";
import {
arc,
arcIncludesPoint,
@ -8,12 +7,18 @@ import {
import { line } from "./line";
import { pointFrom } from "./point";
import { segment } from "./segment";
import type { GlobalPoint, Radians } from "./types";
describe("point on arc", () => {
it("should detect point on simple arc", () => {
expect(
arcIncludesPoint(
arc(pointFrom(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
arc(
pointFrom(0, 0),
1,
(-Math.PI / 4) as Radians,
(Math.PI / 4) as Radians,
),
pointFrom(0.92291667, 0.385),
),
).toBe(true);
@ -21,7 +26,12 @@ describe("point on arc", () => {
it("should not detect point outside of a simple arc", () => {
expect(
arcIncludesPoint(
arc(pointFrom(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
arc(
pointFrom(0, 0),
1,
(-Math.PI / 4) as Radians,
(Math.PI / 4) as Radians,
),
pointFrom(-0.92291667, 0.385),
),
).toBe(false);
@ -29,7 +39,12 @@ describe("point on arc", () => {
it("should not detect point with good angle but incorrect radius", () => {
expect(
arcIncludesPoint(
arc(pointFrom(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
arc(
pointFrom(0, 0),
1,
(-Math.PI / 4) as Radians,
(Math.PI / 4) as Radians,
),
pointFrom(-0.5, 0.5),
),
).toBe(false);
@ -40,16 +55,37 @@ describe("intersection", () => {
it("should report correct interception point for segment", () => {
expect(
arcSegmentInterceptPoints(
arc(pointFrom(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
arc(
pointFrom(0, 0),
1,
(-Math.PI / 4) as Radians,
(Math.PI / 4) as Radians,
),
segment(pointFrom(2, 1), pointFrom(0, 0)),
),
).toEqual([pointFrom(0.894427190999916, 0.447213595499958)]);
expect(
arcSegmentInterceptPoints(
arc(
pointFrom(0, 0),
1,
Math.PI as Radians,
((3 / 2) * Math.PI) as Radians,
),
segment(pointFrom(-10, -10), pointFrom(0, 0)),
),
).toEqual([pointFrom(-0.7071067811865479, -0.7071067811865479)]);
});
it("should report both interception points when present for segment", () => {
expect(
arcSegmentInterceptPoints(
arc(pointFrom(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
arc(
pointFrom(0, 0),
1,
(-Math.PI / 4) as Radians,
(Math.PI / 4) as Radians,
),
segment(pointFrom(0.9, -2), pointFrom(0.9, 2)),
),
).toEqual([
@ -61,7 +97,12 @@ describe("intersection", () => {
it("should report correct interception point for line", () => {
expect(
arcLineInterceptPoints(
arc(pointFrom(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
arc(
pointFrom<GlobalPoint>(0, 0),
1,
(-Math.PI / 4) as Radians,
(Math.PI / 4) as Radians,
),
line(pointFrom(2, 1), pointFrom(0, 0)),
),
).toEqual([pointFrom(0.894427190999916, 0.447213595499958)]);
@ -70,7 +111,12 @@ describe("intersection", () => {
it("should report both interception points when present for line", () => {
expect(
arcLineInterceptPoints(
arc(pointFrom(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
arc(
pointFrom<GlobalPoint>(0, 0),
1,
(-Math.PI / 4) as Radians,
(Math.PI / 4) as Radians,
),
line(pointFrom(0.9, -2), pointFrom(0.9, 2)),
),
).toEqual([