diff options
author | Wesley Wolfe <weswolf@aol.com> | 2013-10-09 01:56:35 -0500 |
---|---|---|
committer | Wesley Wolfe <weswolf@aol.com> | 2013-10-09 01:56:35 -0500 |
commit | bcab013d15ec8332521b5e42ab9b2bba673f6c20 (patch) | |
tree | aa9f14d430b79530a4686c530bfea14a7705a803 /src | |
parent | e76be09ae5699b933b06b31d242e233740f2b32a (diff) | |
download | bukkit-bcab013d15ec8332521b5e42ab9b2bba673f6c20.tar bukkit-bcab013d15ec8332521b5e42ab9b2bba673f6c20.tar.gz bukkit-bcab013d15ec8332521b5e42ab9b2bba673f6c20.tar.lz bukkit-bcab013d15ec8332521b5e42ab9b2bba673f6c20.tar.xz bukkit-bcab013d15ec8332521b5e42ab9b2bba673f6c20.zip |
Use simple multiplication for squaring. Fixes BUKKIT-4836
This change adds a method to NumberConversions for squaring and
replaces uses of Math.pow(..., 2) with the new method for efficiency
reasons.
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/bukkit/Location.java | 6 | ||||
-rw-r--r-- | src/main/java/org/bukkit/util/NumberConversions.java | 4 | ||||
-rw-r--r-- | src/main/java/org/bukkit/util/Vector.java | 10 |
3 files changed, 12 insertions, 8 deletions
diff --git a/src/main/java/org/bukkit/Location.java b/src/main/java/org/bukkit/Location.java index bcc963bf..e3f362fd 100644 --- a/src/main/java/org/bukkit/Location.java +++ b/src/main/java/org/bukkit/Location.java @@ -334,7 +334,7 @@ public class Location implements Cloneable { * @return the magnitude */ public double length() { - return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)); + return Math.sqrt(NumberConversions.square(x) + NumberConversions.square(y) + NumberConversions.square(z)); } /** @@ -345,7 +345,7 @@ public class Location implements Cloneable { * @return the magnitude */ public double lengthSquared() { - return Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2); + return NumberConversions.square(x) + NumberConversions.square(y) + NumberConversions.square(z); } /** @@ -381,7 +381,7 @@ public class Location implements Cloneable { throw new IllegalArgumentException("Cannot measure distance between " + getWorld().getName() + " and " + o.getWorld().getName()); } - return Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2) + Math.pow(z - o.z, 2); + return NumberConversions.square(x - o.x) + NumberConversions.square(y - o.y) + NumberConversions.square(z - o.z); } /** diff --git a/src/main/java/org/bukkit/util/NumberConversions.java b/src/main/java/org/bukkit/util/NumberConversions.java index dba11afb..29f81d01 100644 --- a/src/main/java/org/bukkit/util/NumberConversions.java +++ b/src/main/java/org/bukkit/util/NumberConversions.java @@ -20,6 +20,10 @@ public final class NumberConversions { return floor(num + 0.5d); } + public static double square(double num) { + return num * num; + } + public static int toInt(Object object) { if (object instanceof Number) { return ((Number) object).intValue(); diff --git a/src/main/java/org/bukkit/util/Vector.java b/src/main/java/org/bukkit/util/Vector.java index 30547c52..de30d2ba 100644 --- a/src/main/java/org/bukkit/util/Vector.java +++ b/src/main/java/org/bukkit/util/Vector.java @@ -152,7 +152,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return the magnitude */ public double length() { - return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)); + return Math.sqrt(NumberConversions.square(x) + NumberConversions.square(y) + NumberConversions.square(z)); } /** @@ -161,7 +161,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return the magnitude */ public double lengthSquared() { - return Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2); + return NumberConversions.square(x) + NumberConversions.square(y) + NumberConversions.square(z); } /** @@ -175,7 +175,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return the distance */ public double distance(Vector o) { - return Math.sqrt(Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2) + Math.pow(z - o.z, 2)); + return Math.sqrt(NumberConversions.square(x - o.x) + NumberConversions.square(y - o.y) + NumberConversions.square(z - o.z)); } /** @@ -185,7 +185,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @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); + return NumberConversions.square(x - o.x) + NumberConversions.square(y - o.y) + NumberConversions.square(z - o.z); } /** @@ -346,7 +346,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return whether this vector is in the sphere */ public boolean isInSphere(Vector origin, double radius) { - return (Math.pow(origin.x - x, 2) + Math.pow(origin.y - y, 2) + Math.pow(origin.z - z, 2)) <= Math.pow(radius, 2); + return (NumberConversions.square(origin.x - x) + NumberConversions.square(origin.y - y) + NumberConversions.square(origin.z - z)) <= NumberConversions.square(radius); } /** |