package utility; /** * * @author Tom Troppmann * */ public class Vector2Int { private int x; private int y; public Vector2Int() { x = y = 0; } public Vector2Int(int x, int y) { setX(x); setY(y); } public void setX(int x) { this.x = x; } public int getX() { return x; } public void setY(int y) { this.y = y; } public int getY() { return y; } public void set(int x, int y) { setX(x); setY(y); } public void set(Vector2Int other) { setX(other.x); setY(other.y); } public float dot(Vector2Int 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(Vector2Int 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(Vector2Int other) { int xDistance = other.getX() - x; int yDistance = other.getY() - y; return xDistance * xDistance + yDistance * yDistance; } public Vector2Int add(Vector2Int other) { Vector2Int result = new Vector2Int(); result.setX(x + other.getX()); result.setY(y + other.getY()); return result; } public Vector2Int subtract(Vector2Int other) { Vector2Int result = new Vector2Int(); result.setX(x - other.getX()); result.setY(y - other.getY()); return result; } public Vector2Int multiply(float scaleFactor) { Vector2Int result = new Vector2Int(); result.setX((int)(x * scaleFactor)); result.setY((int)(y * scaleFactor)); return result; } public Vector2Int divide(float divideFactor) { return this.multiply(1.0f/divideFactor); } /** * Clamp the X Value two a upper or lower bound * @param min lower bound * @param max upper bound */ public void clampX(int min, int 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(int min, int max) { if(y < min) setY(min); else if(y > max) setY(max); } public String toString() { return "X: " + x + " Y: " + y; } public Vector2Int clone() { return new Vector2Int(x, y); } }