package holeg.utility.math.vector; public class Vec2f { public float x; public float y; public Vec2f() { x = y = 0; } public Vec2f(float x, float y) { setX(x); setY(y); } public Vec2f(Vec2f other) { x = other.x; y = other.y; } public void setX(float x) { this.x = x; } public float getX() { return x; } public void setY(float y) { this.y = y; } public float getY() { return y; } public void set(float x, float y) { setX(x); setY(y); } public float dot(Vec2f other) { float result = 0.0f; result = x * other.getX() + y * other.getY(); return result; } public float getLength() { return (float)Math.sqrt(x * x + y * y); } /** * Return the Distance to a other Position. * @param other the other Position. * @return distance to the Position. */ public float getDistance(Vec2f other) { return (float) Math.sqrt(getSquaredDistance(other)); } /** * Return the Distance squared to a other Position. * Faster then Distance because no Sqrt() needed. * @param other the other Position. * @return squared distance to the Position */ public float getSquaredDistance(Vec2f other) { float xDistance = other.getX() - x; float yDistance = other.getY() - y; return xDistance * xDistance + yDistance * yDistance; } public Vec2f add(Vec2f other) { Vec2f result = new Vec2f(); result.setX(x + other.getX()); result.setY(y + other.getY()); return result; } public void addAssign(Vec2f other) { x += other.getX(); y += other.getY(); } public Vec2f subtract(Vec2f other) { Vec2f result = new Vec2f(); result.setX(x - other.getX()); result.setY(y - other.getY()); return result; } public void subtractAssign(Vec2f other) { x -= other.getX(); y -= other.getY(); } public Vec2f multiply(float scaleFactor) { Vec2f result = new Vec2f(); result.setX(x * scaleFactor); result.setY(y * scaleFactor); return result; } public void multiplyAssign(float scaleFactor) { x *= scaleFactor; y *= scaleFactor; } public Vec2f divide(float divideFactor) { return this.multiply(1.0f/divideFactor); } public void divideAssign(float divideFactor) { multiplyAssign(1.0f/divideFactor); } public Vec2f normalize() { float len = getLength(); if (len != 0.0f) { this.setX(x / len); this.setY(y / len); } else { this.setX(0.0f); this.setY(0.0f); } return this; } /** * Clamp the X Value two a upper or lower bound * @param min lower bound * @param max upper bound */ public void clampX(float min, float max) { if(x < min) setX(min); else if(x > max) setX(max); } /** * Clamp the Y Value two a upper or lower bound * @param min lower bound * @param max upper bound */ public void clampY(float min, float max) { if(y < min) setY(min); else if(y > max) setY(max); } public String toString() { return "X: " + x + " Y: " + y; } }