More math methods in Vector

This commit is contained in:
themode 2021-01-25 13:56:17 +01:00
parent 886c4ca9c9
commit f48c291929

View File

@ -150,6 +150,18 @@ public class Vector implements PublicCloneable<Vector> {
return MathUtils.square(x - o.x) + MathUtils.square(y - o.y) + MathUtils.square(z - o.z);
}
/**
* Gets the angle between this vector and another in radians.
*
* @param other The other vector
* @return angle in radians
*/
public float angle(Vector other) {
double dot = dot(other) / (length() * other.length());
return (float) Math.acos(dot);
}
/**
* Performs scalar multiplication, multiplying all components with a
* scalar.
@ -195,6 +207,40 @@ public class Vector implements PublicCloneable<Vector> {
return this;
}
/**
* Calculates the dot product of this vector with another. The dot product
* is defined as x1*x2+y1*y2+z1*z2. The returned value is a scalar.
*
* @param other The other vector
* @return dot product
*/
public double dot(Vector other) {
return x * other.x + y * other.y + z * other.z;
}
/**
* Calculates the cross product of this vector with another. The cross
* product is defined as:
* <ul>
* <li>x = y1 * z2 - y2 * z1
* <li>y = z1 * x2 - z2 * x1
* <li>z = x1 * y2 - x2 * y1
* </ul>
*
* @param o The other vector
* @return the same vector
*/
public Vector crossProduct(Vector o) {
float newX = y * o.z - o.y * z;
float newY = z * o.x - o.z * x;
float newZ = x * o.y - o.x * y;
x = newX;
y = newY;
z = newZ;
return this;
}
/**
* Converts this vector to a unit vector (a vector with length of 1).
*