From 5e5367486d9af6a2dfaa5f659a19faa944109a9d Mon Sep 17 00:00:00 2001 From: Ivan Kupriyanov Date: Wed, 4 Oct 2017 19:24:57 +0300 Subject: [PATCH 1/5] New methods and test for DoubleRectangle --- .../jetpad/geometry/DoubleRectangle.java | 29 +++++++++++ .../geometry/DoubleRectangleEdgesTest.java | 50 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 util/geometry/src/test/java/jetbrains/jetpad/geometry/DoubleRectangleEdgesTest.java diff --git a/util/geometry/src/main/java/jetbrains/jetpad/geometry/DoubleRectangle.java b/util/geometry/src/main/java/jetbrains/jetpad/geometry/DoubleRectangle.java index 22ea5905..e48416bf 100644 --- a/util/geometry/src/main/java/jetbrains/jetpad/geometry/DoubleRectangle.java +++ b/util/geometry/src/main/java/jetbrains/jetpad/geometry/DoubleRectangle.java @@ -136,6 +136,34 @@ public double distance(DoubleVector to) { return result; } + public DoubleSegment getLeftEdge() { + return new DoubleSegment( + origin, + new DoubleVector(getLeft(), getBottom()) + ); + } + + public DoubleSegment getTopEdge() { + return new DoubleSegment( + origin, + new DoubleVector(getRight(), getTop()) + ); + } + + public DoubleSegment getRightEdge() { + return new DoubleSegment( + new DoubleVector(getRight(), getTop()), + origin.add(dimension) + ); + } + + public DoubleSegment getBottomEdge() { + return new DoubleSegment( + new DoubleVector(getLeft(), getBottom()), + origin.add(dimension) + ); + } + public Iterable getParts() { List result = new ArrayList<>(); result.add(new DoubleSegment(origin, origin.add(new DoubleVector(dimension.x, 0)))); @@ -163,4 +191,5 @@ public boolean equals(Object o) { public String toString() { return "[rect " + origin + ", " + dimension + "]"; } + } \ No newline at end of file diff --git a/util/geometry/src/test/java/jetbrains/jetpad/geometry/DoubleRectangleEdgesTest.java b/util/geometry/src/test/java/jetbrains/jetpad/geometry/DoubleRectangleEdgesTest.java new file mode 100644 index 00000000..9eb9d416 --- /dev/null +++ b/util/geometry/src/test/java/jetbrains/jetpad/geometry/DoubleRectangleEdgesTest.java @@ -0,0 +1,50 @@ +package jetbrains.jetpad.geometry; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class DoubleRectangleEdgesTest { + + private static final double LEFT = 5.; + private static final double RIGHT = 15.; + + private static final double TOP = 40.; + private static final double BOTTOM = 60.; + + private static final DoubleVector LEFT_TOP = new DoubleVector(LEFT, TOP); + private static final DoubleVector LEFT_BOTTOM = new DoubleVector(LEFT, BOTTOM); + private static final DoubleVector RIGHT_TOP = new DoubleVector(RIGHT, TOP); + private static final DoubleVector RIGHT_BOTTOM = new DoubleVector(RIGHT, BOTTOM); + + private static final DoubleSegment LEFT_EDGE = edge(LEFT_TOP, LEFT_BOTTOM); + private static final DoubleSegment BOTTOM_EDGE = edge(LEFT_BOTTOM, RIGHT_BOTTOM); + private static final DoubleSegment RIGHT_EDGE = edge(RIGHT_TOP, RIGHT_BOTTOM); + private static final DoubleSegment TOP_EDGE = edge(LEFT_TOP, RIGHT_TOP); + + private static final DoubleRectangle RECTANGLE = new DoubleRectangle(LEFT_TOP, RIGHT_BOTTOM.subtract(LEFT_TOP)); + + @Test + public void checkLeftEdge() throws Exception { + assertEquals(LEFT_EDGE, RECTANGLE.getLeftEdge()); + } + + @Test + public void checkTopEdge() throws Exception { + assertEquals(TOP_EDGE, RECTANGLE.getTopEdge()); + } + + @Test + public void checkRightEdge() throws Exception { + assertEquals(RIGHT_EDGE, RECTANGLE.getRightEdge()); + } + + @Test + public void checkBottomEdge() throws Exception { + assertEquals(BOTTOM_EDGE, RECTANGLE.getBottomEdge()); + } + + private static DoubleSegment edge(DoubleVector start, DoubleVector end) { + return new DoubleSegment(start, end); + } +} \ No newline at end of file From 480be1b53d4003dabb03908dc7bd54d56d42cd72 Mon Sep 17 00:00:00 2001 From: Ivan Kupriyanov Date: Wed, 4 Oct 2017 20:00:57 +0300 Subject: [PATCH 2/5] Rename test methods and remove autogenerated throws --- .../jetpad/geometry/DoubleRectangleEdgesTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/util/geometry/src/test/java/jetbrains/jetpad/geometry/DoubleRectangleEdgesTest.java b/util/geometry/src/test/java/jetbrains/jetpad/geometry/DoubleRectangleEdgesTest.java index 9eb9d416..94567cb3 100644 --- a/util/geometry/src/test/java/jetbrains/jetpad/geometry/DoubleRectangleEdgesTest.java +++ b/util/geometry/src/test/java/jetbrains/jetpad/geometry/DoubleRectangleEdgesTest.java @@ -25,22 +25,22 @@ public class DoubleRectangleEdgesTest { private static final DoubleRectangle RECTANGLE = new DoubleRectangle(LEFT_TOP, RIGHT_BOTTOM.subtract(LEFT_TOP)); @Test - public void checkLeftEdge() throws Exception { + public void leftEdge() { assertEquals(LEFT_EDGE, RECTANGLE.getLeftEdge()); } @Test - public void checkTopEdge() throws Exception { + public void topEdge() { assertEquals(TOP_EDGE, RECTANGLE.getTopEdge()); } @Test - public void checkRightEdge() throws Exception { + public void rightEdge() { assertEquals(RIGHT_EDGE, RECTANGLE.getRightEdge()); } @Test - public void checkBottomEdge() throws Exception { + public void bottomEdge() { assertEquals(BOTTOM_EDGE, RECTANGLE.getBottomEdge()); } From 9f84ec0dc241f392b976945aae7a706c5f6858e4 Mon Sep 17 00:00:00 2001 From: Ivan Kupriyanov Date: Thu, 5 Oct 2017 16:21:30 +0300 Subject: [PATCH 3/5] Replace getParts() implementation with new methods for retrieving edges --- .../jetpad/geometry/DoubleRectangle.java | 17 ++++++----- .../geometry/DoubleRectangleEdgesTest.java | 28 +++++++++++++++---- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/util/geometry/src/main/java/jetbrains/jetpad/geometry/DoubleRectangle.java b/util/geometry/src/main/java/jetbrains/jetpad/geometry/DoubleRectangle.java index e48416bf..f72af649 100644 --- a/util/geometry/src/main/java/jetbrains/jetpad/geometry/DoubleRectangle.java +++ b/util/geometry/src/main/java/jetbrains/jetpad/geometry/DoubleRectangle.java @@ -152,24 +152,24 @@ public DoubleSegment getTopEdge() { public DoubleSegment getRightEdge() { return new DoubleSegment( - new DoubleVector(getRight(), getTop()), - origin.add(dimension) + origin.add(dimension), + new DoubleVector(getRight(), getTop()) ); } public DoubleSegment getBottomEdge() { return new DoubleSegment( - new DoubleVector(getLeft(), getBottom()), - origin.add(dimension) + origin.add(dimension), + new DoubleVector(getLeft(), getBottom()) ); } public Iterable getParts() { List result = new ArrayList<>(); - result.add(new DoubleSegment(origin, origin.add(new DoubleVector(dimension.x, 0)))); - result.add(new DoubleSegment(origin, origin.add(new DoubleVector(0, dimension.y)))); - result.add(new DoubleSegment(origin.add(dimension), origin.add(new DoubleVector(dimension.x, 0)))); - result.add(new DoubleSegment(origin.add(dimension), origin.add(new DoubleVector(0, dimension.y)))); + result.add(getTopEdge()); + result.add(getLeftEdge()); + result.add(getRightEdge()); + result.add(getBottomEdge()); return result; } @@ -191,5 +191,4 @@ public boolean equals(Object o) { public String toString() { return "[rect " + origin + ", " + dimension + "]"; } - } \ No newline at end of file diff --git a/util/geometry/src/test/java/jetbrains/jetpad/geometry/DoubleRectangleEdgesTest.java b/util/geometry/src/test/java/jetbrains/jetpad/geometry/DoubleRectangleEdgesTest.java index 94567cb3..dd9624d2 100644 --- a/util/geometry/src/test/java/jetbrains/jetpad/geometry/DoubleRectangleEdgesTest.java +++ b/util/geometry/src/test/java/jetbrains/jetpad/geometry/DoubleRectangleEdgesTest.java @@ -2,7 +2,10 @@ import org.junit.Test; +import java.util.Iterator; + import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; public class DoubleRectangleEdgesTest { @@ -18,33 +21,46 @@ public class DoubleRectangleEdgesTest { private static final DoubleVector RIGHT_BOTTOM = new DoubleVector(RIGHT, BOTTOM); private static final DoubleSegment LEFT_EDGE = edge(LEFT_TOP, LEFT_BOTTOM); - private static final DoubleSegment BOTTOM_EDGE = edge(LEFT_BOTTOM, RIGHT_BOTTOM); - private static final DoubleSegment RIGHT_EDGE = edge(RIGHT_TOP, RIGHT_BOTTOM); + private static final DoubleSegment BOTTOM_EDGE = edge(RIGHT_BOTTOM, LEFT_BOTTOM); + private static final DoubleSegment RIGHT_EDGE = edge(RIGHT_BOTTOM, RIGHT_TOP); private static final DoubleSegment TOP_EDGE = edge(LEFT_TOP, RIGHT_TOP); private static final DoubleRectangle RECTANGLE = new DoubleRectangle(LEFT_TOP, RIGHT_BOTTOM.subtract(LEFT_TOP)); @Test - public void leftEdge() { + public void getLeftEdge() { assertEquals(LEFT_EDGE, RECTANGLE.getLeftEdge()); } @Test - public void topEdge() { + public void getTopEdge() { assertEquals(TOP_EDGE, RECTANGLE.getTopEdge()); } @Test - public void rightEdge() { + public void getRightEdge() { assertEquals(RIGHT_EDGE, RECTANGLE.getRightEdge()); } @Test - public void bottomEdge() { + public void getBottomEdge() { assertEquals(BOTTOM_EDGE, RECTANGLE.getBottomEdge()); } + @Test + public void getParts() { + Iterator edges = RECTANGLE.getParts().iterator(); + + assertEquals(TOP_EDGE, edges.next()); + assertEquals(LEFT_EDGE, edges.next()); + assertEquals(RIGHT_EDGE, edges.next()); + assertEquals(BOTTOM_EDGE, edges.next()); + + assertFalse(edges.hasNext()); + } + private static DoubleSegment edge(DoubleVector start, DoubleVector end) { return new DoubleSegment(start, end); } + } \ No newline at end of file From d1a67733608c98c994fc7015f779d4e7c7fbd50e Mon Sep 17 00:00:00 2001 From: Ivan Kupriyanov Date: Thu, 5 Oct 2017 17:48:47 +0300 Subject: [PATCH 4/5] Add missing test classes to AllTests --- .../src/test/java/jetbrains/jetpad/geometry/AllTests.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/util/geometry/src/test/java/jetbrains/jetpad/geometry/AllTests.java b/util/geometry/src/test/java/jetbrains/jetpad/geometry/AllTests.java index 18d00316..9c1269f5 100644 --- a/util/geometry/src/test/java/jetbrains/jetpad/geometry/AllTests.java +++ b/util/geometry/src/test/java/jetbrains/jetpad/geometry/AllTests.java @@ -21,7 +21,9 @@ @RunWith(Suite.class) @Suite.SuiteClasses({ DistanceTest.class, + DoubleRectangleEdgesTest.class, DoubleRectangleIntersectionTest.class, + DoubleRectangleTest.class, DoubleSegmentIntersectionTest.class, DoubleVectorOperationsTest.class, RectanglesTest.class, From 6b1d9baea87c6b266b535d192a4603313e4edf85 Mon Sep 17 00:00:00 2001 From: Ivan Kupriyanov Date: Wed, 11 Oct 2017 16:48:46 +0300 Subject: [PATCH 5/5] Use list --- .../jetpad/geometry/DoubleRectangle.java | 12 ++++-------- .../geometry/DoubleRectangleEdgesTest.java | 16 +++++----------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/util/geometry/src/main/java/jetbrains/jetpad/geometry/DoubleRectangle.java b/util/geometry/src/main/java/jetbrains/jetpad/geometry/DoubleRectangle.java index f72af649..81ab101c 100644 --- a/util/geometry/src/main/java/jetbrains/jetpad/geometry/DoubleRectangle.java +++ b/util/geometry/src/main/java/jetbrains/jetpad/geometry/DoubleRectangle.java @@ -17,9 +17,10 @@ import com.google.common.collect.Range; -import java.util.ArrayList; import java.util.List; +import static java.util.Arrays.asList; + public class DoubleRectangle { public static DoubleRectangle span(DoubleVector leftTop, DoubleVector rightBottom) { return new DoubleRectangle(leftTop, rightBottom.subtract(leftTop)); @@ -164,13 +165,8 @@ public DoubleSegment getBottomEdge() { ); } - public Iterable getParts() { - List result = new ArrayList<>(); - result.add(getTopEdge()); - result.add(getLeftEdge()); - result.add(getRightEdge()); - result.add(getBottomEdge()); - return result; + public List getParts() { + return asList(getTopEdge(), getLeftEdge(), getRightEdge(), getBottomEdge()); } @Override diff --git a/util/geometry/src/test/java/jetbrains/jetpad/geometry/DoubleRectangleEdgesTest.java b/util/geometry/src/test/java/jetbrains/jetpad/geometry/DoubleRectangleEdgesTest.java index dd9624d2..9c3bb4a9 100644 --- a/util/geometry/src/test/java/jetbrains/jetpad/geometry/DoubleRectangleEdgesTest.java +++ b/util/geometry/src/test/java/jetbrains/jetpad/geometry/DoubleRectangleEdgesTest.java @@ -2,10 +2,8 @@ import org.junit.Test; -import java.util.Iterator; - +import static java.util.Arrays.asList; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; public class DoubleRectangleEdgesTest { @@ -49,14 +47,10 @@ public void getBottomEdge() { @Test public void getParts() { - Iterator edges = RECTANGLE.getParts().iterator(); - - assertEquals(TOP_EDGE, edges.next()); - assertEquals(LEFT_EDGE, edges.next()); - assertEquals(RIGHT_EDGE, edges.next()); - assertEquals(BOTTOM_EDGE, edges.next()); - - assertFalse(edges.hasNext()); + assertEquals( + asList(TOP_EDGE, LEFT_EDGE, RIGHT_EDGE, BOTTOM_EDGE), + RECTANGLE.getParts() + ); } private static DoubleSegment edge(DoubleVector start, DoubleVector end) {