Skip to content

Instantly share code, notes, and snippets.

@andrewlalis
Created April 2, 2022 16:47
Show Gist options
  • Save andrewlalis/b25745ca533553b293b43a28679958f4 to your computer and use it in GitHub Desktop.
Save andrewlalis/b25745ca533553b293b43a28679958f4 to your computer and use it in GitHub Desktop.
2D floating-point vector in Java
/**
* Standard 2-dimensional floating-point vector implementation.
*/
public final class Vec2F {
public float x;
public float y;
public Vec2F(float x, float y) {
this.x = x;
this.y = y;
}
public Vec2F(float n) {
this(n, n);
}
public Vec2F() {
this(0);
}
public Vec2F(Vec2F other) {
this(other.x, other.y);
}
public float length() {
return (float) Math.sqrt(x * x + y * y);
}
public float dot(Vec2F other) {
return x * other.x + y * other.y;
}
public Vec2F add(Vec2F other) {
x += other.x;
y += other.y;
return this;
}
public Vec2F sub(Vec2F other) {
x -= other.x;
y -= other.y;
return this;
}
public Vec2F mul(float factor) {
x *= factor;
y *= factor;
return this;
}
public Vec2F div(float factor) {
x /= factor;
y /= factor;
return this;
}
public Vec2F normalize() {
return div(length());
}
public Vec2F toPolar() {
float r = length();
float theta = (float) Math.atan2(y, x);
x = r;
y = theta;
return this;
}
public Vec2F toCartesian() {
float cx = (float) (x * Math.cos(y));
float cy = (float) (x * Math.sin(y));
x = cx;
y = cy;
return this;
}
@Override
public String toString() {
return "[ " + x + ", " + y + " ]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment