summaryrefslogtreecommitdiffstats
path: root/libjava/java/awt/geom/Rectangle2D.java
blob: eeb19259cb6b9f47c2cf6e8c0b342f08ba14fcfe (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* Copyright (C) 2000, 2001, 2002  Free Software Foundation

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package java.awt.geom;

/**
 * @author Tom Tromey <tromey@cygnus.com>
 * @date April 16, 2000
 */

public abstract class Rectangle2D extends RectangularShape
{
  public static final int OUT_LEFT = 1;
  public static final int OUT_TOP = 2;
  public static final int OUT_RIGHT = 4;
  public static final int OUT_BOTTOM = 8;

  protected Rectangle2D ()
  {
  }

  /** Set the bounding box of this rectangle.
   * @param x X coordinate
   * @param y Y coordinate
   * @param w Width
   * @param h Height
   */
  public abstract void setRect (double x, double y, double w, double h);

  /** Set the bounding box of this rectangle.
   * @param r  Bounding rectangle.
   */
  public void setRect (Rectangle2D r)
  {
    setRect (r.getX (), r.getY (), r.getWidth (), r.getHeight ());
  }

  /** Returns true if line segment intersects interior of this
   * rectangle.
   * @param x1 X coordinate of first end of line segment
   * @param y1 Y coordinate of first end of line segment
   * @param x2 X coordinate of second end of line segment
   * @param y2 Y coordinate of segment end of line segment
   */
  public boolean intersectsLine (double x1, double y1, double x2, double y2)
  {
    int o1 = outcode (x1, y1);
    int o2 = outcode (x2, y2);

    double mx = getX ();
    double my = getY ();
    double mx2 = getWidth ();
    double my2 = getHeight ();
    x1 -= mx;
    x2 -= mx;
    y1 -= my;
    y2 -= my;

    // Here we handle all lines that stay entirely on one side of the
    // rectangle.  We also handle some lines that intersect the
    // rectangle.  All vertical and horizontal lines are handled here.
    int xor = o1 ^ o2;
    int or  = o1 | o2;
    if ((xor & (OUT_BOTTOM | OUT_TOP)) == 0)
      {
	if ((or & (OUT_BOTTOM | OUT_TOP)) != 0)
	  return false;
	return ((o1 & (OUT_LEFT | OUT_RIGHT)) != 0
		|| (o2 & (OUT_LEFT | OUT_RIGHT)) != 0);
      }
    else if ((xor & (OUT_LEFT | OUT_RIGHT)) == 0)
      {
	if ((or & (OUT_LEFT | OUT_RIGHT)) != 0)
	  return false;
	return ((o1 & (OUT_BOTTOM | OUT_TOP)) != 0
		|| (o2 & (OUT_BOTTOM | OUT_TOP)) != 0);
      }

    double dx = x2 - x1;
    double dy = y2 - y1;

    double t1l = - x1 / dx;
    double t1h = (mx2 - x1) / dx;

    if (t1l >= t1h)
      return false;
    double t2l = - y1 / dy;
    double t2h = (my2 - y1) / dy;

    if (t2l >= t2h || t2l >= t1h || t2h < t1l)
      return false;
    return true;
  }

  /** Return true if line segment intersects interior of this
   * rectangle.
   * @param l The line segment
   */
//    public boolean intersectsLine (Line2D l)
//    {
//    }

  public abstract int outcode (double x, double y);

  public int outcode (Point2D p)
  {
    return outcode (p.getX (), p.getY ());
  }

  /** Set bounding frame for this rectangle.
   * @param x X coordinate
   * @param y Y coordinate
   * @param w Width
   * @param h Height
   */
  public void setFrame (double x, double y, double w, double h)
  {
    setRect (x, y, w, h);
  }

  public Rectangle2D getBounds2D ()
  {
    // FIXME.
    return (Rectangle2D) clone ();
  }

  public boolean contains (double x, double y)
  {
    double mx = getX ();
    double mw = getWidth ();
    if (x < mx || x >= mx + mw)
      return false;
    double my = getY ();
    double mh = getHeight ();
    return y >= my && y < my + mh;
  }

  public boolean intersects (double x, double y, double w, double h)
  {
    double mx = getX ();
    double mw = getWidth ();
    if (x < mx || x >= mx + mw || x + w < mx || x + w >= mx + mw)
      return false;
    double my = getY ();
    double mh = getHeight ();
    return y >= my && y < my + mh && y + h >= my && y + h < my + mh;
  }

  public boolean contains (double x, double y, double w, double h)
  {
    return contains (x, y) && contains (x + w, y + h);
  }

  public abstract Rectangle2D createIntersection (Rectangle2D r);

  public static void intersect (Rectangle2D src1, Rectangle2D src2,
				Rectangle2D dest)
  {
    double x1l = src1.getMinX ();
    double x2l = src2.getMinX ();
    double nxl = Math.max (x1l, x2l);
    double x1h = src1.getMaxX ();
    double x2h = src2.getMaxX ();
    double nxh = Math.min (x1h, x2h);
    if (nxh < nxl)
      nxh = nxl;
    double y1l = src1.getMinY ();
    double y2l = src2.getMinY ();
    double nyl = Math.max (y1l, y2l);
    double y1h = src1.getMaxY ();
    double y2h = src2.getMaxY ();
    double nyh = Math.min (y1h, y2h);
    if (nyh < nyl)
      nyh = nyl;
    dest.setFrameFromDiagonal (nxl, nyl, nxh, nyh);
  }

  public abstract Rectangle2D createUnion (Rectangle2D r);

  public static void union (Rectangle2D src1, Rectangle2D src2,
			    Rectangle2D dest)
  {
    double x1l = src1.getMinX ();
    double x2l = src2.getMinX ();
    double nxl = Math.max (x1l, x2l);
    double x1h = src1.getMaxX ();
    double x2h = src2.getMaxX ();
    double nxh = Math.min (x1h, x2h);
    double y1l = src1.getMinY ();
    double y2l = src2.getMinY ();
    double nyl = Math.max (y1l, y2l);
    double y1h = src1.getMaxY ();
    double y2h = src2.getMaxY ();
    double nyh = Math.min (y1h, y2h);
    dest.setFrameFromDiagonal (nxl, nyl, nxh, nyh);
  }

  public void add (double newx, double newy)
  {
    double minx = Math.min (getMinX (), newx);
    double maxx = Math.max (getMaxX (), newx);
    double miny = Math.min (getMinY (), newy);
    double maxy = Math.max (getMaxY (), newy);
    setFrameFromDiagonal (minx, miny, maxx, maxy);
  }

  public void add (Point2D p)
  {
    add (p.getX (), p.getY ());
  }

  public void add (Rectangle2D r)
  {
    add (r.getMinX (), r.getMinY ());
    add (r.getMaxX (), r.getMaxY ());
  }

  public PathIterator getPathIterator (AffineTransform at)
  {
    // We know the superclass just ignores the flatness parameter.
    return getPathIterator (at, 0);
  }

  public static class Double extends Rectangle2D
  {
    public double height;
    public double width;
    public double x;
    public double y;

    public Double ()
    {
      height = width = x = y = 0;
    }

    public Double (double x, double y, double w, double h)
    {
      this.x = x;
      this.y = y;
      this.width = w;
      this.height = h;
    }

    public double getX ()
    {
      return x;
    }

    public double getY ()
    {
      return y;
    }

    public double getWidth ()
    {
      return width;
    }

    public double getHeight ()
    {
      return height;
    }

    public boolean isEmpty ()
    {
      return width <= 0 || height <= 0;
    }

    public void setRect (double x, double y, double w, double h)
    {
      this.x = x;
      this.y = y;
      this.width = w;
      this.height = h;
    }

    public void setRect (Rectangle2D r)
    {
      this.x = r.getX ();
      this.y = r.getY ();
      this.width = r.getWidth ();
      this.height = r.getHeight ();
    }

    public int outcode (double x, double y)
    {
      int code = 0;
      if (x < this.x)
	code |= OUT_LEFT;
      else if (x >= this.x + this.width)
	code |= OUT_RIGHT;
      if (y < this.y)
	code |= OUT_TOP;
      else if (y >= this.y + this.height)
	code |= OUT_BOTTOM;
      return code;
    }

    public Rectangle2D getBounds2D ()
    {
      return new Rectangle2D.Double (x, y, width, height);
    }

    public Rectangle2D createIntersection (Rectangle2D r)
    {
      Double res = new Double ();
      intersect (this, r, res);
      return res;
    }

    public Rectangle2D createUnion (Rectangle2D r)
    {
      Double res = new Double ();
      union (this, r, res);
      return res;
    }

    public String toString ()
    {
      return "fixme";
    }
  }

  public static class Float extends Rectangle2D
  {
    public float height;
    public float width;
    public float x;
    public float y;

    public Float ()
    {
      height = width = x = y = 0;
    }

    public Float (float x, float y, float w, float h)
    {
      this.x = x;
      this.y = y;
      this.width = w;
      this.height = h;
    }

    public double getX ()
    {
      return x;
    }

    public double getY ()
    {
      return y;
    }

    public double getWidth ()
    {
      return width;
    }

    public double getHeight ()
    {
      return height;
    }

    public boolean isEmpty ()
    {
      return width <= 0 || height <= 0;
    }

    public void setRect (double x, double y, double w, double h)
    {
      this.x = (float) x;
      this.y = (float) y;
      this.width = (float) w;
      this.height = (float) h;
    }

    public void setRect (float x, float y, float w, float h)
    {
      this.x = x;
      this.y = y;
      this.width = w;
      this.height = h;
    }

    public void setRect (Rectangle2D r)
    {
      this.x = (float) r.getX ();
      this.y = (float) r.getY ();
      this.width = (float) r.getWidth ();
      this.height = (float) r.getHeight ();
    }

    public int outcode (double x, double y)
    {
      int code = 0;
      if (x < this.x)
	code |= OUT_LEFT;
      else if (x >= this.x + this.width)
	code |= OUT_RIGHT;
      if (y < this.y)
	code |= OUT_TOP;
      else if (y >= this.y + this.height)
	code |= OUT_BOTTOM;
      return code;
    }

    public Rectangle2D getBounds2D ()
    {
      return new Rectangle2D.Float (x, y, width, height);
    }

    public Rectangle2D createIntersection (Rectangle2D r)
    {
      Float res = new Float ();
      intersect (this, r, res);
      return res;
    }

    public Rectangle2D createUnion (Rectangle2D r)
    {
      Float res = new Float ();
      union (this, r, res);
      return res;
    }

    public String toString ()
    {
      return "fixme";
    }
  }
}
OpenPOWER on IntegriCloud