source: josm/trunk/src/org/openstreetmap/josm/data/osm/BBox.java@ 3115

Last change on this file since 3115 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/plain
File size: 3.9 KB
Line 
1package org.openstreetmap.josm.data.osm;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import org.openstreetmap.josm.data.Bounds;
7import org.openstreetmap.josm.data.coor.LatLon;
8
9public class BBox {
10
11 private double xmin = Double.POSITIVE_INFINITY;
12 private double xmax = Double.NEGATIVE_INFINITY;
13 private double ymin = Double.POSITIVE_INFINITY;
14 private double ymax = Double.NEGATIVE_INFINITY;
15
16 public BBox(Bounds bounds) {
17 add(bounds.getMin());
18 add(bounds.getMax());
19 }
20
21 public BBox(LatLon a, LatLon b) {
22 add(a);
23 add(b);
24 }
25
26 public BBox(double a_x, double a_y, double b_x, double b_y) {
27 xmin = Math.min(a_x, b_x);
28 xmax = Math.max(a_x, b_x);
29 ymin = Math.min(a_y, b_y);
30 ymax = Math.max(a_y, b_y);
31 sanity();
32 }
33
34 public BBox(Way w) {
35 for (Node n : w.getNodes()) {
36 LatLon coor = n.getCoor();
37 if (coor == null) {
38 continue;
39 }
40 add(coor);
41 }
42 }
43
44 private void sanity() {
45 if (xmin < -180.0) {
46 xmin = -180.0;
47 }
48 if (xmax > 180.0) {
49 xmax = 180.0;
50 }
51 if (ymin < -90.0) {
52 ymin = -90.0;
53 }
54 if (ymax > 90.0) {
55 ymax = 90.0;
56 }
57 }
58
59 public void add(LatLon c) {
60 add(c.lon(), c.lat());
61 }
62
63 public void add(double x, double y) {
64 xmin = Math.min(xmin, x);
65 xmax = Math.max(xmax, x);
66 ymin = Math.min(ymin, y);
67 ymax = Math.max(ymax, y);
68 sanity();
69 }
70
71 public void add(BBox box) {
72 add(box.getTopLeft());
73 add(box.getBottomRight());
74 }
75
76 public void addPrimitive(OsmPrimitive primitive, double extraSpace) {
77 BBox primBbox = primitive.getBBox();
78 add(primBbox.xmin - extraSpace, primBbox.ymin - extraSpace);
79 add(primBbox.xmax + extraSpace, primBbox.ymax + extraSpace);
80 }
81
82 public double height() {
83 return ymax-ymin;
84 }
85
86 public double width() {
87 return xmax-xmin;
88 }
89
90 public boolean bounds(BBox b) {
91 if (!(xmin <= b.xmin) ||
92 !(xmax >= b.xmax) ||
93 !(ymin <= b.ymin) ||
94 !(ymax >= b.ymax))
95 return false;
96 return true;
97 }
98
99 public boolean bounds(LatLon c) {
100 if ((xmin <= c.lon()) &&
101 (xmax >= c.lon()) &&
102 (ymin <= c.lat()) &&
103 (ymax >= c.lat()))
104 return true;
105 return false;
106 }
107
108 public boolean inside(BBox b) {
109 if (xmin > b.xmax)
110 return false;
111 if (xmax < b.xmin)
112 return false;
113 if (ymin > b.ymax)
114 return false;
115 if (ymax < b.ymin)
116 return false;
117 return true;
118 }
119
120 public boolean intersects(BBox b) {
121 return this.inside(b) || b.inside(this);
122 }
123
124 public List<LatLon> points() {
125 LatLon p1 = new LatLon(ymin, xmin);
126 LatLon p2 = new LatLon(ymin, xmax);
127 LatLon p3 = new LatLon(ymax, xmin);
128 LatLon p4 = new LatLon(ymax, xmax);
129 List<LatLon> ret = new ArrayList<LatLon>(4);
130 ret.add(p1);
131 ret.add(p2);
132 ret.add(p3);
133 ret.add(p4);
134 return ret;
135 }
136
137 public LatLon getTopLeft() {
138 return new LatLon(ymax, xmin);
139 }
140
141 public LatLon getBottomRight() {
142 return new LatLon(ymin, xmax);
143 }
144
145 @Override
146 public int hashCode() {
147 return (int)(ymin * xmin);
148 }
149
150 @Override
151 public boolean equals(Object o) {
152 if (o instanceof BBox) {
153 BBox b = (BBox)o;
154 return b.xmax == xmax && b.ymax == ymax && b.xmin == xmin && b.ymin == ymin;
155 } else
156 return false;
157 }
158
159 @Override
160 public String toString() {
161 return "[ x: " + xmin + " -> " + xmax +
162 ", y: " + ymin + " -> " + ymax + " ]";
163 }
164}
Note: See TracBrowser for help on using the repository browser.