diff --git a/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java b/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java index b1be29d5fa..e3b2aa2a69 100644 --- a/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java +++ b/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java @@ -3,7 +3,6 @@ package net.osmand.map; import net.osmand.data.LatLon; import net.osmand.data.QuadRect; import net.osmand.util.Algorithms; -import net.osmand.util.Algorithms.Point2D; import java.io.Serializable; import java.util.LinkedList; @@ -196,19 +195,13 @@ public class WorldRegion implements Serializable { } private boolean containsBoundingBox(QuadRect rectangle) { - if (boundingBox != null && rectangle != null) { - return boundingBox.contains(rectangle); - } - return false; + return (boundingBox != null && rectangle != null) && + boundingBox.contains(rectangle); } private boolean containsPolygon(List another) { - if (polygon != null && another != null) { - Point2D[] inner = Algorithms.createPoint2DArrayFromLatLon(polygon); - Point2D[] outer = Algorithms.createPoint2DArrayFromLatLon(another); - return Algorithms.isFirstPolygonInsideTheSecond(outer, inner); - } - return false; + return (polygon != null && another != null) && + Algorithms.isFirstPolygonInsideSecond(another, polygon); } public boolean isContinent() { diff --git a/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java b/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java index b539ca8363..a8069ce9da 100644 --- a/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java +++ b/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java @@ -119,26 +119,10 @@ public class Algorithms { return def; } - public static class Point2D { - public double x; - public double y; - } - - public static Point2D[] createPoint2DArrayFromLatLon(List latLons) { - Point2D[] array = new Point2D[latLons.size()]; - for (int i = 0; i < array.length; i++) { - Point2D point = new Point2D(); - point.x = latLons.get(i).getLatitude(); - point.y = latLons.get(i).getLongitude(); - array[i] = point; - } - return array; - } - - public static boolean isFirstPolygonInsideTheSecond(Point2D[] first, - Point2D[] second) { - for (Point2D point : first) { - if (!isPointInsideTheBoundary(point, second)) { + public static boolean isFirstPolygonInsideSecond(List firstPolygon, + List secondPolygon) { + for (LatLon point : firstPolygon) { + if (!isPointInsidePolygon(point, secondPolygon)) { // if at least one point is not inside the boundary, return false return false; } @@ -146,13 +130,18 @@ public class Algorithms { return true; } - public static boolean isPointInsideTheBoundary(Point2D point, - Point2D[] polygon) { + public static boolean isPointInsidePolygon(LatLon point, + List polygon) { + double pointX = point.getLongitude(); + double pointY = point.getLatitude(); boolean result = false; - for (int i = 0, j = polygon.length - 1; i < polygon.length; j = i++) { - if ((polygon[i].y > point.y) != (polygon[j].y > point.y) - && (point.x < (polygon[j].x - polygon[i].x) * (point.y - polygon[i].y) / - (polygon[j].y-polygon[i].y) + polygon[i].x)) { + for (int i = 0, j = polygon.size() - 1; i < polygon.size(); j = i++) { + double x1 = polygon.get(i).getLongitude(); + double y1 = polygon.get(i).getLatitude(); + double x2 = polygon.get(j).getLongitude(); + double y2 = polygon.get(j).getLatitude(); + if ((y1 > pointY) != (y2 > pointY) + && (pointX < (x2 - x1) * (pointY - y1) / (y2-y1) + x1)) { result = !result; } }