From 6284b8117ce0d1d52b50223ebfba98abb112b4f7 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Mon, 3 Jan 2011 17:55:42 +0800 Subject: [PATCH] Made Vector.equals() fuzzy. By: sk89q --- .../src/main/java/org/bukkit/Vector.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/Vector.java b/paper-api/src/main/java/org/bukkit/Vector.java index b5ee2bde60..11b56f4098 100644 --- a/paper-api/src/main/java/org/bukkit/Vector.java +++ b/paper-api/src/main/java/org/bukkit/Vector.java @@ -8,6 +8,11 @@ package org.bukkit; public class Vector implements Cloneable { private static final long serialVersionUID = -2657651106777219169L; + /** + * Threshold for fuzzy equals(). + */ + private static final double epsilon = 0.000001; + protected double x; protected double y; protected double z; @@ -321,6 +326,10 @@ public class Vector implements Cloneable { return this; } + /** + * Checks to see if two objects are equal. Only two Vectors can ever + * return true + */ @Override public boolean equals(Object obj) { if (!(obj instanceof Vector)) { @@ -329,9 +338,9 @@ public class Vector implements Cloneable { Vector other = (Vector)obj; - return Double.doubleToLongBits(x) == Double.doubleToLongBits(other.x) - && Double.doubleToLongBits(y) == Double.doubleToLongBits(other.y) - && Double.doubleToLongBits(z) == Double.doubleToLongBits(other.z); + return Math.abs(x - other.x) < epsilon + && Math.abs(y - other.y) < epsilon + && Math.abs(z - other.z) < epsilon; } @Override @@ -359,6 +368,15 @@ public class Vector implements Cloneable { return new Location(world, x, y, z, yaw, pitch); } + /** + * Get the threshold used for equals(). + * + * @return + */ + public static double getEpsilon() { + return epsilon; + } + /** * Gets the minimum components of two vectors. *