package classes; public class Vector2dFloat { private float x; private float y; public Vector2dFloat() { x = y = 0; } public Vector2dFloat(float x, float y) { setX(x); setY(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(Vector2dFloat 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(Vector2dFloat 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(Vector2dFloat other) { float xDistance = other.getX() - x; float yDistance = other.getY() - y; return xDistance * xDistance + yDistance * yDistance; } public Vector2dFloat add(Vector2dFloat other) { Vector2dFloat result = new Vector2dFloat(); result.setX(x + other.getX()); result.setY(y + other.getY()); return result; } public Vector2dFloat subtract(Vector2dFloat other) { Vector2dFloat result = new Vector2dFloat(); result.setX(x - other.getX()); result.setY(y - other.getY()); return result; } public Vector2dFloat multiply(float scaleFactor) { Vector2dFloat result = new Vector2dFloat(); result.setX(x * scaleFactor); result.setY(y * scaleFactor); return result; } public Vector2dFloat divide(float divideFactor) { Vector2dFloat result = new Vector2dFloat(); result.setX(x / divideFactor); result.setY(y / divideFactor); return result; } public Vector2dFloat 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; } }