mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-10-26 14:00:14 +00:00
implement robust equals and hashcode in Vector3
This commit is contained in:
1 parent
058ec9f24d
commit
c8434e73ef
1 file changed
+36
-3
@@ -303,9 +303,42 @@ public class Vector3 implements Cloneable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Vector3 subtract(Vector3 end) {
|
public Vector3 subtract(Vector3 end) {
|
||||||
x-=end.x;
|
x -= end.x;
|
||||||
y-=end.y;
|
y -= end.y;
|
||||||
z-=end.z;
|
z -= end.z;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a hash code for this vector
|
||||||
|
*
|
||||||
|
* @return hash code
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int hash = 7;
|
||||||
|
|
||||||
|
hash = 79 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
|
||||||
|
hash = 79 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
|
||||||
|
hash = 79 * hash + (int) (Double.doubleToLongBits(this.z) ^ (Double.doubleToLongBits(this.z) >>> 32));
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks to see if two objects are equal.
|
||||||
|
* <p>
|
||||||
|
* Only two Vectors can ever return true. This method uses a fuzzy match
|
||||||
|
* to account for floating point errors. The epsilon can be retrieved
|
||||||
|
* with epsilon.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(!(obj instanceof Vector3)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector3 other = (Vector3) obj;
|
||||||
|
|
||||||
|
return Math.abs(x - other.x) < epsilon && Math.abs(y - other.y) < epsilon && Math.abs(z - other.z) < epsilon && (this.getClass().equals(obj.getClass()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user