Improved Vector and Location with some utility methods, optimized Vector.isInSphere().

By: sk89q <the.sk89q@gmail.com>
This commit is contained in:
Bukkit/Spigot 2011-01-02 10:42:13 -08:00
parent f6873cab82
commit 61b42932b1
2 changed files with 419 additions and 352 deletions

View file

@ -61,6 +61,16 @@ public class Location implements Cloneable {
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
*
@ -79,6 +89,16 @@ public class Location implements Cloneable {
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
*
@ -97,6 +117,16 @@ public class Location implements Cloneable {
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
*

View file

@ -85,7 +85,9 @@ public class Vector implements Cloneable {
/**
* 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.
* 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
*/
@ -96,7 +98,9 @@ public class Vector implements Cloneable {
/**
* 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.
* 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.
*
* @return the distance
*/
@ -224,21 +228,54 @@ public class Vector implements Cloneable {
* @return whether this vector is in the sphere
*/
public boolean isInSphere(Vector origin, double radius) {
return origin.clone().subtract(this).length() <= radius;
return (Math.pow(origin.x - x, 2)
+ Math.pow(origin.y - y, 2)
+ Math.pow(origin.z - z, 2))
<= Math.pow(radius, 2);
}
public double getX() {
return x;
}
/**
* Gets the floored value of the X component, indicating the block that
* this vector is contained with.
*
* @return block X
*/
public int getBlockX() {
return (int)Math.floor(x);
}
public double getY() {
return y;
}
/**
* Gets the floored value of the Y component, indicating the block that
* this vector is contained with.
*
* @return block y
*/
public int getBlockY() {
return (int)Math.floor(y);
}
public double getZ() {
return z;
}
/**
* Gets the floored value of the Z component, indicating the block that
* this vector is contained with.
*
* @return block z
*/
public int getBlockZ() {
return (int)Math.floor(z);
}
public Vector setX(int x) {
this.x = x;
return this;