From 4b051ae2fd9a373404e115b9012fc2d952f8b185 Mon Sep 17 00:00:00 2001 From: dragoncoder047 <101021094+dragoncoder047@users.noreply.github.com> Date: Fri, 4 Oct 2024 07:38:39 -0400 Subject: [PATCH] closest orthagonal distance function (#426) --- src/math/math.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/math/math.ts b/src/math/math.ts index 9d4f08ea..3da86076 100644 --- a/src/math/math.ts +++ b/src/math/math.ts @@ -112,6 +112,13 @@ export class Vec2 { static UP = new Vec2(0, -1); static DOWN = new Vec2(0, 1); + /** Closest orthogonal direction: LEFT, RIGHT, UP, or DOWN */ + toAxis(): Vec2 { + return Math.abs(this.x) > Math.abs(this.y) ? + this.x < 0 ? Vec2.LEFT : Vec2.RIGHT : + this.y < 0 ? Vec2.UP : Vec2.DOWN; + } + /** Clone the vector */ clone(): Vec2 { return new Vec2(this.x, this.y);