aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/awt/geom/Line2D.java
blob: 9f530e5d7c0424438c4a2ccc1f1112eea2f39b8e (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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/* 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.

As a special exception, if you link this library with other files to
produce an executable, this library does not by itself cause the
resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why the
executable file might be covered by the GNU General Public License. */

package java.awt.geom;

import java.awt.Rectangle;
import java.awt.Shape;

/**
 * @author Tom Tromey <tromey@cygnus.com>
 * @date April 21, 2001
 */

public abstract class Line2D implements Shape, Cloneable
{
  protected Line2D ()
  {
  }

  public Object clone ()
  {
    try
      {
	return super.clone ();
      }
    catch (CloneNotSupportedException _)
      {
	// Can't happen.
	return null;
      }
  }

  public boolean contains (double x, double y)
  {
    double x1 = getX1 ();
    double t1 = (x - x1) / (getX2 () - x1);
    if (t1 < 0 || t1 > 1)
      return false;
    double y1 = getY1 ();
    double t2 = (y - y1) / (getY2 () - y1);
    // FIXME: use of == here is bogus
    return t2 >= 0 && t2 <= 1 && t1 == t2;
  }

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

  public boolean contains (Point2D p)
  {
    return contains (p.getX (), p.getY ());
  }

  public boolean contains (Rectangle2D r)
  {
    return false;
  }

  public Rectangle getBounds ()
  {
    double x1 = getX1 ();
    double y1 = getY1 ();
    double x2 = getX2 ();
    double y2 = getY2 ();

    double x = Math.min (x1, x2);
    double y = Math.min (y1, y2);
    double w = Math.abs (x1 - x2);
    double h = Math.abs (y1 - y2);

    return new Rectangle ((int) x, (int) y, (int) w, (int) h);
  }

  public abstract Point2D getP1 ();
  public abstract Point2D getP2 ();

  public PathIterator getPathIterator (AffineTransform at)
  {
    return getPathIterator (at, 0);
  }

  public PathIterator getPathIterator (AffineTransform at, double flatness)
  {
    return at.new Iterator (new Iterator ());
  }

  public abstract double getX1 ();
  public abstract double getY1 ();
  public abstract double getX2 ();
  public abstract double getY2 ();

  public boolean intersects (double x, double y, double w, double h)
  {
    double x1 = getX1 ();
    double y1 = getY1 ();
    double x2 = getX2 ();
    double y2 = getY2 ();

    if (x1 >= x && x1 <= x + w && y1 >= y && y1 <= y +h)
      return true;
    if (x2 >= x && x2 <= x + w && y2 >= y && y2 <= y +h)
      return true;

    double x3 = x + w;
    double y3 = y + h;

    return (linesIntersect (x1, y1, x2, y2, x, y, x, y3)
	    || linesIntersect (x1, y1, x2, y2, x, y3, x3, y3)
	    || linesIntersect (x1, y1, x2, y2, x3, y3, x3, y)
	    || linesIntersect (x1, y1, x2, y2, x3, y, x, y));
  }

  public boolean intersects (Rectangle2D r)
  {
    return intersects (r.getX (), r.getY (), r.getWidth (), r.getHeight ());
  }

  public boolean intersectsLine (double x1, double y1, double x2, double y2)
  {
    return linesIntersect (getX1 (), getY1 (), getX2 (), getY2(),
			   x1, y1, x2, y2);
  }

  public boolean intersectsLine (Line2D l)
  {
    return linesIntersect (getX1 (), getY1 (), getX2 (), getY2(),
			   l.getX1 (), l.getY1 (), l.getX2 (), l.getY2 ());
  }

  public static boolean linesIntersect (double x1, double y1,
					double x2, double y2,
					double x3,double y3,
					double x4, double y4)
  {
    double beta = (((y1 - y3) * (x4 - x3) + (x1 - x3) * (y4 - y3))
		   / ((y2 - y1) * (x4 - x3) + (x2 - x1) * (y4 - y3)));
    if (beta < 0.0 || beta > 1.0)
      return false;
    double alpha = (x1 + beta * (x2 - x1) - x3) / (x4 - x3);
    return alpha >= 0.0 && alpha <= 1.0;
  }

  public double ptLineDist (double px, double py)
  {
    return ptLineDist (getX1 (), getY1 (), getX2 (), getY2 (),
		       px, py);
  }

  public static double ptLineDist (double x1, double y1,
				   double x2, double y2,
				   double px, double py)
  {
    return Math.sqrt (ptLineDistSq (x1, y1, x2, y2, px, py));
  }

  public double ptLineDist (Point2D p)
  {
    return ptLineDist (getX1 (), getY1 (), getX2 (), getY2 (),
		       p.getX (), p.getY ());
  }

  public double ptLineDistSq (double px, double py)
  {
    return ptLineDistSq (getX1 (), getY1 (), getX2 (), getY2 (),
			 px, py);
  }

  public static double ptLineDistSq (double x1, double y1,
				     double x2, double y2,
				     double px, double py)
  {
    double pd2 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);

    double x, y;
    if (pd2 == 0)
      {
	// Points are coincident.
	x = x1;
	y = y2;
      }
    else
      {
	double u = ((px - x1) * (x2 - x1) + (py - y1) * (y2 - y1)) / pd2;
	x = x1 + u * (x2 - x1);
	y = y1 + u * (y2 - y1);
      }

    return (x - px) * (x - px) + (y - py) * (y - py);
  }

  public double ptLineDistSq (Point2D p)
  {
    return ptLineDistSq (getX1 (), getY1 (), getX2 (), getY2 (),
			 p.getX (), p.getY ());
  }

  public double ptSegDist (double px, double py)
  {
    return ptSegDist (getX1 (), getY1 (), getX2 (), getY2 (),
		      px, py);
  }

  public static double ptSegDist (double x1, double y1,
				   double x2, double y2,
				   double px, double py)
  {
    return Math.sqrt (ptSegDistSq (x1, y1, x2, y2, px, py));
  }

  public double ptSegDist (Point2D p)
  {
    return ptSegDist (getX1 (), getY1 (), getX2 (), getY2 (),
		      p.getX (), p.getY ());
  }

  public double ptSegDistSq (double px, double py)
  {
    return ptSegDistSq (getX1 (), getY1 (), getX2 (), getY2 (),
			px, py);
  }

  public static double ptSegDistSq (double x1, double y1,
				     double x2, double y2,
				     double px, double py)
  {
    double pd2 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);

    double x, y;
    if (pd2 == 0)
      {
	// Points are coincident.
	x = x1;
	y = y2;
      }
    else
      {
	double u = ((px - x1) * (x2 - x1) + (py - y1) * (y2 - y1)) / pd2;

	if (u < 0)
	  {
	    // "Off the end"
	    x = x1;
	    y = y1;
	  }
	else if (u > 1.0)
	  {
	    x = x2;
	    y = y2;
	  }
	else
	  {
	    x = x1 + u * (x2 - x1);
	    y = y1 + u * (y2 - y1);
	  }
      }

    return (x - px) * (x - px) + (y - py) * (y - py);
  }

  public double ptSegDistSq (Point2D p)
  {
    return ptSegDistSq (getX1 (), getY1 (), getX2 (), getY2 (),
			p.getX (), p.getY ());
  }

  public int relativeCCW (double px, double py)
  {
    return relativeCCW (getX1 (), getY1 (),
			getX2 (), getY2 (),
			px, py);
  }

  public static int relativeCCW (double x1, double y1,
				 double x2, double y2,
				 double px, double py)
  {
    // This is a somewhat silly way to compute this.
    // Please write a better one.
    double a1 = Math.atan2 (y2 - y1, x2 - x1);
    double a2 = Math.atan2 (py - y1, px - x1);

    double a = (a1 - a2) % (2 * Math.PI);
    if (a == 0 || a == Math.PI)
      {
	double u = ((px - x1) * (x2 - x1) + (py - y1) * (y2 - y1));
	if (u < 0.0)
	  return 1;
	else if (u > 1.0)
	  return -1;
	else
	  return 0;
      }

    return (a > 0 && a < Math.PI) ? 1 : -1;
  }

  public int relativeCCW (Point2D p)
  {
    return relativeCCW (getX1 (), getY1 (),
			getX2 (), getY2 (),
			p.getX (), p.getY ());
  }

  public abstract void setLine (double x1, double y1, double x2, double y2);

  public void setLine (Line2D l)
  {
    setLine (l.getX1 (), l.getY1 (), l.getX2 (), l.getY2 ());
  }

  public void setLine (Point2D p1, Point2D p2)
  {
    setLine (p1.getX (), p1.getY (), p2.getX (), p2.getY ());
  }					     

  public static class Float extends Line2D
  {
    float x1, y1, x2, y2;

    public Float ()
    {
      this (0.0F, 0.0F, 0.0F, 0.0F);
    }

    public Float (float x1, float y1, float x2, float y2)
    {
      this.x1 = x1;
      this.y1 = y1;
      this.x2 = x2;
      this.y2 = y2;
    }

    public Float (Point2D p1, Point2D p2)
    {
      this.x1 = (float) p1.getX ();
      this.y1 = (float) p1.getY ();
      this.x2 = (float) p2.getX ();
      this.y2 = (float) p2.getY ();
    }

    public Rectangle2D getBounds2D ()
    {
      float x = Math.min (x1, x2);
      float w = Math.abs (x1 - x2);
      float y = Math.min (y1, y2);
      float h = Math.abs (y1 - y2);
      return new Rectangle2D.Float (x, y, w, h);
    }

    public Point2D getP1 ()
    {
      return new Point2D.Float (x1, y1);
    }

    public Point2D getP2 ()
    {
      return new Point2D.Float (x2, y2);
    }

    public double getX1 ()
    {
      return x1;
    }

    public double getY1 ()
    {
      return y1;
    }

    public double getX2 ()
    {
      return x2;
    }

    public double getY2 ()
    {
      return y2;
    }

    public void setLine (double x1, double y1, double x2, double y2)
    {
      this.x1 = (float) x1;
      this.y1 = (float) y1;
      this.x2 = (float) x2;
      this.y2 = (float) y2;
    }

    public void setLine (float x1, float y1, float x2, float y2)
    {
      this.x1 = x1;
      this.y1 = y1;
      this.x2 = x2;
      this.y2 = y2;
    }
  }

  public static class Double extends Line2D
  {
    double x1, y1, x2, y2;

    public Double ()
    {
      this (0.0, 0.0, 0.0, 0.0);
    }

    public Double (double x1, double y1, double x2, double y2)
    {
      this.x1 = x1;
      this.y1 = y1;
      this.x2 = x2;
      this.y2 = y2;
    }

    public Double (Point2D p1, Point2D p2)
    {
      this.x1 = (double) p1.getX ();
      this.y1 = p1.getY ();
      this.x2 = p2.getX ();
      this.y2 = p2.getY ();
    }

    public Rectangle2D getBounds2D ()
    {
      double x = Math.min (x1, x2);
      double w = Math.abs (x1 - x2);
      double y = Math.min (y1, y2);
      double h = Math.abs (y1 - y2);
      return new Rectangle2D.Double (x, y, w, h);
    }

    public Point2D getP1 ()
    {
      return new Point2D.Double (x1, y1);
    }

    public Point2D getP2 ()
    {
      return new Point2D.Double (x2, y2);
    }

    public double getX1 ()
    {
      return x1;
    }

    public double getY1 ()
    {
      return y1;
    }

    public double getX2 ()
    {
      return x2;
    }

    public double getY2 ()
    {
      return y2;
    }

    public void setLine (double x1, double y1, double x2, double y2)
    {
      this.x1 = x1;
      this.y1 = y1;
      this.x2 = x2;
      this.y2 = y2;
    }
  }

  // This implements the PathIterator for all line objects that don't
  // override getPathIterator.
  private class Iterator implements PathIterator
  {
    // Current coordinate.
    private int coord;

    private static final int START = 0;
    private static final int END_PLUS_ONE = 2;

    public Iterator ()
    {
      coord = START;
    }

    public int currentSegment (double[] coords)
    {
      int r = SEG_MOVETO;
      if (coord == 0)
	{
	  coords[0] = getX1 ();
	  coords[1] = getY1 ();
	}
      else if (coord == 1)
	{
	  coords[0] = getX2 ();
	  coords[1] = getY2 ();
	}
      else
	r = SEG_CLOSE;

      return r;
    }

    public int currentSegment (float[] coords)
    {
      int r = SEG_MOVETO;
      if (coord == 0)
	{
	  coords[0] = (float) getX1 ();
	  coords[1] = (float) getY1 ();
	}
      else if (coord == 1)
	{
	  coords[0] = (float) getX2 ();
	  coords[1] = (float) getY2 ();
	}
      else
	r = SEG_CLOSE;

      return r;
    }

    public int getWindingRule ()
    {
      return WIND_NON_ZERO;
    }

    public boolean isDone ()
    {
      return coord == END_PLUS_ONE;
    }

    public void next ()
    {
      if (coord < END_PLUS_ONE)
	++coord;
    }
  }
}