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