1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package math.geom2d.polygon;
27
28 import java.awt.Graphics2D;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Iterator;
32
33 import math.geom2d.AffineTransform2D;
34 import math.geom2d.Box2D;
35 import math.geom2d.Point2D;
36 import math.geom2d.circulinear.CirculinearBoundarySet2D;
37 import math.geom2d.circulinear.CirculinearCurve2DUtils;
38 import math.geom2d.circulinear.CirculinearDomain2D;
39 import math.geom2d.circulinear.GenericCirculinearDomain2D;
40 import math.geom2d.domain.Domain2D;
41 import math.geom2d.line.LineSegment2D;
42 import math.geom2d.transform.CircleInversion2D;
43
44
45
46
47
48
49
50
51
52 public class HRectangle2D extends java.awt.geom.Rectangle2D.Double implements
53 Polygon2D {
54
55
56
57
58 private static final long serialVersionUID = 1L;
59
60
61
62
63
64
65
66
67 public HRectangle2D(double x0, double y0, double w, double h) {
68 super(x0, y0, w, h);
69 }
70
71
72 public HRectangle2D() {
73 super(0, 0, 0, 0);
74 }
75
76
77 public HRectangle2D(java.awt.geom.Rectangle2D rect) {
78 super(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
79 }
80
81
82 public HRectangle2D(Point2D point, double w, double h) {
83 super(point.getX(), point.getY(), w, h);
84 }
85
86
87
88
89
90 public boolean isBounded() {
91 return true;
92 }
93
94
95
96
97
98 public Collection<Point2D> getVertices() {
99 ArrayList<Point2D> points = new ArrayList<Point2D>(4);
100 points.add(new Point2D(x, y));
101 points.add(new Point2D(x+width, y));
102 points.add(new Point2D(x+width, y+height));
103 points.add(new Point2D(x, y+height));
104 return points;
105 }
106
107
108
109
110
111
112 public Point2D getVertex(int i) {
113 switch (i) {
114 case 0:
115 return new Point2D(x, y);
116 case 1:
117 return new Point2D(x+width, y);
118 case 2:
119 return new Point2D(x+width, y+height);
120 case 3:
121 return new Point2D(x, y+height);
122 default:
123 throw new IndexOutOfBoundsException();
124 }
125 }
126
127
128
129
130
131
132 public int getVertexNumber() {
133 return 4;
134 }
135
136 public Collection<LineSegment2D> getEdges() {
137 ArrayList<LineSegment2D> edges = new ArrayList<LineSegment2D>(4);
138 edges.add(new LineSegment2D(x, y, x+width, y));
139 edges.add(new LineSegment2D(x+width, y, x+width, y+height));
140 edges.add(new LineSegment2D(x+width, y+height, x, y+height));
141 edges.add(new LineSegment2D(x, y+height, x, y));
142 return edges;
143 }
144
145 public int getEdgeNumber() {
146 return 4;
147 }
148
149
150
151
152 public Collection<LinearRing2D> getRings() {
153 ArrayList<LinearRing2D> rings = new ArrayList<LinearRing2D>(1);
154 rings.add(new LinearRing2D(this.getVertices()));
155 return rings;
156 }
157
158
159
160
161
162
163
164
165 public CirculinearDomain2D transform(CircleInversion2D inv) {
166 return new GenericCirculinearDomain2D(
167 this.getBoundary().transform(inv));
168 }
169
170
171
172
173 public CirculinearDomain2D getBuffer(double dist) {
174 return CirculinearCurve2DUtils.computeBuffer(
175 this.getBoundary(), dist);
176 }
177
178
179
180
181 public CirculinearBoundarySet2D<LinearRing2D> getBoundary() {
182 Point2D pts[] = new Point2D[4];
183 pts[0] = new Point2D(x, y);
184 pts[1] = new Point2D(width+x, y);
185 pts[2] = new Point2D(width+x, y+height);
186 pts[3] = new Point2D(x, y+height);
187 return new CirculinearBoundarySet2D<LinearRing2D>(
188 new LinearRing2D(pts));
189 }
190
191 public Polygon2D complement() {
192 Point2D pts[] = new Point2D[4];
193 pts[0] = new Point2D(x, y);
194 pts[1] = new Point2D(x, y+height);
195 pts[2] = new Point2D(width+x, y+height);
196 pts[3] = new Point2D(width+x, y);
197 return new SimplePolygon2D(pts);
198 }
199
200 public double getDistance(java.awt.geom.Point2D p) {
201 return Math.max(getSignedDistance(p.getX(), p.getY()), 0);
202 }
203
204 public double getDistance(double x, double y) {
205 return Math.max(getSignedDistance(x, y), 0);
206 }
207
208
209
210
211
212
213
214 public double getSignedDistance(java.awt.geom.Point2D p) {
215 return getSignedDistance(p.getX(), p.getY());
216 }
217
218
219
220
221
222
223
224 public double getSignedDistance(double x, double y) {
225 double dist = getBoundary().getDistance(x, y);
226 if (contains(x, y))
227 return -dist;
228 else
229 return dist;
230 }
231
232
233
234
235
236
237 public Domain2D clip(Box2D box) {
238 double xmin = Math.max(this.getMinX(), box.getMinX());
239 double xmax = Math.min(this.getMaxX(), box.getMaxX());
240 double ymin = Math.max(this.getMinY(), box.getMinY());
241 double ymax = Math.min(this.getMaxY(), box.getMaxY());
242 if (xmin>xmax||ymin>ymax)
243 return new HRectangle2D(xmin, ymin, 0, 0);
244 else
245 return new HRectangle2D(xmin, xmax, xmax-xmin, ymax-ymin);
246 }
247
248 public void draw(Graphics2D g2) {
249 g2.draw(this.getBoundary().getGeneralPath());
250 }
251
252 public void fill(Graphics2D g2) {
253 g2.fill(this.getBoundary().getGeneralPath());
254 }
255
256 public Box2D getBoundingBox() {
257 return new Box2D(this.getMinX(), this.getMaxX(), this.getMinY(), this
258 .getMaxY());
259 }
260
261
262
263
264
265
266
267
268
269
270
271
272 @Override
273 public boolean equals(Object obj) {
274
275
276 if (!(obj instanceof HRectangle2D))
277 return false;
278 HRectangle2D rect = (HRectangle2D) obj;
279
280
281 boolean ok;
282 for (Point2D point : this.getVertices()) {
283 ok = false;
284
285
286 for (Point2D point2 : rect.getVertices())
287 if (point.equals(point2))
288 ok = true;
289
290
291
292
293 if (!ok)
294 return false;
295 }
296
297
298 return true;
299 }
300
301
302
303
304
305
306
307 public SimplePolygon2D transform(AffineTransform2D trans) {
308 int nPoints = 4;
309 Point2D[] array = new Point2D[nPoints];
310 Point2D[] res = new Point2D[nPoints];
311 Iterator<Point2D> iter = this.getVertices().iterator();
312 for (int i = 0; i<nPoints; i++) {
313 array[i] = iter.next();
314 res[i] = new Point2D();
315 }
316
317 trans.transform(array, res);
318 return new SimplePolygon2D(res);
319 }
320
321 }