From 12dbcf6bbc39c1fee42a19f1a1c1ef15a0e11304 Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Thu, 5 Jan 2012 21:07:24 +0100 Subject: [PATCH] Added Vector2D.isCollinearWith(Vector2D other). Also added a test for it. --- .../java/com/sk89q/worldedit/Vector2D.java | 30 +++++++++ .../java/com/sk89q/worldedit/VectorTest.java | 61 +++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/src/main/java/com/sk89q/worldedit/Vector2D.java b/src/main/java/com/sk89q/worldedit/Vector2D.java index ea49fa2ec..6055d9d8c 100644 --- a/src/main/java/com/sk89q/worldedit/Vector2D.java +++ b/src/main/java/com/sk89q/worldedit/Vector2D.java @@ -524,6 +524,36 @@ public class Vector2D { ); } + public boolean isCollinearWith(Vector2D other) { + if (x == 0 && z == 0) { + // this is a zero vector + return true; + } + + final double otherX = other.x; + final double otherZ = other.z; + + if (otherX == 0 && otherZ == 0) { + // other is a zero vector + return true; + } + + if ((x == 0) != (otherX == 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 quotientZ = otherZ / z; + if (!Double.isNaN(quotientZ)) { + return other.equals(multiply(quotientZ)); + } + + throw new RuntimeException("This should not happen"); + } + /** * Gets a BlockVector version. * diff --git a/src/test/java/com/sk89q/worldedit/VectorTest.java b/src/test/java/com/sk89q/worldedit/VectorTest.java index 1da315002..eece15fb3 100644 --- a/src/test/java/com/sk89q/worldedit/VectorTest.java +++ b/src/test/java/com/sk89q/worldedit/VectorTest.java @@ -49,6 +49,50 @@ public class VectorTest { assertNotCollinear(4,2,8, 1,4,1); assertNotCollinear(1,8,2, 1,8,1); assertNotCollinear(1,1,2, 4,2,2); + + assertCollinear(0,0, 0,0); + + assertCollinear(0,0, 1,0); + assertCollinear(0,0, 0,1); + assertCollinear(0,0, 0,0); + + assertCollinear(1,0, 0,0); + assertCollinear(0,1, 0,0); + assertCollinear(0,0, 0,0); + + assertCollinear(1,0, 2,0); + assertNotCollinear(1,0, 0,1); + + assertNotCollinear(2,2, 8,4); + assertCollinear(8,2, 8,2); + assertNotCollinear(4,2, 4,4); + assertNotCollinear(1,1, 4,8); + assertNotCollinear(4,1, 1,4); + assertCollinear(2,4, 1,2); + assertNotCollinear(2,2, 1,2); + assertCollinear(4,4, 4,4); + assertNotCollinear(4,1, 1,8); + assertCollinear(8,8, 4,4); + assertNotCollinear(2,1, 1,1); + assertNotCollinear(8,1, 2,1); + assertCollinear(4,4, 2,2); + assertNotCollinear(8,4, 1,4); + assertNotCollinear(2,2, 1,4); + assertCollinear(1,1, 8,8); + assertNotCollinear(4,4, 8,4); + assertNotCollinear(1,8, 4,4); + assertNotCollinear(8,4, 1,2); + assertNotCollinear(1,8, 8,1); + assertCollinear(4,8, 4,8); + assertNotCollinear(8,1, 8,8); + assertCollinear(8,4, 4,2); + assertNotCollinear(4,8, 4,2); + assertNotCollinear(8,8, 2,4); + assertCollinear(8,1, 8,1); + assertNotCollinear(4,1, 2,4); + assertNotCollinear(4,2, 1,4); + assertCollinear(1,8, 1,8); + assertNotCollinear(1,1, 4,2); } private void assertCollinear(double ax, double ay, double az, double bx, double by, double bz) { @@ -67,4 +111,21 @@ public class VectorTest { assertFalse(a.multiply(-1).isCollinearWith(b)); assertFalse(a.isCollinearWith(b.multiply(-1))); } + + private void assertCollinear(double ax, double az, double bx, double bz) { + final Vector2D a = new Vector2D(ax,az); + final Vector2D b = new Vector2D(bx,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 az, double bx, double bz) { + final Vector2D a = new Vector2D(ax,az); + final Vector2D b = new Vector2D(bx,bz); + assertFalse(a.isCollinearWith(b)); + assertFalse(b.isCollinearWith(a)); + assertFalse(a.multiply(-1).isCollinearWith(b)); + assertFalse(a.isCollinearWith(b.multiply(-1))); + } }