123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- 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 Vec2f(Vec2i other)
- {
- x = other.getX();
- y = other.getY();
- }
- 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 static float dot(Vec2f p1, Vec2f p2)
- {
- return p1.dot(p2);
- }
- 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;
- }
- }
|