1 /* QuadCurve2D.java -- represents a parameterized quadratic curve in 2-D space
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package java.awt.geom;
40 import java.awt.Rectangle;
41 import java.awt.Shape;
42 import java.util.NoSuchElementException;
45 * A two-dimensional curve that is parameterized with a quadratic
48 * <p><img src="doc-files/QuadCurve2D-1.png" width="350" height="180"
49 * alt="A drawing of a QuadCurve2D" />
51 * @author Eric Blake (ebb9@email.byu.edu)
52 * @author Graydon Hoare (graydon@redhat.com)
53 * @author Sascha Brawer (brawer@dandelis.ch)
54 * @author Sven de Marothy (sven@physto.se)
58 public abstract class QuadCurve2D implements Shape, Cloneable
60 private static final double BIG_VALUE = java.lang.Double.MAX_VALUE / 10.0;
61 private static final double EPSILON = 1E-10;
64 * Constructs a new QuadCurve2D. Typical users will want to
65 * construct instances of a subclass, such as {@link
66 * QuadCurve2D.Float} or {@link QuadCurve2D.Double}.
68 protected QuadCurve2D()
73 * Returns the <i>x</i> coordinate of the curve’s start
76 public abstract double getX1();
79 * Returns the <i>y</i> coordinate of the curve’s start
82 public abstract double getY1();
85 * Returns the curve’s start point.
87 public abstract Point2D getP1();
90 * Returns the <i>x</i> coordinate of the curve’s control
93 public abstract double getCtrlX();
96 * Returns the <i>y</i> coordinate of the curve’s control
99 public abstract double getCtrlY();
102 * Returns the curve’s control point.
104 public abstract Point2D getCtrlPt();
107 * Returns the <i>x</i> coordinate of the curve’s end
110 public abstract double getX2();
113 * Returns the <i>y</i> coordinate of the curve’s end
116 public abstract double getY2();
119 * Returns the curve’s end point.
121 public abstract Point2D getP2();
124 * Changes the curve geometry, separately specifying each coordinate
127 * @param x1 the <i>x</i> coordinate of the curve’s new start
130 * @param y1 the <i>y</i> coordinate of the curve’s new start
133 * @param cx the <i>x</i> coordinate of the curve’s new
136 * @param cy the <i>y</i> coordinate of the curve’s new
139 * @param x2 the <i>x</i> coordinate of the curve’s new end
142 * @param y2 the <i>y</i> coordinate of the curve’s new end
145 public abstract void setCurve(double x1, double y1, double cx, double cy,
146 double x2, double y2);
149 * Changes the curve geometry, passing coordinate values in an
152 * @param coords an array containing the new coordinate values. The
153 * <i>x</i> coordinate of the new start point is located at
154 * <code>coords[offset]</code>, its <i>y</i> coordinate at
155 * <code>coords[offset + 1]</code>. The <i>x</i> coordinate of the
156 * new control point is located at <code>coords[offset + 2]</code>,
157 * its <i>y</i> coordinate at <code>coords[offset + 3]</code>. The
158 * <i>x</i> coordinate of the new end point is located at
159 * <code>coords[offset + 4]</code>, its <i>y</i> coordinate at
160 * <code>coords[offset + 5]</code>.
162 * @param offset the offset of the first coordinate value in
163 * <code>coords</code>.
165 public void setCurve(double[] coords, int offset)
167 setCurve(coords[offset++], coords[offset++], coords[offset++],
168 coords[offset++], coords[offset++], coords[offset++]);
172 * Changes the curve geometry, specifying coordinate values in
173 * separate Point objects.
175 * <p><img src="doc-files/QuadCurve2D-1.png" width="350" height="180"
176 * alt="A drawing of a QuadCurve2D" />
178 * <p>The curve does not keep any reference to the passed point
179 * objects. Therefore, a later change to <code>p1</code>,
180 * <code>c</code> <code>p2</code> will not affect the curve
183 * @param p1 the new start point.
184 * @param c the new control point.
185 * @param p2 the new end point.
187 public void setCurve(Point2D p1, Point2D c, Point2D p2)
189 setCurve(p1.getX(), p1.getY(), c.getX(), c.getY(), p2.getX(), p2.getY());
193 * Changes the curve geometry, specifying coordinate values in an
194 * array of Point objects.
196 * <p><img src="doc-files/QuadCurve2D-1.png" width="350" height="180"
197 * alt="A drawing of a QuadCurve2D" />
199 * <p>The curve does not keep references to the passed point
200 * objects. Therefore, a later change to the <code>pts</code> array
201 * or any of its elements will not affect the curve geometry.
203 * @param pts an array containing the points. The new start point
204 * is located at <code>pts[offset]</code>, the new control
205 * point at <code>pts[offset + 1]</code>, and the new end point
206 * at <code>pts[offset + 2]</code>.
208 * @param offset the offset of the start point in <code>pts</code>.
210 public void setCurve(Point2D[] pts, int offset)
212 setCurve(pts[offset].getX(), pts[offset].getY(), pts[offset + 1].getX(),
213 pts[offset + 1].getY(), pts[offset + 2].getX(),
214 pts[offset + 2].getY());
218 * Changes the geometry of the curve to that of another curve.
220 * @param c the curve whose coordinates will be copied.
222 public void setCurve(QuadCurve2D c)
224 setCurve(c.getX1(), c.getY1(), c.getCtrlX(), c.getCtrlY(), c.getX2(),
229 * Calculates the squared flatness of a quadratic curve, directly
230 * specifying each coordinate value. The flatness is the distance of
231 * the control point to the line between start and end point.
233 * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180"
234 * alt="A drawing that illustrates the flatness" />
236 * <p>In the above drawing, the straight line connecting start point
237 * P1 and end point P2 is depicted in gray. The result will be the
238 * the square of the distance between C and the gray line, i.e.
239 * the squared length of the red line.
241 * @param x1 the <i>x</i> coordinate of the start point P1.
242 * @param y1 the <i>y</i> coordinate of the start point P1.
243 * @param cx the <i>x</i> coordinate of the control point C.
244 * @param cy the <i>y</i> coordinate of the control point C.
245 * @param x2 the <i>x</i> coordinate of the end point P2.
246 * @param y2 the <i>y</i> coordinate of the end point P2.
248 public static double getFlatnessSq(double x1, double y1, double cx,
249 double cy, double x2, double y2)
251 return Line2D.ptSegDistSq(x1, y1, x2, y2, cx, cy);
255 * Calculates the flatness of a quadratic curve, directly specifying
256 * each coordinate value. The flatness is the distance of the
257 * control point to the line between start and end point.
259 * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180"
260 * alt="A drawing that illustrates the flatness" />
262 * <p>In the above drawing, the straight line connecting start point
263 * P1 and end point P2 is depicted in gray. The result will be the
264 * the distance between C and the gray line, i.e. the length of
267 * @param x1 the <i>x</i> coordinate of the start point P1.
268 * @param y1 the <i>y</i> coordinate of the start point P1.
269 * @param cx the <i>x</i> coordinate of the control point C.
270 * @param cy the <i>y</i> coordinate of the control point C.
271 * @param x2 the <i>x</i> coordinate of the end point P2.
272 * @param y2 the <i>y</i> coordinate of the end point P2.
274 public static double getFlatness(double x1, double y1, double cx, double cy,
275 double x2, double y2)
277 return Line2D.ptSegDist(x1, y1, x2, y2, cx, cy);
281 * Calculates the squared flatness of a quadratic curve, specifying
282 * the coordinate values in an array. The flatness is the distance
283 * of the control point to the line between start and end point.
285 * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180"
286 * alt="A drawing that illustrates the flatness" />
288 * <p>In the above drawing, the straight line connecting start point
289 * P1 and end point P2 is depicted in gray. The result will be the
290 * the square of the distance between C and the gray line, i.e.
291 * the squared length of the red line.
293 * @param coords an array containing the coordinate values. The
294 * <i>x</i> coordinate of the start point P1 is located at
295 * <code>coords[offset]</code>, its <i>y</i> coordinate at
296 * <code>coords[offset + 1]</code>. The <i>x</i> coordinate of the
297 * control point C is located at <code>coords[offset + 2]</code>,
298 * its <i>y</i> coordinate at <code>coords[offset + 3]</code>. The
299 * <i>x</i> coordinate of the end point P2 is located at
300 * <code>coords[offset + 4]</code>, its <i>y</i> coordinate at
301 * <code>coords[offset + 5]</code>.
303 * @param offset the offset of the first coordinate value in
304 * <code>coords</code>.
306 public static double getFlatnessSq(double[] coords, int offset)
308 return Line2D.ptSegDistSq(coords[offset], coords[offset + 1],
309 coords[offset + 4], coords[offset + 5],
310 coords[offset + 2], coords[offset + 3]);
314 * Calculates the flatness of a quadratic curve, specifying the
315 * coordinate values in an array. The flatness is the distance of
316 * the control point to the line between start and end point.
318 * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180"
319 * alt="A drawing that illustrates the flatness" />
321 * <p>In the above drawing, the straight line connecting start point
322 * P1 and end point P2 is depicted in gray. The result will be the
323 * the the distance between C and the gray line, i.e. the length of
326 * @param coords an array containing the coordinate values. The
327 * <i>x</i> coordinate of the start point P1 is located at
328 * <code>coords[offset]</code>, its <i>y</i> coordinate at
329 * <code>coords[offset + 1]</code>. The <i>x</i> coordinate of the
330 * control point C is located at <code>coords[offset + 2]</code>,
331 * its <i>y</i> coordinate at <code>coords[offset + 3]</code>. The
332 * <i>x</i> coordinate of the end point P2 is located at
333 * <code>coords[offset + 4]</code>, its <i>y</i> coordinate at
334 * <code>coords[offset + 5]</code>.
336 * @param offset the offset of the first coordinate value in
337 * <code>coords</code>.
339 public static double getFlatness(double[] coords, int offset)
341 return Line2D.ptSegDist(coords[offset], coords[offset + 1],
342 coords[offset + 4], coords[offset + 5],
343 coords[offset + 2], coords[offset + 3]);
347 * Calculates the squared flatness of this curve. The flatness is
348 * the distance of the control point to the line between start and
351 * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180"
352 * alt="A drawing that illustrates the flatness" />
354 * <p>In the above drawing, the straight line connecting start point
355 * P1 and end point P2 is depicted in gray. The result will be the
356 * the square of the distance between C and the gray line, i.e. the
357 * squared length of the red line.
359 public double getFlatnessSq()
361 return Line2D.ptSegDistSq(getX1(), getY1(), getX2(), getY2(), getCtrlX(),
366 * Calculates the flatness of this curve. The flatness is the
367 * distance of the control point to the line between start and end
370 * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180"
371 * alt="A drawing that illustrates the flatness" />
373 * <p>In the above drawing, the straight line connecting start point
374 * P1 and end point P2 is depicted in gray. The result will be the
375 * the distance between C and the gray line, i.e. the length of the
378 public double getFlatness()
380 return Line2D.ptSegDist(getX1(), getY1(), getX2(), getY2(), getCtrlX(),
385 * Subdivides this curve into two halves.
387 * <p><img src="doc-files/QuadCurve2D-3.png" width="700"
388 * height="180" alt="A drawing that illustrates the effects of
389 * subdividing a QuadCurve2D" />
391 * @param left a curve whose geometry will be set to the left half
392 * of this curve, or <code>null</code> if the caller is not
393 * interested in the left half.
395 * @param right a curve whose geometry will be set to the right half
396 * of this curve, or <code>null</code> if the caller is not
397 * interested in the right half.
399 public void subdivide(QuadCurve2D left, QuadCurve2D right)
401 // Use empty slots at end to share single array.
402 double[] d = new double[]
404 getX1(), getY1(), getCtrlX(), getCtrlY(), getX2(), getY2(),
407 subdivide(d, 0, d, 0, d, 4);
411 right.setCurve(d, 4);
415 * Subdivides a quadratic curve into two halves.
417 * <p><img src="doc-files/QuadCurve2D-3.png" width="700"
418 * height="180" alt="A drawing that illustrates the effects of
419 * subdividing a QuadCurve2D" />
421 * @param src the curve to be subdivided.
423 * @param left a curve whose geometry will be set to the left half
424 * of <code>src</code>, or <code>null</code> if the caller is not
425 * interested in the left half.
427 * @param right a curve whose geometry will be set to the right half
428 * of <code>src</code>, or <code>null</code> if the caller is not
429 * interested in the right half.
431 public static void subdivide(QuadCurve2D src, QuadCurve2D left,
434 src.subdivide(left, right);
438 * Subdivides a quadratic curve into two halves, passing all
439 * coordinates in an array.
441 * <p><img src="doc-files/QuadCurve2D-3.png" width="700"
442 * height="180" alt="A drawing that illustrates the effects of
443 * subdividing a QuadCurve2D" />
445 * <p>The left end point and the right start point will always be
446 * identical. Memory-concious programmers thus may want to pass the
447 * same array for both <code>left</code> and <code>right</code>, and
448 * set <code>rightOff</code> to <code>leftOff + 4</code>.
450 * @param src an array containing the coordinates of the curve to be
451 * subdivided. The <i>x</i> coordinate of the start point is
452 * located at <code>src[srcOff]</code>, its <i>y</i> at
453 * <code>src[srcOff + 1]</code>. The <i>x</i> coordinate of the
454 * control point is located at <code>src[srcOff + 2]</code>, its
455 * <i>y</i> at <code>src[srcOff + 3]</code>. The <i>x</i>
456 * coordinate of the end point is located at <code>src[srcOff +
457 * 4]</code>, its <i>y</i> at <code>src[srcOff + 5]</code>.
459 * @param srcOff an offset into <code>src</code>, specifying
460 * the index of the start point’s <i>x</i> coordinate.
462 * @param left an array that will receive the coordinates of the
463 * left half of <code>src</code>. It is acceptable to pass
464 * <code>src</code>. A caller who is not interested in the left half
465 * can pass <code>null</code>.
467 * @param leftOff an offset into <code>left</code>, specifying the
468 * index where the start point’s <i>x</i> coordinate will be
471 * @param right an array that will receive the coordinates of the
472 * right half of <code>src</code>. It is acceptable to pass
473 * <code>src</code> or <code>left</code>. A caller who is not
474 * interested in the right half can pass <code>null</code>.
476 * @param rightOff an offset into <code>right</code>, specifying the
477 * index where the start point’s <i>x</i> coordinate will be
480 public static void subdivide(double[] src, int srcOff, double[] left,
481 int leftOff, double[] right, int rightOff)
491 y1 = src[srcOff + 1];
492 xc = src[srcOff + 2];
493 yc = src[srcOff + 3];
494 x2 = src[srcOff + 4];
495 y2 = src[srcOff + 5];
500 left[leftOff + 1] = y1;
505 right[rightOff + 4] = x2;
506 right[rightOff + 5] = y2;
518 left[leftOff + 2] = x1;
519 left[leftOff + 3] = y1;
520 left[leftOff + 4] = xc;
521 left[leftOff + 5] = yc;
526 right[rightOff] = xc;
527 right[rightOff + 1] = yc;
528 right[rightOff + 2] = x2;
529 right[rightOff + 3] = y2;
534 * Finds the non-complex roots of a quadratic equation, placing the
535 * results into the same array as the equation coefficients. The
536 * following equation is being solved:
538 * <blockquote><code>eqn[2]</code> · <i>x</i><sup>2</sup>
539 * + <code>eqn[1]</code> · <i>x</i>
540 * + <code>eqn[0]</code>
544 * <p>For some background about solving quadratic equations, see the
546 * "http://planetmath.org/encyclopedia/QuadraticFormula.html"
547 * >“Quadratic Formula”</a> in <a href=
548 * "http://planetmath.org/">PlanetMath</a>. For an extensive library
549 * of numerical algorithms written in the C programming language,
550 * see the <a href="http://www.gnu.org/software/gsl/">GNU Scientific
553 * @see #solveQuadratic(double[], double[])
554 * @see CubicCurve2D#solveCubic(double[], double[])
556 * @param eqn an array with the coefficients of the equation. When
557 * this procedure has returned, <code>eqn</code> will contain the
558 * non-complex solutions of the equation, in no particular order.
560 * @return the number of non-complex solutions. A result of 0
561 * indicates that the equation has no non-complex solutions. A
562 * result of -1 indicates that the equation is constant (i.e.,
563 * always or never zero).
565 * @author Brian Gough (bjg@network-theory.com)
566 * (original C implementation in the <a href=
567 * "http://www.gnu.org/software/gsl/">GNU Scientific Library</a>)
569 * @author Sascha Brawer (brawer@dandelis.ch)
570 * (adaptation to Java)
572 public static int solveQuadratic(double[] eqn)
574 return solveQuadratic(eqn, eqn);
578 * Finds the non-complex roots of a quadratic equation. The
579 * following equation is being solved:
581 * <blockquote><code>eqn[2]</code> · <i>x</i><sup>2</sup>
582 * + <code>eqn[1]</code> · <i>x</i>
583 * + <code>eqn[0]</code>
587 * <p>For some background about solving quadratic equations, see the
589 * "http://planetmath.org/encyclopedia/QuadraticFormula.html"
590 * >“Quadratic Formula”</a> in <a href=
591 * "http://planetmath.org/">PlanetMath</a>. For an extensive library
592 * of numerical algorithms written in the C programming language,
593 * see the <a href="http://www.gnu.org/software/gsl/">GNU Scientific
596 * @see CubicCurve2D#solveCubic(double[],double[])
598 * @param eqn an array with the coefficients of the equation.
600 * @param res an array into which the non-complex roots will be
601 * stored. The results may be in an arbitrary order. It is safe to
602 * pass the same array object reference for both <code>eqn</code>
603 * and <code>res</code>.
605 * @return the number of non-complex solutions. A result of 0
606 * indicates that the equation has no non-complex solutions. A
607 * result of -1 indicates that the equation is constant (i.e.,
608 * always or never zero).
610 * @author Brian Gough (bjg@network-theory.com)
611 * (original C implementation in the <a href=
612 * "http://www.gnu.org/software/gsl/">GNU Scientific Library</a>)
614 * @author Sascha Brawer (brawer@dandelis.ch)
615 * (adaptation to Java)
617 public static int solveQuadratic(double[] eqn, double[] res)
619 // Taken from poly/solve_quadratic.c in the GNU Scientific Library
620 // (GSL), cvs revision 1.7 of 2003-07-26. For the original source,
621 // see http://www.gnu.org/software/gsl/
623 // Brian Gough, the author of that code, has granted the
624 // permission to use it in GNU Classpath under the GNU Classpath
625 // license, and has assigned the copyright to the Free Software
628 // The Java implementation is very similar to the GSL code, but
629 // not a strict one-to-one copy. For example, GSL would sort the
640 // Check for linear or constant functions. This is not done by the
641 // GNU Scientific Library. Without this special check, we
642 // wouldn't return -1 for constant functions, and 2 instead of 1
643 // for linear functions.
653 disc = b * b - 4 * a * c;
660 // The GNU Scientific Library returns two identical results here.
661 // We just return one.
662 res[0] = -0.5 * b / a;
671 r = Math.abs(0.5 * Math.sqrt(disc) / a);
680 sgnb = (b > 0 ? 1 : -1);
681 temp = -0.5 * (b + sgnb * Math.sqrt(disc));
683 // The GNU Scientific Library sorts the result here. We don't.
691 * Determines whether a point is inside the area bounded
692 * by the curve and the straight line connecting its end points.
694 * <p><img src="doc-files/QuadCurve2D-5.png" width="350" height="180"
695 * alt="A drawing of the area spanned by the curve" />
697 * <p>The above drawing illustrates in which area points are
698 * considered “inside” a QuadCurve2D.
700 public boolean contains(double x, double y)
702 if (! getBounds2D().contains(x, y))
705 return ((getAxisIntersections(x, y, true, BIG_VALUE) & 1) != 0);
709 * Determines whether a point is inside the area bounded
710 * by the curve and the straight line connecting its end points.
712 * <p><img src="doc-files/QuadCurve2D-5.png" width="350" height="180"
713 * alt="A drawing of the area spanned by the curve" />
715 * <p>The above drawing illustrates in which area points are
716 * considered “inside” a QuadCurve2D.
718 public boolean contains(Point2D p)
720 return contains(p.getX(), p.getY());
724 * Determines whether any part of a rectangle is inside the area bounded
725 * by the curve and the straight line connecting its end points.
727 * <p><img src="doc-files/QuadCurve2D-5.png" width="350" height="180"
728 * alt="A drawing of the area spanned by the curve" />
730 * <p>The above drawing illustrates in which area points are
731 * considered “inside” in a CubicCurve2D.
733 public boolean intersects(double x, double y, double w, double h)
735 if (! getBounds2D().contains(x, y, w, h))
738 /* Does any edge intersect? */
739 if (getAxisIntersections(x, y, true, w) != 0 /* top */
740 || getAxisIntersections(x, y + h, true, w) != 0 /* bottom */
741 || getAxisIntersections(x + w, y, false, h) != 0 /* right */
742 || getAxisIntersections(x, y, false, h) != 0) /* left */
745 /* No intersections, is any point inside? */
746 if ((getAxisIntersections(x, y, true, BIG_VALUE) & 1) != 0)
753 * Determines whether any part of a Rectangle2D is inside the area bounded
754 * by the curve and the straight line connecting its end points.
755 * @see #intersects(double, double, double, double)
757 public boolean intersects(Rectangle2D r)
759 return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
763 * Determines whether a rectangle is entirely inside the area bounded
764 * by the curve and the straight line connecting its end points.
766 * <p><img src="doc-files/QuadCurve2D-5.png" width="350" height="180"
767 * alt="A drawing of the area spanned by the curve" />
769 * <p>The above drawing illustrates in which area points are
770 * considered “inside” a QuadCurve2D.
771 * @see #contains(double, double)
773 public boolean contains(double x, double y, double w, double h)
775 if (! getBounds2D().intersects(x, y, w, h))
778 /* Does any edge intersect? */
779 if (getAxisIntersections(x, y, true, w) != 0 /* top */
780 || getAxisIntersections(x, y + h, true, w) != 0 /* bottom */
781 || getAxisIntersections(x + w, y, false, h) != 0 /* right */
782 || getAxisIntersections(x, y, false, h) != 0) /* left */
785 /* No intersections, is any point inside? */
786 if ((getAxisIntersections(x, y, true, BIG_VALUE) & 1) != 0)
793 * Determines whether a Rectangle2D is entirely inside the area that is
794 * bounded by the curve and the straight line connecting its end points.
795 * @see #contains(double, double, double, double)
797 public boolean contains(Rectangle2D r)
799 return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
803 * Determines the smallest rectangle that encloses the
804 * curve’s start, end and control point. As the illustration
805 * below shows, the invisible control point may cause the bounds to
806 * be much larger than the area that is actually covered by the
809 * <p><img src="doc-files/QuadCurve2D-2.png" width="350" height="180"
810 * alt="An illustration of the bounds of a QuadCurve2D" />
812 public Rectangle getBounds()
814 return getBounds2D().getBounds();
817 public PathIterator getPathIterator(final AffineTransform at)
819 return new PathIterator()
821 /** Current coordinate. */
822 private int current = 0;
824 public int getWindingRule()
826 return WIND_NON_ZERO;
829 public boolean isDone()
839 public int currentSegment(float[] coords)
845 coords[0] = (float) getX1();
846 coords[1] = (float) getY1();
850 coords[0] = (float) getCtrlX();
851 coords[1] = (float) getCtrlY();
852 coords[2] = (float) getX2();
853 coords[3] = (float) getY2();
857 throw new NoSuchElementException("quad iterator out of bounds");
860 at.transform(coords, 0, coords, 0, 2);
864 public int currentSegment(double[] coords)
875 coords[0] = getCtrlX();
876 coords[1] = getCtrlY();
882 throw new NoSuchElementException("quad iterator out of bounds");
885 at.transform(coords, 0, coords, 0, 2);
891 public PathIterator getPathIterator(AffineTransform at, double flatness)
893 return new FlatteningPathIterator(getPathIterator(at), flatness);
897 * Creates a new curve with the same contents as this one.
901 public Object clone()
905 return super.clone();
907 catch (CloneNotSupportedException e)
909 throw (Error) new InternalError().initCause(e); // Impossible
914 * Helper method used by contains() and intersects() methods
915 * Return the number of curve/line intersections on a given axis
916 * extending from a certain point. useYaxis is true for using the Y axis,
917 * @param x x coordinate of the origin point
918 * @param y y coordinate of the origin point
919 * @param useYaxis axis to follow, if true the positive Y axis is used,
920 * false uses the positive X axis.
922 * This is an implementation of the line-crossings algorithm,
923 * Detailed in an article on Eric Haines' page:
924 * http://www.acm.org/tog/editors/erich/ptinpoly/
926 private int getAxisIntersections(double x, double y, boolean useYaxis,
936 double[] r = new double[3];
960 /* If the axis intersects a start/endpoint, shift it up by some small
961 amount to guarantee the line is 'inside'
962 If this is not done,bad behaviour may result for points on that axis. */
963 if (a0 == 0.0 || a2 == 0.0)
965 double small = getFlatness() * EPSILON;
974 r[1] = 2 * (a1 - a0);
975 r[2] = (a2 - 2 * a1 + a0);
977 nRoots = solveQuadratic(r);
978 for (int i = 0; i < nRoots; i++)
981 if (t >= 0.0 && t <= 1.0)
983 double crossing = t * t * (b2 - 2 * b1 + b0) + 2 * t * (b1 - b0)
985 /* single root is always doubly degenerate in quads */
986 if (crossing > 0 && crossing < distance)
987 nCrossings += (nRoots == 1) ? 2 : 1;
993 if (Line2D.linesIntersect(b0, a0, b2, a2, EPSILON, 0.0, distance, 0.0))
998 if (Line2D.linesIntersect(a0, b0, a2, b2, 0.0, EPSILON, 0.0, distance))
1002 return (nCrossings);
1006 * A two-dimensional curve that is parameterized with a quadratic
1007 * function and stores coordinate values in double-precision
1008 * floating-point format.
1010 * @see QuadCurve2D.Float
1012 * @author Eric Blake (ebb9@email.byu.edu)
1013 * @author Sascha Brawer (brawer@dandelis.ch)
1015 public static class Double extends QuadCurve2D
1018 * The <i>x</i> coordinate of the curve’s start point.
1023 * The <i>y</i> coordinate of the curve’s start point.
1028 * The <i>x</i> coordinate of the curve’s control point.
1030 public double ctrlx;
1033 * The <i>y</i> coordinate of the curve’s control point.
1035 public double ctrly;
1038 * The <i>x</i> coordinate of the curve’s end point.
1043 * The <i>y</i> coordinate of the curve’s end point.
1048 * Constructs a new QuadCurve2D that stores its coordinate values
1049 * in double-precision floating-point format. All points are
1050 * initially at position (0, 0).
1057 * Constructs a new QuadCurve2D that stores its coordinate values
1058 * in double-precision floating-point format, specifying the
1059 * initial position of each point.
1061 * @param x1 the <i>x</i> coordinate of the curve’s start
1064 * @param y1 the <i>y</i> coordinate of the curve’s start
1067 * @param cx the <i>x</i> coordinate of the curve’s control
1070 * @param cy the <i>y</i> coordinate of the curve’s control
1073 * @param x2 the <i>x</i> coordinate of the curve’s end
1076 * @param y2 the <i>y</i> coordinate of the curve’s end
1079 public Double(double x1, double y1, double cx, double cy, double x2,
1091 * Returns the <i>x</i> coordinate of the curve’s start
1094 public double getX1()
1100 * Returns the <i>y</i> coordinate of the curve’s start
1103 public double getY1()
1109 * Returns the curve’s start point.
1111 public Point2D getP1()
1113 return new Point2D.Double(x1, y1);
1117 * Returns the <i>x</i> coordinate of the curve’s control
1120 public double getCtrlX()
1126 * Returns the <i>y</i> coordinate of the curve’s control
1129 public double getCtrlY()
1135 * Returns the curve’s control point.
1137 public Point2D getCtrlPt()
1139 return new Point2D.Double(ctrlx, ctrly);
1143 * Returns the <i>x</i> coordinate of the curve’s end
1146 public double getX2()
1152 * Returns the <i>y</i> coordinate of the curve’s end
1155 public double getY2()
1161 * Returns the curve’s end point.
1163 public Point2D getP2()
1165 return new Point2D.Double(x2, y2);
1169 * Changes the geometry of the curve.
1171 * @param x1 the <i>x</i> coordinate of the curve’s new
1174 * @param y1 the <i>y</i> coordinate of the curve’s new
1177 * @param cx the <i>x</i> coordinate of the curve’s new
1180 * @param cy the <i>y</i> coordinate of the curve’s new
1183 * @param x2 the <i>x</i> coordinate of the curve’s new
1186 * @param y2 the <i>y</i> coordinate of the curve’s new
1189 public void setCurve(double x1, double y1, double cx, double cy,
1190 double x2, double y2)
1201 * Determines the smallest rectangle that encloses the
1202 * curve’s start, end and control point. As the
1203 * illustration below shows, the invisible control point may cause
1204 * the bounds to be much larger than the area that is actually
1205 * covered by the curve.
1207 * <p><img src="doc-files/QuadCurve2D-2.png" width="350" height="180"
1208 * alt="An illustration of the bounds of a QuadCurve2D" />
1210 public Rectangle2D getBounds2D()
1212 double nx1 = Math.min(Math.min(x1, ctrlx), x2);
1213 double ny1 = Math.min(Math.min(y1, ctrly), y2);
1214 double nx2 = Math.max(Math.max(x1, ctrlx), x2);
1215 double ny2 = Math.max(Math.max(y1, ctrly), y2);
1216 return new Rectangle2D.Double(nx1, ny1, nx2 - nx1, ny2 - ny1);
1221 * A two-dimensional curve that is parameterized with a quadratic
1222 * function and stores coordinate values in single-precision
1223 * floating-point format.
1225 * @see QuadCurve2D.Double
1227 * @author Eric Blake (ebb9@email.byu.edu)
1228 * @author Sascha Brawer (brawer@dandelis.ch)
1230 public static class Float extends QuadCurve2D
1233 * The <i>x</i> coordinate of the curve’s start point.
1238 * The <i>y</i> coordinate of the curve’s start point.
1243 * The <i>x</i> coordinate of the curve’s control point.
1248 * The <i>y</i> coordinate of the curve’s control point.
1253 * The <i>x</i> coordinate of the curve’s end point.
1258 * The <i>y</i> coordinate of the curve’s end point.
1263 * Constructs a new QuadCurve2D that stores its coordinate values
1264 * in single-precision floating-point format. All points are
1265 * initially at position (0, 0).
1272 * Constructs a new QuadCurve2D that stores its coordinate values
1273 * in single-precision floating-point format, specifying the
1274 * initial position of each point.
1276 * @param x1 the <i>x</i> coordinate of the curve’s start
1279 * @param y1 the <i>y</i> coordinate of the curve’s start
1282 * @param cx the <i>x</i> coordinate of the curve’s control
1285 * @param cy the <i>y</i> coordinate of the curve’s control
1288 * @param x2 the <i>x</i> coordinate of the curve’s end
1291 * @param y2 the <i>y</i> coordinate of the curve’s end
1294 public Float(float x1, float y1, float cx, float cy, float x2, float y2)
1305 * Returns the <i>x</i> coordinate of the curve’s start
1308 public double getX1()
1314 * Returns the <i>y</i> coordinate of the curve’s start
1317 public double getY1()
1323 * Returns the curve’s start point.
1325 public Point2D getP1()
1327 return new Point2D.Float(x1, y1);
1331 * Returns the <i>x</i> coordinate of the curve’s control
1334 public double getCtrlX()
1340 * Returns the <i>y</i> coordinate of the curve’s control
1343 public double getCtrlY()
1349 * Returns the curve’s control point.
1351 public Point2D getCtrlPt()
1353 return new Point2D.Float(ctrlx, ctrly);
1357 * Returns the <i>x</i> coordinate of the curve’s end
1360 public double getX2()
1366 * Returns the <i>y</i> coordinate of the curve’s end
1369 public double getY2()
1375 * Returns the curve’s end point.
1377 public Point2D getP2()
1379 return new Point2D.Float(x2, y2);
1383 * Changes the geometry of the curve, specifying coordinate values
1384 * as double-precision floating-point numbers.
1386 * @param x1 the <i>x</i> coordinate of the curve’s new
1389 * @param y1 the <i>y</i> coordinate of the curve’s new
1392 * @param cx the <i>x</i> coordinate of the curve’s new
1395 * @param cy the <i>y</i> coordinate of the curve’s new
1398 * @param x2 the <i>x</i> coordinate of the curve’s new
1401 * @param y2 the <i>y</i> coordinate of the curve’s new
1404 public void setCurve(double x1, double y1, double cx, double cy,
1405 double x2, double y2)
1407 this.x1 = (float) x1;
1408 this.y1 = (float) y1;
1411 this.x2 = (float) x2;
1412 this.y2 = (float) y2;
1416 * Changes the geometry of the curve, specifying coordinate values
1417 * as single-precision floating-point numbers.
1419 * @param x1 the <i>x</i> coordinate of the curve’s new
1422 * @param y1 the <i>y</i> coordinate of the curve’s new
1425 * @param cx the <i>x</i> coordinate of the curve’s new
1428 * @param cy the <i>y</i> coordinate of the curve’s new
1431 * @param x2 the <i>x</i> coordinate of the curve’s new
1434 * @param y2 the <i>y</i> coordinate of the curve’s new
1437 public void setCurve(float x1, float y1, float cx, float cy, float x2,
1449 * Determines the smallest rectangle that encloses the
1450 * curve’s start, end and control point. As the
1451 * illustration below shows, the invisible control point may cause
1452 * the bounds to be much larger than the area that is actually
1453 * covered by the curve.
1455 * <p><img src="doc-files/QuadCurve2D-2.png" width="350" height="180"
1456 * alt="An illustration of the bounds of a QuadCurve2D" />
1458 public Rectangle2D getBounds2D()
1460 float nx1 = (float) Math.min(Math.min(x1, ctrlx), x2);
1461 float ny1 = (float) Math.min(Math.min(y1, ctrly), y2);
1462 float nx2 = (float) Math.max(Math.max(x1, ctrlx), x2);
1463 float ny2 = (float) Math.max(Math.max(y1, ctrly), y2);
1464 return new Rectangle2D.Float(nx1, ny1, nx2 - nx1, ny2 - ny1);