123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- package holeg.utility.math.vector;
- import holeg.utility.math.Maths;
- /**
- * A class representing a 2 dimensional vector with float entries.
- */
- public class Vec2f {
- /**
- * The first entry.
- */
- private float x;
- /**
- * The second entry.
- */
- private float y;
- /**
- * Constructs a vector and initialized it with zeros.
- */
- public Vec2f() {
- x = y = 0;
- }
- /**
- * Constructs a vector with x and y.
- *
- * @param x the first entry
- * @param y the second entry
- */
- public Vec2f(float x, float y) {
- this.x = x;
- this.y = y;
- }
- /**
- * Creates a copy of another vector.
- *
- * @param other the other to copy the entries from
- */
- public Vec2f(Vec2f other) {
- x = other.x;
- y = other.y;
- }
- /**
- * Creates a copy of another integer vector but the entries stores as float.
- *
- * @param other the other integer vector
- */
- public Vec2f(Vec2i other) {
- x = other.getX();
- y = other.getY();
- }
- /**
- * Creates the dot product of two vectors.
- *
- * @param v1 the first vector
- * @param v2 the second vector
- * @return the dot product
- */
- public static float dot(Vec2f v1, Vec2f v2) {
- return v1.dot(v2);
- }
- public float getX() {
- return x;
- }
- public void setX(float x) {
- this.x = x;
- }
- public float getY() {
- return y;
- }
- public void setY(float y) {
- this.y = y;
- }
- /**
- * Sets the first and second entry of the vector.
- *
- * @param x the first entry
- * @param y the second entry
- */
- public void set(float x, float y) {
- this.x = x;
- this.y = y;
- }
- /**
- * Creates the dot product with another vector.
- *
- * @param other the other vector
- * @return the dot product
- */
- public float dot(Vec2f other) {
- float result = 0.0f;
- result = x * other.x + y * other.y;
- return result;
- }
- /**
- * Returns the euclidean distance to zero.
- *
- * @return distance to zero
- */
- public float getLength() {
- return (float) Math.sqrt(x * x + y * y);
- }
- /**
- * Return the euclidean distance to a other vector.
- *
- * @param other the other vector.
- * @return distance to the vector.
- */
- public float getDistance(Vec2f other) {
- return (float) Math.sqrt(getSquaredDistance(other));
- }
- /**
- * Return the squared distance to a other Position. Faster than {@link #getDistance(Vec2f)}
- * because no {@link Math#sqrt(double)} needed.
- *
- * @param other the other Position.
- * @return squared distance to the Position
- */
- public float getSquaredDistance(Vec2f other) {
- float xDistance = other.x - x;
- float yDistance = other.x - y;
- return xDistance * xDistance + yDistance * yDistance;
- }
- /**
- * Returns a new vector that is the sum of this vector and another Vector.
- *
- * @param other the other vector
- * @return the sum
- */
- public Vec2f add(Vec2f other) {
- Vec2f result = new Vec2f();
- result.x += other.x;
- result.y += other.y;
- return result;
- }
- /**
- * Add the entries of another Vector to this vector.
- *
- * @param other the other vector
- */
- public void addAssign(Vec2f other) {
- x += other.x;
- y += other.y;
- }
- /**
- * Returns a new vector that is the subtraction of this vector and another Vector.
- *
- * @param other the other vector
- * @return the subtraction
- */
- public Vec2f subtract(Vec2f other) {
- Vec2f result = new Vec2f();
- result.x -= other.x;
- result.y -= other.y;
- return result;
- }
- /**
- * Subtract the entries of another Vector to this vector.
- *
- * @param other the other vector
- */
- public void subtractAssign(Vec2f other) {
- x -= other.x;
- y -= other.y;
- }
- /**
- * Returns a new vector which entries are multiplied times a scaled factor.
- * @param scaleFactor the scale factor
- * @return the multiplication from this vector with a scale
- */
- public Vec2f multiply(float scaleFactor) {
- Vec2f result = new Vec2f();
- x *= scaleFactor;
- y *= scaleFactor;
- return result;
- }
- /**
- * Multiply this vector entries time a scale factor.
- * @param scaleFactor the scale factor
- */
- public void multiplyAssign(float scaleFactor) {
- x *= scaleFactor;
- y *= scaleFactor;
- }
- /** Returns a new vector which entries are divided with a divide factor.
- * @param divideFactor the divide factor
- * @return the division from this vector and a divide factor
- */
- public Vec2f divide(float divideFactor) {
- return this.multiply(1.0f / divideFactor);
- }
- /**
- * Divide this vector entries with a divide factor.
- * @param divideFactor the divide factor
- */
- public void divideAssign(float divideFactor) {
- multiplyAssign(1.0f / divideFactor);
- }
- /**
- * Normalizes this vector by dividing with the length of the vector.
- */
- public void normalize() {
- float len = getLength();
- if (len != 0.0f) {
- x /= len;
- y /= len;
- } else {
- this.set(0, 0);
- }
- }
- /**
- * 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) {
- x = Maths.clamp(x, min, 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) {
- y = Maths.clamp(y, min, max);
- }
- @Override
- public String toString() {
- return "Vec2f(" + x + "," + y + ")";
- }
- }
|