mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-15 14:13:56 +01:00
Improved Vector and Location with some utility methods, optimized Vector.isInSphere().
By: sk89q <the.sk89q@gmail.com>
This commit is contained in:
parent
f6873cab82
commit
61b42932b1
2 changed files with 419 additions and 352 deletions
|
@ -60,6 +60,16 @@ public class Location implements Cloneable {
|
||||||
public double getX() {
|
public double getX() {
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the floored value of the X component, indicating the block that
|
||||||
|
* this location is contained with.
|
||||||
|
*
|
||||||
|
* @return block X
|
||||||
|
*/
|
||||||
|
public int getBlockX() {
|
||||||
|
return (int)Math.floor(x);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the y-coordinate of this location
|
* Sets the y-coordinate of this location
|
||||||
|
@ -79,6 +89,16 @@ public class Location implements Cloneable {
|
||||||
return y;
|
return y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the floored value of the Y component, indicating the block that
|
||||||
|
* this location is contained with.
|
||||||
|
*
|
||||||
|
* @return block y
|
||||||
|
*/
|
||||||
|
public int getBlockY() {
|
||||||
|
return (int)Math.floor(y);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the z-coordinate of this location
|
* Sets the z-coordinate of this location
|
||||||
*
|
*
|
||||||
|
@ -97,6 +117,16 @@ public class Location implements Cloneable {
|
||||||
return z;
|
return z;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the floored value of the Z component, indicating the block that
|
||||||
|
* this location is contained with.
|
||||||
|
*
|
||||||
|
* @return block z
|
||||||
|
*/
|
||||||
|
public int getBlockZ() {
|
||||||
|
return (int)Math.floor(z);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the yaw of this location
|
* Sets the yaw of this location
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,352 +1,389 @@
|
||||||
package org.bukkit;
|
package org.bukkit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a mutable vector.
|
* Represents a mutable vector.
|
||||||
*
|
*
|
||||||
* @author sk89q
|
* @author sk89q
|
||||||
*/
|
*/
|
||||||
public class Vector implements Cloneable {
|
public class Vector implements Cloneable {
|
||||||
private static final long serialVersionUID = -2657651106777219169L;
|
private static final long serialVersionUID = -2657651106777219169L;
|
||||||
|
|
||||||
protected double x;
|
protected double x;
|
||||||
protected double y;
|
protected double y;
|
||||||
protected double z;
|
protected double z;
|
||||||
|
|
||||||
public Vector(int x, int y, int z) {
|
public Vector(int x, int y, int z) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
this.z = z;
|
this.z = z;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector(double x, double y, double z) {
|
public Vector(double x, double y, double z) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
this.z = z;
|
this.z = z;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector(float x, float y, float z) {
|
public Vector(float x, float y, float z) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
this.z = z;
|
this.z = z;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds the vector by another.
|
* Adds the vector by another.
|
||||||
*
|
*
|
||||||
* @param vec
|
* @param vec
|
||||||
* @return the same vector
|
* @return the same vector
|
||||||
*/
|
*/
|
||||||
public Vector add(Vector vec) {
|
public Vector add(Vector vec) {
|
||||||
x += vec.x;
|
x += vec.x;
|
||||||
y += vec.y;
|
y += vec.y;
|
||||||
z += vec.z;
|
z += vec.z;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subtracts the vector by another.
|
* Subtracts the vector by another.
|
||||||
*
|
*
|
||||||
* @param vec
|
* @param vec
|
||||||
* @return the same vector
|
* @return the same vector
|
||||||
*/
|
*/
|
||||||
public Vector subtract(Vector vec) {
|
public Vector subtract(Vector vec) {
|
||||||
x -= vec.x;
|
x -= vec.x;
|
||||||
y -= vec.y;
|
y -= vec.y;
|
||||||
z -= vec.z;
|
z -= vec.z;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Multiplies the vector by another.
|
* Multiplies the vector by another.
|
||||||
*
|
*
|
||||||
* @param vec
|
* @param vec
|
||||||
* @return the same vector
|
* @return the same vector
|
||||||
*/
|
*/
|
||||||
public Vector multiply(Vector vec) {
|
public Vector multiply(Vector vec) {
|
||||||
x *= vec.x;
|
x *= vec.x;
|
||||||
y *= vec.y;
|
y *= vec.y;
|
||||||
z *= vec.z;
|
z *= vec.z;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Divides the vector by another.
|
* Divides the vector by another.
|
||||||
*
|
*
|
||||||
* @param vec
|
* @param vec
|
||||||
* @return the same vector
|
* @return the same vector
|
||||||
*/
|
*/
|
||||||
public Vector divide(Vector vec) {
|
public Vector divide(Vector vec) {
|
||||||
x /= vec.x;
|
x /= vec.x;
|
||||||
y /= vec.y;
|
y /= vec.y;
|
||||||
z /= vec.z;
|
z /= vec.z;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the magnitude of the vector, defined as sqrt(x^2+y^2+z^2). The value
|
* 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
|
* 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.
|
* 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,
|
||||||
* @return the magnitude
|
* which will be caused if the length is too long.
|
||||||
*/
|
*
|
||||||
public double length() {
|
* @return the magnitude
|
||||||
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
|
*/
|
||||||
}
|
public double length() {
|
||||||
|
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
|
||||||
/**
|
}
|
||||||
* Get 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.
|
* Get the distance between this vector and another. The value
|
||||||
*
|
* of this method is not cached and uses a costly square-root function, so
|
||||||
* @return the distance
|
* 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,
|
||||||
public double distance(Vector o) {
|
* which will be caused if the distance is too long.
|
||||||
return Math.sqrt(Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2)
|
*
|
||||||
+ Math.pow(z - o.z, 2));
|
* @return the distance
|
||||||
}
|
*/
|
||||||
|
public double distance(Vector o) {
|
||||||
/**
|
return Math.sqrt(Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2)
|
||||||
* Performs scalar multiplication, multiplying all components with a scalar.
|
+ Math.pow(z - o.z, 2));
|
||||||
*
|
}
|
||||||
* @param m
|
|
||||||
* @return the same vector
|
/**
|
||||||
*/
|
* Performs scalar multiplication, multiplying all components with a scalar.
|
||||||
public Vector multiply(int m) {
|
*
|
||||||
x *= m;
|
* @param m
|
||||||
y *= m;
|
* @return the same vector
|
||||||
z *= m;
|
*/
|
||||||
return this;
|
public Vector multiply(int m) {
|
||||||
}
|
x *= m;
|
||||||
|
y *= m;
|
||||||
/**
|
z *= m;
|
||||||
* Performs scalar multiplication, multiplying all components with a scalar.
|
return this;
|
||||||
*
|
}
|
||||||
* @param m
|
|
||||||
* @return the same vector
|
/**
|
||||||
*/
|
* Performs scalar multiplication, multiplying all components with a scalar.
|
||||||
public Vector multiply(double m) {
|
*
|
||||||
x *= m;
|
* @param m
|
||||||
y *= m;
|
* @return the same vector
|
||||||
z *= m;
|
*/
|
||||||
return this;
|
public Vector multiply(double m) {
|
||||||
}
|
x *= m;
|
||||||
|
y *= m;
|
||||||
/**
|
z *= m;
|
||||||
* Performs scalar multiplication, multiplying all components with a scalar.
|
return this;
|
||||||
*
|
}
|
||||||
* @param m
|
|
||||||
* @return the same vector
|
/**
|
||||||
*/
|
* Performs scalar multiplication, multiplying all components with a scalar.
|
||||||
public Vector multiply(float m) {
|
*
|
||||||
x *= m;
|
* @param m
|
||||||
y *= m;
|
* @return the same vector
|
||||||
z *= m;
|
*/
|
||||||
return this;
|
public Vector multiply(float m) {
|
||||||
}
|
x *= m;
|
||||||
|
y *= m;
|
||||||
/**
|
z *= m;
|
||||||
* Calculates the dot product of this vector with another. The dot product
|
return this;
|
||||||
* is defined as x1*x2+y1*y2+z1*z2. The returned value is a scalar.
|
}
|
||||||
*
|
|
||||||
* @param other
|
/**
|
||||||
* @return dot product
|
* 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.
|
||||||
public double getDotProduct(Vector other) {
|
*
|
||||||
return x * other.x + y * other.y + z * other.z;
|
* @param other
|
||||||
}
|
* @return dot product
|
||||||
|
*/
|
||||||
/**
|
public double getDotProduct(Vector other) {
|
||||||
* Calculates the cross product of this vector with another. The cross
|
return x * other.x + y * other.y + z * other.z;
|
||||||
* product is defined as:
|
}
|
||||||
*
|
|
||||||
* x = y1 * z2 - y2 * z1<br/>
|
/**
|
||||||
* y = z1 * x2 - z2 * x1<br/>
|
* Calculates the cross product of this vector with another. The cross
|
||||||
* z = x1 * y2 - x2 * y1
|
* product is defined as:
|
||||||
*
|
*
|
||||||
* @param o
|
* x = y1 * z2 - y2 * z1<br/>
|
||||||
* @return the same vector
|
* y = z1 * x2 - z2 * x1<br/>
|
||||||
*/
|
* z = x1 * y2 - x2 * y1
|
||||||
public Vector crossProduct(Vector o) {
|
*
|
||||||
double newX = y * o.z - o.y * z;
|
* @param o
|
||||||
double newY = z * o.x - o.z * x;
|
* @return the same vector
|
||||||
double newZ = x * o.y - o.x * y;
|
*/
|
||||||
x = newX;
|
public Vector crossProduct(Vector o) {
|
||||||
y = newY;
|
double newX = y * o.z - o.y * z;
|
||||||
z = newZ;
|
double newY = z * o.x - o.z * x;
|
||||||
return this;
|
double newZ = x * o.y - o.x * y;
|
||||||
}
|
x = newX;
|
||||||
|
y = newY;
|
||||||
/**
|
z = newZ;
|
||||||
* Converts this vector to a unit vector (a vector with length of 1).
|
return this;
|
||||||
*
|
}
|
||||||
* @return the same vector
|
|
||||||
*/
|
/**
|
||||||
public Vector unitVector() {
|
* Converts this vector to a unit vector (a vector with length of 1).
|
||||||
double length = length();
|
*
|
||||||
|
* @return the same vector
|
||||||
x /= length;
|
*/
|
||||||
y /= length;
|
public Vector unitVector() {
|
||||||
z /= length;
|
double length = length();
|
||||||
|
|
||||||
return this;
|
x /= length;
|
||||||
}
|
y /= length;
|
||||||
|
z /= length;
|
||||||
/**
|
|
||||||
* Gets a unit vector of this vector. This vector will not be chagned.
|
return this;
|
||||||
*
|
}
|
||||||
* @return a brand new vector
|
|
||||||
*/
|
/**
|
||||||
public Vector getUnitVector() {
|
* Gets a unit vector of this vector. This vector will not be chagned.
|
||||||
double length = length();
|
*
|
||||||
return new Vector(x / length, y / length, z / length);
|
* @return a brand new vector
|
||||||
}
|
*/
|
||||||
|
public Vector getUnitVector() {
|
||||||
/**
|
double length = length();
|
||||||
* Returns whether this vector is in a cuboid. The minimum and maximum
|
return new Vector(x / length, y / length, z / length);
|
||||||
* vectors given must be truly the minimum and maximum X, Y and Z
|
}
|
||||||
* components.
|
|
||||||
*
|
/**
|
||||||
* @param min
|
* Returns whether this vector is in a cuboid. The minimum and maximum
|
||||||
* @param max
|
* vectors given must be truly the minimum and maximum X, Y and Z
|
||||||
* @return whether this vector is in the cuboid
|
* components.
|
||||||
*/
|
*
|
||||||
public boolean isInCuboid(Vector min, Vector max) {
|
* @param min
|
||||||
return x >= min.x && x <= max.x
|
* @param max
|
||||||
&& y >= min.y && y <= max.y
|
* @return whether this vector is in the cuboid
|
||||||
&& z >= min.z && z <= max.z;
|
*/
|
||||||
}
|
public boolean isInCuboid(Vector min, Vector max) {
|
||||||
|
return x >= min.x && x <= max.x
|
||||||
/**
|
&& y >= min.y && y <= max.y
|
||||||
* Returns whether this vector is within a sphere.
|
&& z >= min.z && z <= max.z;
|
||||||
*
|
}
|
||||||
* @param origin
|
|
||||||
* @param radius
|
/**
|
||||||
* @return whether this vector is in the sphere
|
* Returns whether this vector is within a sphere.
|
||||||
*/
|
*
|
||||||
public boolean isInSphere(Vector origin, double radius) {
|
* @param origin
|
||||||
return origin.clone().subtract(this).length() <= radius;
|
* @param radius
|
||||||
}
|
* @return whether this vector is in the sphere
|
||||||
|
*/
|
||||||
public double getX() {
|
public boolean isInSphere(Vector origin, double radius) {
|
||||||
return x;
|
return (Math.pow(origin.x - x, 2)
|
||||||
}
|
+ Math.pow(origin.y - y, 2)
|
||||||
|
+ Math.pow(origin.z - z, 2))
|
||||||
public double getY() {
|
<= Math.pow(radius, 2);
|
||||||
return y;
|
}
|
||||||
}
|
|
||||||
|
public double getX() {
|
||||||
public double getZ() {
|
return x;
|
||||||
return z;
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
public Vector setX(int x) {
|
* Gets the floored value of the X component, indicating the block that
|
||||||
this.x = x;
|
* this vector is contained with.
|
||||||
return this;
|
*
|
||||||
}
|
* @return block X
|
||||||
|
*/
|
||||||
public Vector setX(double x) {
|
public int getBlockX() {
|
||||||
this.x = x;
|
return (int)Math.floor(x);
|
||||||
return this;
|
}
|
||||||
}
|
|
||||||
|
public double getY() {
|
||||||
public Vector setX(float x) {
|
return y;
|
||||||
this.x = x;
|
}
|
||||||
return this;
|
|
||||||
}
|
/**
|
||||||
|
* Gets the floored value of the Y component, indicating the block that
|
||||||
public Vector setY(int y) {
|
* this vector is contained with.
|
||||||
this.y = y;
|
*
|
||||||
return this;
|
* @return block y
|
||||||
}
|
*/
|
||||||
|
public int getBlockY() {
|
||||||
public Vector setY(double y) {
|
return (int)Math.floor(y);
|
||||||
this.y = y;
|
}
|
||||||
return this;
|
|
||||||
}
|
public double getZ() {
|
||||||
|
return z;
|
||||||
public Vector setY(float y) {
|
}
|
||||||
this.y = y;
|
|
||||||
return this;
|
/**
|
||||||
}
|
* Gets the floored value of the Z component, indicating the block that
|
||||||
|
* this vector is contained with.
|
||||||
public Vector setZ(int z) {
|
*
|
||||||
this.z = z;
|
* @return block z
|
||||||
return this;
|
*/
|
||||||
}
|
public int getBlockZ() {
|
||||||
|
return (int)Math.floor(z);
|
||||||
public Vector setZ(double z) {
|
}
|
||||||
this.z = z;
|
|
||||||
return this;
|
public Vector setX(int x) {
|
||||||
}
|
this.x = x;
|
||||||
|
return this;
|
||||||
public Vector setZ(float z) {
|
}
|
||||||
this.z = z;
|
|
||||||
return this;
|
public Vector setX(double x) {
|
||||||
}
|
this.x = x;
|
||||||
|
return this;
|
||||||
@Override
|
}
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (!(obj instanceof Vector)) {
|
public Vector setX(float x) {
|
||||||
return false;
|
this.x = x;
|
||||||
}
|
return this;
|
||||||
|
}
|
||||||
Vector other = (Vector)obj;
|
|
||||||
|
public Vector setY(int y) {
|
||||||
return Double.doubleToLongBits(x) == Double.doubleToLongBits(other.x)
|
this.y = y;
|
||||||
&& Double.doubleToLongBits(y) == Double.doubleToLongBits(other.y)
|
return this;
|
||||||
&& Double.doubleToLongBits(z) == Double.doubleToLongBits(other.z);
|
}
|
||||||
}
|
|
||||||
|
public Vector setY(double y) {
|
||||||
@Override
|
this.y = y;
|
||||||
public int hashCode() {
|
return this;
|
||||||
return ((int)Double.doubleToLongBits(x) >> 13) ^
|
}
|
||||||
((int)Double.doubleToLongBits(y) >> 7) ^
|
|
||||||
(int)Double.doubleToLongBits(z);
|
public Vector setY(float y) {
|
||||||
}
|
this.y = y;
|
||||||
|
return this;
|
||||||
@Override
|
}
|
||||||
public Vector clone() {
|
|
||||||
return new Vector(x, y, z);
|
public Vector setZ(int z) {
|
||||||
}
|
this.z = z;
|
||||||
|
return this;
|
||||||
@Override
|
}
|
||||||
public String toString() {
|
|
||||||
return x + "," + y + "," + z;
|
public Vector setZ(double z) {
|
||||||
}
|
this.z = z;
|
||||||
|
return this;
|
||||||
public Location toLocation(World world) {
|
}
|
||||||
return new Location(world, x, y, z);
|
|
||||||
}
|
public Vector setZ(float z) {
|
||||||
|
this.z = z;
|
||||||
public Location toLocation(World world, float yaw, float pitch) {
|
return this;
|
||||||
return new Location(world, x, y, z, yaw, pitch);
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
/**
|
public boolean equals(Object obj) {
|
||||||
* Gets the minimum components of two vectors.
|
if (!(obj instanceof Vector)) {
|
||||||
*
|
return false;
|
||||||
* @param v1
|
}
|
||||||
* @param v2
|
|
||||||
* @return minimum
|
Vector other = (Vector)obj;
|
||||||
*/
|
|
||||||
public static Vector getMinimum(Vector v1, Vector v2) {
|
return Double.doubleToLongBits(x) == Double.doubleToLongBits(other.x)
|
||||||
return new Vector(
|
&& Double.doubleToLongBits(y) == Double.doubleToLongBits(other.y)
|
||||||
Math.min(v1.x, v2.x),
|
&& Double.doubleToLongBits(z) == Double.doubleToLongBits(other.z);
|
||||||
Math.min(v1.y, v2.y),
|
}
|
||||||
Math.min(v1.z, v2.z));
|
|
||||||
}
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
/**
|
return ((int)Double.doubleToLongBits(x) >> 13) ^
|
||||||
* Gets the maximum components of two vectors.
|
((int)Double.doubleToLongBits(y) >> 7) ^
|
||||||
*
|
(int)Double.doubleToLongBits(z);
|
||||||
* @param v1
|
}
|
||||||
* @param v2
|
|
||||||
* @return maximum
|
@Override
|
||||||
*/
|
public Vector clone() {
|
||||||
public static Vector getMaximum(Vector v1, Vector v2) {
|
return new Vector(x, y, z);
|
||||||
return new Vector(
|
}
|
||||||
Math.max(v1.x, v2.x),
|
|
||||||
Math.max(v1.y, v2.y),
|
@Override
|
||||||
Math.max(v1.z, v2.z));
|
public String toString() {
|
||||||
}
|
return x + "," + y + "," + z;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Location toLocation(World world) {
|
||||||
|
return new Location(world, x, y, z);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Location toLocation(World world, float yaw, float pitch) {
|
||||||
|
return new Location(world, x, y, z, yaw, pitch);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the minimum components of two vectors.
|
||||||
|
*
|
||||||
|
* @param v1
|
||||||
|
* @param v2
|
||||||
|
* @return minimum
|
||||||
|
*/
|
||||||
|
public static Vector getMinimum(Vector v1, Vector v2) {
|
||||||
|
return new Vector(
|
||||||
|
Math.min(v1.x, v2.x),
|
||||||
|
Math.min(v1.y, v2.y),
|
||||||
|
Math.min(v1.z, v2.z));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the maximum components of two vectors.
|
||||||
|
*
|
||||||
|
* @param v1
|
||||||
|
* @param v2
|
||||||
|
* @return maximum
|
||||||
|
*/
|
||||||
|
public static Vector getMaximum(Vector v1, Vector v2) {
|
||||||
|
return new Vector(
|
||||||
|
Math.max(v1.x, v2.x),
|
||||||
|
Math.max(v1.y, v2.y),
|
||||||
|
Math.max(v1.z, v2.z));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue