Permalink
Browse files

BoundingBox: extend by margin factor #660

  • Loading branch information...
1 parent ae47111 commit a3995d2b533f8785dfba9065f18c546200a265bd @devemux86 devemux86 committed Oct 8, 2016
Showing with 25 additions and 1 deletion.
  1. +25 −1 mapsforge-core/src/main/java/org/mapsforge/core/model/BoundingBox.java
@@ -1,7 +1,7 @@
/*
* Copyright 2010, 2011, 2012, 2013 mapsforge.org
* Copyright 2014 Christian Pesch
- * Copyright 2015 devemux86
+ * Copyright 2015-2016 devemux86
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
@@ -212,6 +212,30 @@ public BoundingBox extendDegrees(double verticalExpansion, double horizontalExpa
}
/**
+ * Creates a BoundingBox that is a fixed margin factor larger on all sides (but does not cross date line/poles).
+ *
+ * @param margin extension (must be > 0)
+ * @return an extended BoundingBox or this (if margin == 1)
+ */
+ public BoundingBox extendMargin(float margin) {
+ if (margin == 1) {
+ return this;
+ } else if (margin <= 0) {
+ throw new IllegalArgumentException("BoundingBox extend operation does not accept negative or zero values");
+ }
+
+ double verticalExpansion = (this.getLatitudeSpan() * margin - this.getLatitudeSpan()) * 0.5;
+ double horizontalExpansion = (this.getLongitudeSpan() * margin - this.getLongitudeSpan()) * 0.5;
+
+ double minLat = Math.max(MercatorProjection.LATITUDE_MIN, this.minLatitude - verticalExpansion);
+ double minLon = Math.max(-180, this.minLongitude - horizontalExpansion);
+ double maxLat = Math.min(MercatorProjection.LATITUDE_MAX, this.maxLatitude + verticalExpansion);
+ double maxLon = Math.min(180, this.maxLongitude + horizontalExpansion);
+
+ return new BoundingBox(minLat, minLon, maxLat, maxLon);
+ }
+
+ /**
* Creates a BoundingBox that is a fixed meter amount larger on all sides (but does not cross date line/poles).
*
* @param meters extension (must be >= 0)

0 comments on commit a3995d2

Please sign in to comment.