Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

closest orthagonal distance function #426

Merged
merged 6 commits into from
Oct 4, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/math/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ export class Vec2 {
static UP = new Vec2(0, -1);
static DOWN = new Vec2(0, 1);

/** Closest orthagonal direction: LEFT, RIGHT, UP, or DOWN */
toAxis(): Vec2 {
var result = vec2();
var minDist = Number.MAX_VALUE;
for (var c of [Vec2.LEFT, Vec2.RIGHT, Vec2.UP, Vec2.DOWN]) {
const dist = this.unit().sub(c).len();
if (dist < minDist) {
minDist = dist;
result = c;
}
}
return result;
}

/** Clone the vector */
clone(): Vec2 {
return new Vec2(this.x, this.y);
Expand Down