source: josm/trunk/src/org/openstreetmap/josm/data/Bounds.java@ 729

Last change on this file since 729 was 655, checked in by ramack, 16 years ago

patch by bruce89, closes #812; thanks bruce

  • Property svn:eol-style set to native
File size: 1.8 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data;
3
4import org.openstreetmap.josm.data.coor.LatLon;
5import org.openstreetmap.josm.data.projection.Projection;
6
7/**
8 * This is a simple data class for "rectangular" areas of the world, given in
9 * lat/lon min/max values.
10 *
11 * @author imi
12 */
13public class Bounds {
14 /**
15 * The minimum and maximum coordinates.
16 */
17 public LatLon min, max;
18
19 /**
20 * Construct bounds out of two points
21 */
22 public Bounds(LatLon min, LatLon max) {
23 this.min = min;
24 this.max = max;
25 }
26
27 /**
28 * Construct bounds that span the whole world.
29 */
30 public Bounds() {
31 min = new LatLon(-Projection.MAX_LAT, -Projection.MAX_LON);
32 max = new LatLon(Projection.MAX_LAT, Projection.MAX_LON);
33 }
34
35 @Override public String toString() {
36 return "Bounds["+min.lat()+","+min.lon()+","+max.lat()+","+max.lon()+"]";
37 }
38
39 /**
40 * @return Center of the bounding box.
41 */
42 public LatLon center() {
43 // FIXME: not sure whether this calculation is right; maybe there is some
44 // more complex calculation needed to get a center of a spherical
45 // dimension?
46 return new LatLon((min.lat()+max.lat())/2, (min.lon()+max.lon())/2);
47 }
48
49 /**
50 * Extend the bounds if necessary to include the given point.
51 */
52 public void extend(LatLon ll) {
53 if (ll.lat() < min.lat() || ll.lon() < min.lon())
54 min = new LatLon(Math.min(ll.lat(), min.lat()), Math.min(ll.lon(), min.lon()));
55 if (ll.lat() > max.lat() || ll.lon() > max.lon())
56 max = new LatLon(Math.max(ll.lat(), max.lat()), Math.max(ll.lon(), max.lon()));
57 }
58 /**
59 * Is the given point within this bounds?
60 */
61 public boolean contains(LatLon ll) {
62 if (ll.lat() < min.lat() || ll.lon() < min.lon())
63 return false;
64 if (ll.lat() > max.lat() || ll.lon() > max.lon())
65 return false;
66 return true;
67 }
68}
Note: See TracBrowser for help on using the repository browser.