Made Vector.equals() fuzzy.

By: sk89q <the.sk89q@gmail.com>
This commit is contained in:
Bukkit/Spigot 2011-01-03 17:55:42 +08:00
parent 04c824307a
commit 6284b8117c

View file

@ -8,6 +8,11 @@ package org.bukkit;
public class Vector implements Cloneable { public class Vector implements Cloneable {
private static final long serialVersionUID = -2657651106777219169L; private static final long serialVersionUID = -2657651106777219169L;
/**
* Threshold for fuzzy equals().
*/
private static final double epsilon = 0.000001;
protected double x; protected double x;
protected double y; protected double y;
protected double z; protected double z;
@ -321,6 +326,10 @@ public class Vector implements Cloneable {
return this; return this;
} }
/**
* Checks to see if two objects are equal. Only two Vectors can ever
* return true
*/
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (!(obj instanceof Vector)) { if (!(obj instanceof Vector)) {
@ -329,9 +338,9 @@ public class Vector implements Cloneable {
Vector other = (Vector)obj; Vector other = (Vector)obj;
return Double.doubleToLongBits(x) == Double.doubleToLongBits(other.x) return Math.abs(x - other.x) < epsilon
&& Double.doubleToLongBits(y) == Double.doubleToLongBits(other.y) && Math.abs(y - other.y) < epsilon
&& Double.doubleToLongBits(z) == Double.doubleToLongBits(other.z); && Math.abs(z - other.z) < epsilon;
} }
@Override @Override
@ -359,6 +368,15 @@ public class Vector implements Cloneable {
return new Location(world, x, y, z, yaw, pitch); 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. * Gets the minimum components of two vectors.
* *