diff --git a/src/main/java/com/sk89q/worldedit/Vector.java b/src/main/java/com/sk89q/worldedit/Vector.java index 1c241ac7d..1082c16d2 100644 --- a/src/main/java/com/sk89q/worldedit/Vector.java +++ b/src/main/java/com/sk89q/worldedit/Vector.java @@ -600,6 +600,43 @@ public class Vector { ); } + public boolean isCollinearWith(Vector other) { + if (x == 0 && y == 0 && z == 0) { + // this is a zero vector + return true; + } + + final double otherX = other.x; + final double otherY = other.y; + final double otherZ = other.z; + + if (otherX == 0 && otherY == 0 && otherZ == 0) { + // other is a zero vector + return true; + } + + if ((x == 0) != (otherX == 0)) return false; + if ((y == 0) != (otherY == 0)) return false; + if ((z == 0) != (otherZ == 0)) return false; + + final double quotientX = otherX / x; + if (!Double.isNaN(quotientX)) { + return other.equals(multiply(quotientX)); + } + + final double quotientY = otherY / y; + if (!Double.isNaN(quotientY)) { + return other.equals(multiply(quotientY)); + } + + final double quotientZ = otherZ / z; + if (!Double.isNaN(quotientZ)) { + return other.equals(multiply(quotientZ)); + } + + throw new RuntimeException("This should not happen"); + } + /** * Get a block point from a point. * @@ -643,7 +680,6 @@ public class Vector { Vector other = (Vector) obj; return other.getX() == this.x && other.getY() == this.y && other.getZ() == this.z; - } /** diff --git a/src/test/java/com/sk89q/worldedit/VectorTest.java b/src/test/java/com/sk89q/worldedit/VectorTest.java new file mode 100644 index 000000000..1da315002 --- /dev/null +++ b/src/test/java/com/sk89q/worldedit/VectorTest.java @@ -0,0 +1,70 @@ +package com.sk89q.worldedit; + +import org.junit.*; +import static org.junit.Assert.*; + +public class VectorTest { + @Test + public void collinearityTest() { + assertCollinear(0,0,0, 0,0,0); + + assertCollinear(0,0,0, 1,0,0); + assertCollinear(0,0,0, 0,1,0); + assertCollinear(0,0,0, 0,0,1); + + assertCollinear(1,0,0, 0,0,0); + assertCollinear(0,1,0, 0,0,0); + assertCollinear(0,0,1, 0,0,0); + + assertCollinear(1,0,0, 2,0,0); + assertNotCollinear(1,0,0, 0,1,0); + + assertNotCollinear(2,2,2, 8,4,4); + assertCollinear(8,2,2, 8,2,2); + assertNotCollinear(4,2,4, 4,4,4); + assertNotCollinear(1,1,2, 4,8,2); + assertNotCollinear(4,1,8, 1,4,4); + assertCollinear(2,4,2, 1,2,1); + assertNotCollinear(2,2,4, 1,2,1); + assertNotCollinear(4,4,1, 4,4,4); + assertNotCollinear(4,1,4, 1,8,2); + assertCollinear(8,8,4, 4,4,2); + assertNotCollinear(2,1,8, 1,1,2); + assertNotCollinear(8,1,2, 2,1,2); + assertNotCollinear(4,4,8, 2,2,8); + assertNotCollinear(8,4,8, 1,4,8); + assertNotCollinear(2,2,2, 1,4,2); + assertNotCollinear(1,1,2, 8,8,2); + assertNotCollinear(4,4,8, 8,4,4); + assertNotCollinear(1,8,2, 4,4,4); + assertNotCollinear(8,4,2, 1,2,2); + assertNotCollinear(1,8,2, 8,1,4); + assertNotCollinear(4,8,1, 4,8,8); + assertNotCollinear(8,1,8, 8,8,8); + assertNotCollinear(8,4,1, 4,2,2); + assertNotCollinear(4,8,1, 4,2,1); + assertNotCollinear(8,8,1, 2,4,2); + assertCollinear(8,1,4, 8,1,4); + assertNotCollinear(4,1,1, 2,4,8); + assertNotCollinear(4,2,8, 1,4,1); + assertNotCollinear(1,8,2, 1,8,1); + assertNotCollinear(1,1,2, 4,2,2); + } + + private void assertCollinear(double ax, double ay, double az, double bx, double by, double bz) { + final Vector a = new Vector(ax,ay,az); + final Vector b = new Vector(bx,by,bz); + assertTrue(a.isCollinearWith(b)); + assertTrue(b.isCollinearWith(a)); + assertTrue(a.multiply(-1).isCollinearWith(b)); + assertTrue(a.isCollinearWith(b.multiply(-1))); + } + private void assertNotCollinear(double ax, double ay, double az, double bx, double by, double bz) { + final Vector a = new Vector(ax,ay,az); + final Vector b = new Vector(bx,by,bz); + assertFalse(a.isCollinearWith(b)); + assertFalse(b.isCollinearWith(a)); + assertFalse(a.multiply(-1).isCollinearWith(b)); + assertFalse(a.isCollinearWith(b.multiply(-1))); + } +}