1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.vectomatic.common.model.geometry;
19
20 import com.google.gwt.user.client.rpc.IsSerializable;
21
22
23
24
25 public class BoundingBox implements IsSerializable {
26 public static final BoundingBox UNIT_BOX = new BoundingBox(-1f, -1f, 1f, 1f);
27
28 public static final int PT_NW = 0;
29 public static final int PT_SW = 1;
30 public static final int PT_SE = 2;
31 public static final int PT_NE = 3;
32 public static final int PT_N = 4;
33 public static final int PT_W = 5;
34 public static final int PT_S = 6;
35 public static final int PT_E = 7;
36 public static final int PT_C = 8;
37 public static final int PT_R = 9;
38 public float xmin, xmax, ymin, ymax;
39
40 public BoundingBox() {
41 }
42
43 public BoundingBox(BoundingBox bbox) {
44 this.xmin = bbox.xmin;
45 this.ymin = bbox.ymin;
46 this.xmax = bbox.xmax;
47 this.ymax = bbox.ymax;
48 }
49
50 public BoundingBox(float xmin, float ymin, float xmax, float ymax) {
51 this.xmin = xmin;
52 this.ymin = ymin;
53 this.xmax = xmax;
54 this.ymax = ymax;
55 }
56
57 @Override
58 public boolean equals(Object o) {
59 if (o instanceof BoundingBox) {
60 BoundingBox bbox = (BoundingBox)o;
61 return (xmin == bbox.xmin) && (xmax == bbox.xmax) && (ymin == bbox.ymin) && (ymax == bbox.ymax);
62 }
63 return false;
64 }
65
66 @Override
67 public int hashCode() {
68 return (int)(17 * xmin) + (int)(19 * ymin) + (int)(23 * xmax) + (int)(29 * ymax);
69 }
70
71 public BoundingBox union(BoundingBox bbox) {
72 return union(bbox, this);
73 }
74
75 public BoundingBox union(BoundingBox bbox, BoundingBox dest) {
76 dest.xmin = Math.min(xmin, bbox.xmin);
77 dest.ymin = Math.min(ymin, bbox.ymin);
78 dest.xmax = Math.max(xmax, bbox.xmax);
79 dest.ymax = Math.max(ymax, bbox.ymax);
80 return dest;
81 }
82
83 public Point getPoint(int pt, Point p) {
84 switch (pt) {
85 case PT_NW:
86 p.x = xmin;
87 p.y = ymin;
88 break;
89 case PT_SW:
90 p.x = xmin;
91 p.y = ymax;
92 break;
93 case PT_SE:
94 p.x = xmax;
95 p.y = ymax;
96 break;
97 case PT_NE:
98 p.x = xmax;
99 p.y = ymin;
100 break;
101 case PT_N:
102 p.x = (xmin + xmax) * 0.5f;
103 p.y = ymin;
104 break;
105 case PT_W:
106 p.x = xmin;
107 p.y = (ymin + ymax) * 0.5f;
108 break;
109 case PT_S:
110 p.x = (xmin + xmax) * 0.5f;
111 p.y = ymax;
112 break;
113 case PT_E:
114 p.x = xmax;
115 p.y = (ymin + ymax) * 0.5f;
116 break;
117 case PT_C:
118 p.x = (xmin + xmax) * 0.5f;
119 p.y = (ymin + ymax) * 0.5f;
120 break;
121 case PT_R:
122 p.x = xmax + 0.25f * getWidth();
123 p.y = (ymin + ymax) * 0.5f;
124 break;
125 }
126 return p;
127 }
128
129 public float getXCenter() {
130 return (xmin + xmax) * 0.5f;
131 }
132 public float getYCenter() {
133 return (ymin + ymax) * 0.5f;
134 }
135
136 public float getWidth() {
137 return xmax - xmin;
138 }
139 public float getHeight() {
140 return ymax - ymin;
141 }
142
143 public boolean containsPoint(Point p) {
144 return (p.x >= xmin && p.x <= xmax && p.y >= ymin && p.y <= ymax);
145 }
146
147 @Override
148 public String toString() {
149 StringBuffer buffer = new StringBuffer();
150 buffer.append("[(");
151 buffer.append(xmin);
152 buffer.append(" ,");
153 buffer.append(ymin);
154 buffer.append(")(");
155 buffer.append(xmax);
156 buffer.append(" ,");
157 buffer.append(ymax);
158 buffer.append(")]");
159 return buffer.toString();
160 }
161
162 public BoundingBox parseString(String str) {
163 int ix1 = 2;
164 int ix2 = str.indexOf(' ');
165 xmin = Float.parseFloat(str.substring(ix1, ix2));
166 ix1 = ix2 + 2;
167 ix2 = str.indexOf(')', ix1);
168 ymin = Float.parseFloat(str.substring(ix1, ix2));
169 ix1 = ix2 + 2;
170 ix2 = str.indexOf(' ', ix1);
171 xmax = Float.parseFloat(str.substring(ix1, ix2));
172 ix1 = ix2 + 2;
173 ix2 = str.indexOf(')', ix1);
174 ymax = Float.parseFloat(str.substring(ix1, ix2));
175 return this;
176 }
177 }