mirror of https://github.com/Minestom/Minestom.git
277 lines
6.1 KiB
Java
277 lines
6.1 KiB
Java
package net.minestom.server.utils;
|
|
|
|
public class Vector {
|
|
|
|
private static final double epsilon = 0.000001;
|
|
|
|
protected float x, y, z;
|
|
|
|
public Vector() {
|
|
this.x = 0;
|
|
this.y = 0;
|
|
this.z = 0;
|
|
}
|
|
|
|
public Vector(float x, float y, float z) {
|
|
this.x = x;
|
|
this.y = y;
|
|
this.z = z;
|
|
}
|
|
|
|
public Vector add(Vector vec) {
|
|
x += vec.x;
|
|
y += vec.y;
|
|
z += vec.z;
|
|
return this;
|
|
}
|
|
|
|
public Vector add(float x, float y, float z) {
|
|
this.x += x;
|
|
this.y += y;
|
|
this.z += z;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Subtracts a vector from this one.
|
|
*
|
|
* @param vec The other vector
|
|
* @return the same vector
|
|
*/
|
|
public Vector subtract(Vector vec) {
|
|
x -= vec.x;
|
|
y -= vec.y;
|
|
z -= vec.z;
|
|
return this;
|
|
}
|
|
|
|
public Vector subtract(float x, float y, float z) {
|
|
this.x -= x;
|
|
this.y -= y;
|
|
this.z -= z;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Multiplies the vector by another.
|
|
*
|
|
* @param vec The other vector
|
|
* @return the same vector
|
|
*/
|
|
public Vector multiply(Vector vec) {
|
|
x *= vec.x;
|
|
y *= vec.y;
|
|
z *= vec.z;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Divides the vector by another.
|
|
*
|
|
* @param vec The other vector
|
|
* @return the same vector
|
|
*/
|
|
public Vector divide(Vector vec) {
|
|
x /= vec.x;
|
|
y /= vec.y;
|
|
z /= vec.z;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Copies another vector
|
|
*
|
|
* @param vec The other vector
|
|
* @return the same vector
|
|
*/
|
|
public Vector copy(Vector vec) {
|
|
x = vec.x;
|
|
y = vec.y;
|
|
z = vec.z;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Gets the magnitude of the vector, defined as sqrt(x^2+y^2+z^2). The
|
|
* value of this method is not cached and uses a costly square-root
|
|
* function, so do not repeatedly call this method to get the vector's
|
|
* magnitude. NaN will be returned if the inner result of the sqrt()
|
|
* function overflows, which will be caused if the length is too long.
|
|
*
|
|
* @return the magnitude
|
|
*/
|
|
public double length() {
|
|
return Math.sqrt(MathUtils.square(x) + MathUtils.square(y) + MathUtils.square(z));
|
|
}
|
|
|
|
/**
|
|
* Gets the distance between this vector and another. The value of this
|
|
* method is not cached and uses a costly square-root function, so do not
|
|
* repeatedly call this method to get the vector's magnitude. NaN will be
|
|
* returned if the inner result of the sqrt() function overflows, which
|
|
* will be caused if the distance is too long.
|
|
*
|
|
* @param o The other vector
|
|
* @return the distance
|
|
*/
|
|
public double distance(Vector o) {
|
|
return Math.sqrt(MathUtils.square(x - o.x) + MathUtils.square(y - o.y) + MathUtils.square(z - o.z));
|
|
}
|
|
|
|
/**
|
|
* Gets the squared distance between this vector and another.
|
|
*
|
|
* @param o The other vector
|
|
* @return the squared distance
|
|
*/
|
|
public double distanceSquared(Vector o) {
|
|
return MathUtils.square(x - o.x) + MathUtils.square(y - o.y) + MathUtils.square(z - o.z);
|
|
}
|
|
|
|
/**
|
|
* Performs scalar multiplication, multiplying all components with a
|
|
* scalar.
|
|
*
|
|
* @param m The factor
|
|
* @return the same vector
|
|
*/
|
|
public Vector multiply(int m) {
|
|
x *= m;
|
|
y *= m;
|
|
z *= m;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Performs scalar multiplication, multiplying all components with a
|
|
* scalar.
|
|
*
|
|
* @param m The factor
|
|
* @return the same vector
|
|
*/
|
|
public Vector multiply(double m) {
|
|
x *= m;
|
|
y *= m;
|
|
z *= m;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Performs scalar multiplication, multiplying all components with a
|
|
* scalar.
|
|
*
|
|
* @param m The factor
|
|
* @return the same vector
|
|
*/
|
|
public Vector multiply(float m) {
|
|
x *= m;
|
|
y *= m;
|
|
z *= m;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Converts this vector to a unit vector (a vector with length of 1).
|
|
*
|
|
* @return the same vector
|
|
*/
|
|
public Vector normalize() {
|
|
double length = length();
|
|
|
|
x /= length;
|
|
y /= length;
|
|
z /= length;
|
|
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Zero this vector's components.
|
|
*
|
|
* @return the same vector
|
|
*/
|
|
public Vector zero() {
|
|
x = 0;
|
|
y = 0;
|
|
z = 0;
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
if (!(obj instanceof Vector)) {
|
|
return false;
|
|
}
|
|
|
|
Vector other = (Vector) obj;
|
|
|
|
return Math.abs(x - other.x) < epsilon && Math.abs(y - other.y) < epsilon && Math.abs(z - other.z) < epsilon && (this.getClass().equals(obj.getClass()));
|
|
}
|
|
|
|
/**
|
|
* Returns a hash code for this vector
|
|
*
|
|
* @return hash code
|
|
*/
|
|
@Override
|
|
public int hashCode() {
|
|
int hash = 7;
|
|
|
|
hash = 79 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
|
|
hash = 79 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
|
|
hash = 79 * hash + (int) (Double.doubleToLongBits(this.z) ^ (Double.doubleToLongBits(this.z) >>> 32));
|
|
return hash;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return "Vector{" +
|
|
"x=" + x +
|
|
", y=" + y +
|
|
", z=" + z +
|
|
'}';
|
|
}
|
|
|
|
/**
|
|
* Gets a new vector with the same values.
|
|
*
|
|
* @return vector
|
|
*/
|
|
public Vector copy() {
|
|
return new Vector(x, y, z);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public float getZ() {
|
|
return z;
|
|
}
|
|
|
|
public void setZ(float z) {
|
|
this.z = z;
|
|
}
|
|
|
|
/**
|
|
* Gets a new {@link Position} from this vector.
|
|
*
|
|
* @return this vector as a position
|
|
*/
|
|
public Position toPosition() {
|
|
return new Position(x, y, z);
|
|
}
|
|
}
|