Rectangle distance and tests

This commit is contained in:
Mark Tolmacs 2024-09-25 15:04:46 +02:00
parent 9a8aabbeca
commit 47cc842415
No known key found for this signature in database
5 changed files with 72 additions and 25 deletions

View file

@ -0,0 +1,39 @@
import { point } from "./point";
import { rectangle, rectangleDistanceFromPoint } from "./rectangle";
describe("rectangle distance", () => {
it("finds the shortest distance", () => {
expect(
rectangleDistanceFromPoint(
rectangle(point(-1, -1), point(1, 1)),
point(2, 0),
),
).toBe(1);
expect(
rectangleDistanceFromPoint(
rectangle(point(-1, -1), point(1, 1)),
point(0, 2),
),
).toBe(1);
expect(
rectangleDistanceFromPoint(
rectangle(point(-1, -1), point(1, 1)),
point(-2, 0),
),
).toBe(1);
expect(
rectangleDistanceFromPoint(
rectangle(point(-1, -1), point(1, 1)),
point(0, -2),
),
).toBe(1);
});
it("finds the corner as closest point", () => {
expect(
rectangleDistanceFromPoint(
rectangle(point(-1, -1), point(1, 1)),
point(2, 2),
),
).toBe(Math.sqrt(2));
});
});