diff options
author | Samuel Tardieu <sam@rfc1149.net> | 2012-04-14 12:32:05 +0200 |
---|---|---|
committer | Samuel Tardieu <sam@rfc1149.net> | 2012-04-14 12:52:16 +0200 |
commit | 82ee19f07fdc8c43ba6746eb1831309fea6c2f81 (patch) | |
tree | 9b58db752b822002d81f2d2d7bfedb5335dbef57 /main/src | |
parent | 81687dd215a3cbeb4b00da7af74b18fb6a96c403 (diff) | |
download | cgeo-82ee19f07fdc8c43ba6746eb1831309fea6c2f81.zip cgeo-82ee19f07fdc8c43ba6746eb1831309fea6c2f81.tar.gz cgeo-82ee19f07fdc8c43ba6746eb1831309fea6c2f81.tar.bz2 |
Add new Viewport method and builder
Diffstat (limited to 'main/src')
-rw-r--r-- | main/src/cgeo/geocaching/geopoint/Viewport.java | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/geopoint/Viewport.java b/main/src/cgeo/geocaching/geopoint/Viewport.java index 478aa7b..fee91b6 100644 --- a/main/src/cgeo/geocaching/geopoint/Viewport.java +++ b/main/src/cgeo/geocaching/geopoint/Viewport.java @@ -1,5 +1,9 @@ package cgeo.geocaching.geopoint; +import cgeo.geocaching.ICoordinates; + +import java.util.Set; + public class Viewport { @@ -159,6 +163,49 @@ public class Viewport { return new Viewport(getCenter(), getLatitudeSpan() * factor, getLongitudeSpan() * factor); } + /** + * Return a viewport that contains the current viewport as well as another point. + * + * @param coords + * the coordinates we want in the viewport + * @return either the same or an expanded viewport + */ + public Viewport expand(final Geopoint coords) { + if (contains(coords)) { + return this; + } else { + final double latitude = coords.getLatitude(); + final double longitude = coords.getLongitude(); + final double latMin = Math.min(getLatitudeMin(), latitude); + final double latMax = Math.max(getLatitudeMax(), latitude); + final double lonMin = Math.min(getLongitudeMin(), longitude); + final double lonMax = Math.max(getLongitudeMax(), longitude); + return new Viewport(new Geopoint(latMin, lonMin), new Geopoint(latMax, lonMax)); + } + } + + /** + * Return the smallest viewport containing all the given points. + * + * @param points + * a set of points. Point with null coordinates (or null themselves) will be ignored + * @return the smallest viewport containing the non-null coordinates, or null if no coordinates are non-null + */ + static public Viewport containing(final Set<? extends ICoordinates> points) { + Viewport viewport = null; + for (final ICoordinates point : points) { + final Geopoint coords = point == null ? null : point.getCoords(); + if (coords != null) { + if (viewport == null) { + viewport = new Viewport(coords, coords); + } else { + viewport = viewport.expand(coords); + } + } + } + return viewport; + } + @Override public boolean equals(final Object other) { if (other == null || !(other instanceof Viewport)) { |