From 04fdeaf2e289b6861c69597fa25fa5a8d447ace3 Mon Sep 17 00:00:00 2001 From: Joseph Gardner Date: Fri, 12 May 2017 11:58:31 +1200 Subject: [PATCH] Optimised boundary checking #story[873] --- .../java/seng302/Model/GPSCoordinate.java | 36 ++++++++---------- .../java/seng302/Model/GPSCoordinateTest.java | 37 ++++++------------- 2 files changed, 27 insertions(+), 46 deletions(-) diff --git a/mock/src/main/java/seng302/Model/GPSCoordinate.java b/mock/src/main/java/seng302/Model/GPSCoordinate.java index 9dc03fd6..a99e2b2a 100644 --- a/mock/src/main/java/seng302/Model/GPSCoordinate.java +++ b/mock/src/main/java/seng302/Model/GPSCoordinate.java @@ -79,12 +79,20 @@ public class GPSCoordinate { * @return true if coordinate is in the boundary */ public static boolean isInsideBoundary(GPSCoordinate coordinate, List boundary) { - double maxLatitude = boundary.stream().max(Comparator.comparingDouble(GPSCoordinate::getLatitude)).get().getLatitude(); - double maxLongitude = boundary.stream().max(Comparator.comparingDouble(GPSCoordinate::getLongitude)).get().getLongitude(); - double minLatitude = boundary.stream().min(Comparator.comparingDouble(GPSCoordinate::getLatitude)).get().getLatitude(); - double minLongitude = boundary.stream().min(Comparator.comparingDouble(GPSCoordinate::getLongitude)).get().getLongitude(); - return isInsideBoundary(coordinate, boundary, new GPSCoordinate(minLatitude, minLongitude), - new GPSCoordinate(maxLatitude, maxLongitude)); + int length = boundary.size(); + + boolean inside = false; + + // Check if inside using ray casting algorithm + for (int i = 0, j = length - 1; i < length; j = i++) { + if (intersects(boundary.get(i), boundary.get(j), coordinate)) { + inside = !inside; + } + + + } + return inside; + } /** @@ -102,24 +110,12 @@ public class GPSCoordinate { double maxLon = maxValues.getLongitude(); double coordinateLat = coordinate.getLatitude(); double coordinateLon = coordinate.getLongitude(); - int length = boundary.size(); - - boolean inside = false; - // End computation early if (coordinateLat <= minLat || coordinateLat >= maxLat || coordinateLon <= minLon || coordinateLon >= maxLon) { return false; + } else { + return isInsideBoundary(coordinate, boundary); } - - // Check if inside using ray casting algorithm - for (int i = 0, j = length - 1; i < length; j = i++) { - if (intersects(boundary.get(i), boundary.get(j), coordinate)) { - inside = !inside; - } - - - } - return inside; } /** diff --git a/mock/src/test/java/seng302/Model/GPSCoordinateTest.java b/mock/src/test/java/seng302/Model/GPSCoordinateTest.java index be0dafea..9f789069 100644 --- a/mock/src/test/java/seng302/Model/GPSCoordinateTest.java +++ b/mock/src/test/java/seng302/Model/GPSCoordinateTest.java @@ -56,24 +56,6 @@ public class GPSCoordinateTest { assertFalse(inside); } - /** - * ------- - * | | - * | | - * *------ - */ - @Test - public void edgeSquareTest() { - boundary.add(new GPSCoordinate(0, 0)); - boundary.add(new GPSCoordinate(10, 0)); - boundary.add(new GPSCoordinate(10, 10)); - boundary.add(new GPSCoordinate(0, 10)); - - GPSCoordinate coordinate = new GPSCoordinate(0, 0); - boolean inside = GPSCoordinate.isInsideBoundary(coordinate, boundary); - assertFalse(inside); - } - @Test public void insideShapeWithObtuseAnglesTest() { boundary.add(new GPSCoordinate(0, 0)); @@ -104,18 +86,21 @@ public class GPSCoordinateTest { assertFalse(inside); } + /** + * ------- + * | | + * * | | + * ------- + */ @Test - public void edgeOfShapeWithObtuseAnglesTest() { + public void earlyTerminationTest() { boundary.add(new GPSCoordinate(0, 0)); - boundary.add(new GPSCoordinate(4, 4)); - boundary.add(new GPSCoordinate(7, 2)); - boundary.add(new GPSCoordinate(9, 5)); + boundary.add(new GPSCoordinate(10, 0)); boundary.add(new GPSCoordinate(10, 10)); - boundary.add(new GPSCoordinate(6, 6)); - boundary.add(new GPSCoordinate(2, 10)); + boundary.add(new GPSCoordinate(0, 10)); - GPSCoordinate coordinate = new GPSCoordinate(2, 10); - boolean inside = GPSCoordinate.isInsideBoundary(coordinate, boundary); + GPSCoordinate coordinate = new GPSCoordinate(-2, 8); + boolean inside = GPSCoordinate.isInsideBoundary(coordinate, boundary, new GPSCoordinate(0, 0), new GPSCoordinate(10, 10)); assertFalse(inside); } }