summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorsk89q <the.sk89q@gmail.com>2011-01-03 18:10:17 +0800
committerDinner Bone <dinnerbone@dinnerbone.com>2011-01-03 22:59:04 +0800
commit49d6f74dcaa966b46f2ec6823c8b4d18304cd893 (patch)
treee67059bc4ce5253bbbd29fdc2c62db53de9f03b8 /src
parentb3bc868ce372aa25351087194c732e4c4db39da5 (diff)
downloadbukkit-49d6f74dcaa966b46f2ec6823c8b4d18304cd893.tar
bukkit-49d6f74dcaa966b46f2ec6823c8b4d18304cd893.tar.gz
bukkit-49d6f74dcaa966b46f2ec6823c8b4d18304cd893.tar.lz
bukkit-49d6f74dcaa966b46f2ec6823c8b4d18304cd893.tar.xz
bukkit-49d6f74dcaa966b46f2ec6823c8b4d18304cd893.zip
Added more math to Vector, renamed some methods.
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/bukkit/Vector.java68
1 files changed, 51 insertions, 17 deletions
diff --git a/src/main/java/org/bukkit/Vector.java b/src/main/java/org/bukkit/Vector.java
index 11b56f40..cf72898b 100644
--- a/src/main/java/org/bukkit/Vector.java
+++ b/src/main/java/org/bukkit/Vector.java
@@ -101,6 +101,15 @@ public class Vector implements Cloneable {
}
/**
+ * Gets the magnitude of the vector squared.
+ *
+ * @return the magnitude
+ */
+ public double lengthSquared() {
+ return 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. NaN
@@ -115,6 +124,41 @@ public class Vector implements Cloneable {
}
/**
+ * Get the squared distance between this vector and another.
+ *
+ * @return the distance
+ */
+ public double distanceSquared(Vector o) {
+ return Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2)
+ + Math.pow(z - o.z, 2);
+ }
+
+ /**
+ * Gets the angle between this vector and another in radians.
+ *
+ * @param other
+ * @return angle in radians
+ */
+ public float angle(Vector other) {
+ double dot = dot(other) / (length() * other.length());
+ return (float)Math.acos(dot);
+ }
+
+ /**
+ * Sets this vector to the midpoint between this vector and another.
+ * You may want to use Vector.clone() to keep the old vector unchanged.
+ *
+ * @param other
+ * @return this same vector (now a midpoint)
+ */
+ public Vector midpoint(Vector other) {
+ x = (x + other.x) / 2;
+ y = (y + other.y) / 2;
+ z = (z + other.z) / 2;
+ return this;
+ }
+
+ /**
* Performs scalar multiplication, multiplying all components with a scalar.
*
* @param m
@@ -160,7 +204,7 @@ public class Vector implements Cloneable {
* @param other
* @return dot product
*/
- public double getDotProduct(Vector other) {
+ public double dot(Vector other) {
return x * other.x + y * other.y + z * other.z;
}
@@ -190,7 +234,7 @@ public class Vector implements Cloneable {
*
* @return the same vector
*/
- public Vector unitVector() {
+ public Vector normalize() {
double length = length();
x /= length;
@@ -201,25 +245,15 @@ public class Vector implements Cloneable {
}
/**
- * Gets a unit vector of this vector. This vector will not be chagned.
- *
- * @return a brand new vector
- */
- public Vector getUnitVector() {
- double length = length();
- return new Vector(x / length, y / length, z / length);
- }
-
- /**
- * Returns whether this vector is in a cuboid. The minimum and maximum
- * vectors given must be truly the minimum and maximum X, Y and Z
- * components.
+ * Returns whether this vector is in an axis-aligned bounding box.
+ * The minimum and maximum vectors given must be truly the minimum and
+ * maximum X, Y and Z components.
*
* @param min
* @param max
- * @return whether this vector is in the cuboid
+ * @return whether this vector is in the AABB
*/
- public boolean isInCuboid(Vector min, Vector max) {
+ public boolean isInAABB(Vector min, Vector max) {
return x >= min.x && x <= max.x
&& y >= min.y && y <= max.y
&& z >= min.z && z <= max.z;