|
@@ -1,31 +1,68 @@
|
|
|
package holeg.utility.math.vector;
|
|
|
|
|
|
+import holeg.utility.math.Maths;
|
|
|
+
|
|
|
+
|
|
|
+ * A class representing a 2 dimensional vector with float entries.
|
|
|
+ */
|
|
|
public class Vec2f {
|
|
|
|
|
|
- public float x;
|
|
|
- public float y;
|
|
|
+
|
|
|
+ * 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) {
|
|
|
- setX(x);
|
|
|
- setY(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();
|
|
|
}
|
|
|
|
|
|
- public static float dot(Vec2f p1, Vec2f p2) {
|
|
|
- return p1.dot(p2);
|
|
|
+
|
|
|
+ * 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() {
|
|
@@ -44,100 +81,155 @@ public class Vec2f {
|
|
|
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) {
|
|
|
- setX(x);
|
|
|
- setY(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.getX() + y * other.getY();
|
|
|
+ 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 Distance to a other Position.
|
|
|
+ * Return the euclidean distance to a other vector.
|
|
|
*
|
|
|
- * @param other the other Position.
|
|
|
- * @return distance to the Position.
|
|
|
+ * @param other the other vector.
|
|
|
+ * @return distance to the vector.
|
|
|
*/
|
|
|
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.
|
|
|
+ * 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.getX() - x;
|
|
|
- float yDistance = other.getY() - y;
|
|
|
+ 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.setX(x + other.getX());
|
|
|
- result.setY(y + other.getY());
|
|
|
+ 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.getX();
|
|
|
- y += other.getY();
|
|
|
+ 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.setX(x - other.getX());
|
|
|
- result.setY(y - other.getY());
|
|
|
+ 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.getX();
|
|
|
- y -= other.getY();
|
|
|
+ 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();
|
|
|
- result.setX(x * scaleFactor);
|
|
|
- result.setY(y * scaleFactor);
|
|
|
+ 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;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * @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);
|
|
|
}
|
|
|
|
|
|
- public Vec2f normalize() {
|
|
|
+
|
|
|
+ * Normalizes this vector by dividing with the length of the vector.
|
|
|
+ */
|
|
|
+ public void normalize() {
|
|
|
float len = getLength();
|
|
|
if (len != 0.0f) {
|
|
|
- this.setX(x / len);
|
|
|
- this.setY(y / len);
|
|
|
+ x /= len;
|
|
|
+ y /= len;
|
|
|
} else {
|
|
|
- this.setX(0.0f);
|
|
|
- this.setY(0.0f);
|
|
|
+ this.set(0, 0);
|
|
|
}
|
|
|
-
|
|
|
- return this;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -147,11 +239,7 @@ public class Vec2f {
|
|
|
* @param max upper bound
|
|
|
*/
|
|
|
public void clampX(float min, float max) {
|
|
|
- if (x < min) {
|
|
|
- setX(min);
|
|
|
- } else if (x > max) {
|
|
|
- setX(max);
|
|
|
- }
|
|
|
+ x = Maths.clamp(x, min, max);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -161,17 +249,11 @@ public class Vec2f {
|
|
|
* @param max upper bound
|
|
|
*/
|
|
|
public void clampY(float min, float max) {
|
|
|
- if (y < min) {
|
|
|
- setY(min);
|
|
|
- } else if (y > max) {
|
|
|
- setY(max);
|
|
|
- }
|
|
|
+ y = Maths.clamp(y, min, max);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ @Override
|
|
|
public String toString() {
|
|
|
- return "X: " + x + " Y: " + y;
|
|
|
+ return "Vec2f(" + x + "," + y + ")";
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|