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;
46 * A two-dimensional curve that is parameterized with a quadratic
49 * <p><img src="doc-files/QuadCurve2D-1.png" width="350" height="180"
50 * alt="A drawing of a QuadCurve2D" />
52 * @author Eric Blake (ebb9@email.byu.edu)
53 * @author Graydon Hoare (graydon@redhat.com)
54 * @author Sascha Brawer (brawer@dandelis.ch)
55 * @author Sven de Marothy (sven@physto.se)
59 public abstract class QuadCurve2D implements Shape, Cloneable
61 private static final double BIG_VALUE = java.lang.Double.MAX_VALUE / 10.0;
62 private static final double EPSILON = 1E-10;
65 * Constructs a new QuadCurve2D. Typical users will want to
66 * construct instances of a subclass, such as {@link
67 * QuadCurve2D.Float} or {@link QuadCurve2D.Double}.
69 protected QuadCurve2D()
74 * Returns the <i>x</i> coordinate of the curve’s start
77 public abstract double getX1();
80 * Returns the <i>y</i> coordinate of the curve’s start
83 public abstract double getY1();
86 * Returns the curve’s start point.
88 public abstract Point2D getP1();
91 * Returns the <i>x</i> coordinate of the curve’s control
94 public abstract double getCtrlX();
97 * Returns the <i>y</i> coordinate of the curve’s control
100 public abstract double getCtrlY();
103 * Returns the curve’s control point.
105 public abstract Point2D getCtrlPt();
108 * Returns the <i>x</i> coordinate of the curve’s end
111 public abstract double getX2();
114 * Returns the <i>y</i> coordinate of the curve’s end
117 public abstract double getY2();
120 * Returns the curve’s end point.
122 public abstract Point2D getP2();
125 * Changes the curve geometry, separately specifying each coordinate
128 * @param x1 the <i>x</i> coordinate of the curve’s new start
131 * @param y1 the <i>y</i> coordinate of the curve’s new start
134 * @param cx the <i>x</i> coordinate of the curve’s new
137 * @param cy the <i>y</i> coordinate of the curve’s new
140 * @param x2 the <i>x</i> coordinate of the curve’s new end
143 * @param y2 the <i>y</i> coordinate of the curve’s new end
146 public abstract void setCurve(double x1, double y1, double cx, double cy,
147 double x2, double y2);
150 * Changes the curve geometry, passing coordinate values in an
153 * @param coords an array containing the new coordinate values. The
154 * <i>x</i> coordinate of the new start point is located at
155 * <code>coords[offset]</code>, its <i>y</i> coordinate at
156 * <code>coords[offset + 1]</code>. The <i>x</i> coordinate of the
157 * new control point is located at <code>coords[offset + 2]</code>,
158 * its <i>y</i> coordinate at <code>coords[offset + 3]</code>. The
159 * <i>x</i> coordinate of the new end point is located at
160 * <code>coords[offset + 4]</code>, its <i>y</i> coordinate at
161 * <code>coords[offset + 5]</code>.
163 * @param offset the offset of the first coordinate value in
164 * <code>coords</code>.
166 public void setCurve(double[] coords, int offset)
168 setCurve(coords[offset++], coords[offset++], coords[offset++],
169 coords[offset++], coords[offset++], coords[offset++]);
173 * Changes the curve geometry, specifying coordinate values in
174 * separate Point objects.
176 * <p><img src="doc-files/QuadCurve2D-1.png" width="350" height="180"
177 * alt="A drawing of a QuadCurve2D" />
179 * <p>The curve does not keep any reference to the passed point
180 * objects. Therefore, a later change to <code>p1</code>,
181 * <code>c</code> <code>p2</code> will not affect the curve
184 * @param p1 the new start point.
185 * @param c the new control point.
186 * @param p2 the new end point.
188 public void setCurve(Point2D p1, Point2D c, Point2D p2)
190 setCurve(p1.getX(), p1.getY(), c.getX(), c.getY(), p2.getX(), p2.getY());
194 * Changes the curve geometry, specifying coordinate values in an
195 * array of Point objects.
197 * <p><img src="doc-files/QuadCurve2D-1.png" width="350" height="180"
198 * alt="A drawing of a QuadCurve2D" />
200 * <p>The curve does not keep references to the passed point
201 * objects. Therefore, a later change to the <code>pts</code> array
202 * or any of its elements will not affect the curve geometry.
204 * @param pts an array containing the points. The new start point
205 * is located at <code>pts[offset]</code>, the new control
206 * point at <code>pts[offset + 1]</code>, and the new end point
207 * at <code>pts[offset + 2]</code>.
209 * @param offset the offset of the start point in <code>pts</code>.
211 public void setCurve(Point2D[] pts, int offset)
213 setCurve(pts[offset].getX(), pts[offset].getY(), pts[offset + 1].getX(),
214 pts[offset + 1].getY(), pts[offset + 2].getX(),
215 pts[offset + 2].getY());
219 * Changes the geometry of the curve to that of another curve.
221 * @param c the curve whose coordinates will be copied.
223 public void setCurve(QuadCurve2D c)
225 setCurve(c.getX1(), c.getY1(), c.getCtrlX(), c.getCtrlY(), c.getX2(),
230 * Calculates the squared flatness of a quadratic curve, directly
231 * specifying each coordinate value. The flatness is the distance of
232 * the control point to the line between start and end point.
234 * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180"
235 * alt="A drawing that illustrates the flatness" />
237 * <p>In the above drawing, the straight line connecting start point
238 * P1 and end point P2 is depicted in gray. The result will be the
239 * the square of the distance between C and the gray line, i.e.
240 * the squared length of the red line.
242 * @param x1 the <i>x</i> coordinate of the start point P1.
243 * @param y1 the <i>y</i> coordinate of the start point P1.
244 * @param cx the <i>x</i> coordinate of the control point C.
245 * @param cy the <i>y</i> coordinate of the control point C.
246 * @param x2 the <i>x</i> coordinate of the end point P2.
247 * @param y2 the <i>y</i> coordinate of the end point P2.
249 public static double getFlatnessSq(double x1, double y1, double cx,
250 double cy, double x2, double y2)
252 return Line2D.ptSegDistSq(x1, y1, x2, y2, cx, cy);
256 * Calculates the flatness of a quadratic curve, directly specifying
257 * each coordinate value. The flatness is the distance of the
258 * control point to the line between start and end point.
260 * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180"
261 * alt="A drawing that illustrates the flatness" />
263 * <p>In the above drawing, the straight line connecting start point
264 * P1 and end point P2 is depicted in gray. The result will be the
265 * the distance between C and the gray line, i.e. the length of
268 * @param x1 the <i>x</i> coordinate of the start point P1.
269 * @param y1 the <i>y</i> coordinate of the start point P1.
270 * @param cx the <i>x</i> coordinate of the control point C.
271 * @param cy the <i>y</i> coordinate of the control point C.
272 * @param x2 the <i>x</i> coordinate of the end point P2.
273 * @param y2 the <i>y</i> coordinate of the end point P2.
275 public static double getFlatness(double x1, double y1, double cx, double cy,
276 double x2, double y2)
278 return Line2D.ptSegDist(x1, y1, x2, y2, cx, cy);
282 * Calculates the squared flatness of a quadratic curve, specifying
283 * the coordinate values in an array. The flatness is the distance
284 * of the control point to the line between start and end point.
286 * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180"
287 * alt="A drawing that illustrates the flatness" />
289 * <p>In the above drawing, the straight line connecting start point
290 * P1 and end point P2 is depicted in gray. The result will be the
291 * the square of the distance between C and the gray line, i.e.
292 * the squared length of the red line.
294 * @param coords an array containing the coordinate values. The
295 * <i>x</i> coordinate of the start point P1 is located at
296 * <code>coords[offset]</code>, its <i>y</i> coordinate at
297 * <code>coords[offset + 1]</code>. The <i>x</i> coordinate of the
298 * control point C is located at <code>coords[offset + 2]</code>,
299 * its <i>y</i> coordinate at <code>coords[offset + 3]</code>. The
300 * <i>x</i> coordinate of the end point P2 is located at
301 * <code>coords[offset + 4]</code>, its <i>y</i> coordinate at
302 * <code>coords[offset + 5]</code>.
304 * @param offset the offset of the first coordinate value in
305 * <code>coords</code>.
307 public static double getFlatnessSq(double[] coords, int offset)
309 return Line2D.ptSegDistSq(coords[offset], coords[offset + 1],
310 coords[offset + 4], coords[offset + 5],
311 coords[offset + 2], coords[offset + 3]);
315 * Calculates the flatness of a quadratic curve, specifying the
316 * coordinate values in an array. The flatness is the distance of
317 * the control point to the line between start and end point.
319 * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180"
320 * alt="A drawing that illustrates the flatness" />
322 * <p>In the above drawing, the straight line connecting start point
323 * P1 and end point P2 is depicted in gray. The result will be the
324 * the the distance between C and the gray line, i.e. the length of
327 * @param coords an array containing the coordinate values. The
328 * <i>x</i> coordinate of the start point P1 is located at
329 * <code>coords[offset]</code>, its <i>y</i> coordinate at
330 * <code>coords[offset + 1]</code>. The <i>x</i> coordinate of the
331 * control point C is located at <code>coords[offset + 2]</code>,
332 * its <i>y</i> coordinate at <code>coords[offset + 3]</code>. The
333 * <i>x</i> coordinate of the end point P2 is located at
334 * <code>coords[offset + 4]</code>, its <i>y</i> coordinate at
335 * <code>coords[offset + 5]</code>.
337 * @param offset the offset of the first coordinate value in
338 * <code>coords</code>.
340 public static double getFlatness(double[] coords, int offset)
342 return Line2D.ptSegDist(coords[offset], coords[offset + 1],
343 coords[offset + 4], coords[offset + 5],
344 coords[offset + 2], coords[offset + 3]);
348 * Calculates the squared flatness of this curve. The flatness is
349 * the distance of the control point to the line between start and
352 * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180"
353 * alt="A drawing that illustrates the flatness" />
355 * <p>In the above drawing, the straight line connecting start point
356 * P1 and end point P2 is depicted in gray. The result will be the
357 * the square of the distance between C and the gray line, i.e. the
358 * squared length of the red line.
360 public double getFlatnessSq()
362 return Line2D.ptSegDistSq(getX1(), getY1(), getX2(), getY2(), getCtrlX(),
367 * Calculates the flatness of this curve. The flatness is the
368 * distance of the control point to the line between start and end
371 * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180"
372 * alt="A drawing that illustrates the flatness" />
374 * <p>In the above drawing, the straight line connecting start point
375 * P1 and end point P2 is depicted in gray. The result will be the
376 * the distance between C and the gray line, i.e. the length of the
379 public double getFlatness()
381 return Line2D.ptSegDist(getX1(), getY1(), getX2(), getY2(), getCtrlX(),
386 * Subdivides this curve into two halves.
388 * <p><img src="doc-files/QuadCurve2D-3.png" width="700"
389 * height="180" alt="A drawing that illustrates the effects of
390 * subdividing a QuadCurve2D" />
392 * @param left a curve whose geometry will be set to the left half
393 * of this curve, or <code>null</code> if the caller is not
394 * interested in the left half.
396 * @param right a curve whose geometry will be set to the right half
397 * of this curve, or <code>null</code> if the caller is not
398 * interested in the right half.
400 public void subdivide(QuadCurve2D left, QuadCurve2D right)
402 // Use empty slots at end to share single array.
403 double[] d = new double[]
405 getX1(), getY1(), getCtrlX(), getCtrlY(), getX2(), getY2(),
408 subdivide(d, 0, d, 0, d, 4);
412 right.setCurve(d, 4);
416 * Subdivides a quadratic curve into two halves.
418 * <p><img src="doc-files/QuadCurve2D-3.png" width="700"
419 * height="180" alt="A drawing that illustrates the effects of
420 * subdividing a QuadCurve2D" />
422 * @param src the curve to be subdivided.
424 * @param left a curve whose geometry will be set to the left half
425 * of <code>src</code>, or <code>null</code> if the caller is not
426 * interested in the left half.
428 * @param right a curve whose geometry will be set to the right half
429 * of <code>src</code>, or <code>null</code> if the caller is not
430 * interested in the right half.
432 public static void subdivide(QuadCurve2D src, QuadCurve2D left,
435 src.subdivide(left, right);
439 * Subdivides a quadratic curve into two halves, passing all
440 * coordinates in an array.
442 * <p><img src="doc-files/QuadCurve2D-3.png" width="700"
443 * height="180" alt="A drawing that illustrates the effects of
444 * subdividing a QuadCurve2D" />
446 * <p>The left end point and the right start point will always be
447 * identical. Memory-concious programmers thus may want to pass the
448 * same array for both <code>left</code> and <code>right</code>, and
449 * set <code>rightOff</code> to <code>leftOff + 4</code>.
451 * @param src an array containing the coordinates of the curve to be
452 * subdivided. The <i>x</i> coordinate of the start point is
453 * located at <code>src[srcOff]</code>, its <i>y</i> at
454 * <code>src[srcOff + 1]</code>. The <i>x</i> coordinate of the
455 * control point is located at <code>src[srcOff + 2]</code>, its
456 * <i>y</i> at <code>src[srcOff + 3]</code>. The <i>x</i>
457 * coordinate of the end point is located at <code>src[srcOff +
458 * 4]</code>, its <i>y</i> at <code>src[srcOff + 5]</code>.
460 * @param srcOff an offset into <code>src</code>, specifying
461 * the index of the start point’s <i>x</i> coordinate.
463 * @param left an array that will receive the coordinates of the
464 * left half of <code>src</code>. It is acceptable to pass
465 * <code>src</code>. A caller who is not interested in the left half
466 * can pass <code>null</code>.
468 * @param leftOff an offset into <code>left</code>, specifying the
469 * index where the start point’s <i>x</i> coordinate will be
472 * @param right an array that will receive the coordinates of the
473 * right half of <code>src</code>. It is acceptable to pass
474 * <code>src</code> or <code>left</code>. A caller who is not
475 * interested in the right half can pass <code>null</code>.
477 * @param rightOff an offset into <code>right</code>, specifying the
478 * index where the start point’s <i>x</i> coordinate will be
481 public static void subdivide(double[] src, int srcOff, double[] left,
482 int leftOff, double[] right, int rightOff)
492 y1 = src[srcOff + 1];
493 xc = src[srcOff + 2];
494 yc = src[srcOff + 3];
495 x2 = src[srcOff + 4];
496 y2 = src[srcOff + 5];
501 left[leftOff + 1] = y1;
506 right[rightOff + 4] = x2;
507 right[rightOff + 5] = y2;
519 left[leftOff + 2] = x1;
520 left[leftOff + 3] = y1;
521 left[leftOff + 4] = xc;
522 left[leftOff + 5] = yc;
527 right[rightOff] = xc;
528 right[rightOff + 1] = yc;
529 right[rightOff + 2] = x2;
530 right[rightOff + 3] = y2;
535 * Finds the non-complex roots of a quadratic equation, placing the
536 * results into the same array as the equation coefficients. The
537 * following equation is being solved:
539 * <blockquote><code>eqn[2]</code> · <i>x</i><sup>2</sup>
540 * + <code>eqn[1]</code> · <i>x</i>
541 * + <code>eqn[0]</code>
545 * <p>For some background about solving quadratic equations, see the
547 * "http://planetmath.org/encyclopedia/QuadraticFormula.html"
548 * >“Quadratic Formula”</a> in <a href=
549 * "http://planetmath.org/">PlanetMath</a>. For an extensive library
550 * of numerical algorithms written in the C programming language,
551 * see the <a href="http://www.gnu.org/software/gsl/">GNU Scientific
554 * @see #solveQuadratic(double[], double[])
555 * @see CubicCurve2D#solveCubic(double[], double[])
557 * @param eqn an array with the coefficients of the equation. When
558 * this procedure has returned, <code>eqn</code> will contain the
559 * non-complex solutions of the equation, in no particular order.
561 * @return the number of non-complex solutions. A result of 0
562 * indicates that the equation has no non-complex solutions. A
563 * result of -1 indicates that the equation is constant (i.e.,
564 * always or never zero).
566 * @author <a href="mailto:bjg@network-theory.com">Brian Gough</a>
567 * (original C implementation in the <a href=
568 * "http://www.gnu.org/software/gsl/">GNU Scientific Library</a>)
570 * @author <a href="mailto:brawer@dandelis.ch">Sascha Brawer</a>
571 * (adaptation to Java)
573 public static int solveQuadratic(double[] eqn)
575 return solveQuadratic(eqn, eqn);
579 * Finds the non-complex roots of a quadratic equation. The
580 * following equation is being solved:
582 * <blockquote><code>eqn[2]</code> · <i>x</i><sup>2</sup>
583 * + <code>eqn[1]</code> · <i>x</i>
584 * + <code>eqn[0]</code>
588 * <p>For some background about solving quadratic equations, see the
590 * "http://planetmath.org/encyclopedia/QuadraticFormula.html"
591 * >“Quadratic Formula”</a> in <a href=
592 * "http://planetmath.org/">PlanetMath</a>. For an extensive library
593 * of numerical algorithms written in the C programming language,
594 * see the <a href="http://www.gnu.org/software/gsl/">GNU Scientific
597 * @see CubicCurve2D#solveCubic(double[],double[])
599 * @param eqn an array with the coefficients of the equation.
601 * @param res an array into which the non-complex roots will be
602 * stored. The results may be in an arbitrary order. It is safe to
603 * pass the same array object reference for both <code>eqn</code>
604 * and <code>res</code>.
606 * @return the number of non-complex solutions. A result of 0
607 * indicates that the equation has no non-complex solutions. A
608 * result of -1 indicates that the equation is constant (i.e.,
609 * always or never zero).
611 * @author <a href="mailto:bjg@network-theory.com">Brian Gough</a>
612 * (original C implementation in the <a href=
613 * "http://www.gnu.org/software/gsl/">GNU Scientific Library</a>)
615 * @author <a href="mailto:brawer@dandelis.ch">Sascha Brawer</a>
616 * (adaptation to Java)
618 public static int solveQuadratic(double[] eqn, double[] res)
620 // Taken from poly/solve_quadratic.c in the GNU Scientific Library
621 // (GSL), cvs revision 1.7 of 2003-07-26. For the original source,
622 // see http://www.gnu.org/software/gsl/
624 // Brian Gough, the author of that code, has granted the
625 // permission to use it in GNU Classpath under the GNU Classpath
626 // license, and has assigned the copyright to the Free Software
629 // The Java implementation is very similar to the GSL code, but
630 // not a strict one-to-one copy. For example, GSL would sort the
641 // Check for linear or constant functions. This is not done by the
642 // GNU Scientific Library. Without this special check, we
643 // wouldn't return -1 for constant functions, and 2 instead of 1
644 // for linear functions.
654 disc = b * b - 4 * a * c;
661 // The GNU Scientific Library returns two identical results here.
662 // We just return one.
663 res[0] = -0.5 * b / a;
672 r = Math.abs(0.5 * Math.sqrt(disc) / a);
681 sgnb = (b > 0 ? 1 : -1);
682 temp = -0.5 * (b + sgnb * Math.sqrt(disc));
684 // The GNU Scientific Library sorts the result here. We don't.
692 * Determines whether a point is inside the area bounded
693 * by the curve and the straight line connecting its end points.
695 * <p><img src="doc-files/QuadCurve2D-5.png" width="350" height="180"
696 * alt="A drawing of the area spanned by the curve" />
698 * <p>The above drawing illustrates in which area points are
699 * considered “inside” a QuadCurve2D.
701 public boolean contains(double x, double y)
703 if (! getBounds2D().contains(x, y))
706 return ((getAxisIntersections(x, y, true, BIG_VALUE) & 1) != 0);
710 * Determines whether a point is inside the area bounded
711 * by the curve and the straight line connecting its end points.
713 * <p><img src="doc-files/QuadCurve2D-5.png" width="350" height="180"
714 * alt="A drawing of the area spanned by the curve" />
716 * <p>The above drawing illustrates in which area points are
717 * considered “inside” a QuadCurve2D.
719 public boolean contains(Point2D p)
721 return contains(p.getX(), p.getY());
725 * Determines whether any part of a rectangle is inside the area bounded
726 * by the curve and the straight line connecting its end points.
728 * <p><img src="doc-files/QuadCurve2D-5.png" width="350" height="180"
729 * alt="A drawing of the area spanned by the curve" />
731 * <p>The above drawing illustrates in which area points are
732 * considered “inside” in a CubicCurve2D.
734 public boolean intersects(double x, double y, double w, double h)
736 if (! getBounds2D().contains(x, y, w, h))
739 /* Does any edge intersect? */
740 if (getAxisIntersections(x, y, true, w) != 0 /* top */
741 || getAxisIntersections(x, y + h, true, w) != 0 /* bottom */
742 || getAxisIntersections(x + w, y, false, h) != 0 /* right */
743 || getAxisIntersections(x, y, false, h) != 0) /* left */
746 /* No intersections, is any point inside? */
747 if ((getAxisIntersections(x, y, true, BIG_VALUE) & 1) != 0)
754 * Determines whether any part of a Rectangle2D is inside the area bounded
755 * by the curve and the straight line connecting its end points.
756 * @see #intersects(double, double, double, double)
758 public boolean intersects(Rectangle2D r)
760 return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
764 * Determines whether a rectangle is entirely inside the area bounded
765 * by the curve and the straight line connecting its end points.
767 * <p><img src="doc-files/QuadCurve2D-5.png" width="350" height="180"
768 * alt="A drawing of the area spanned by the curve" />
770 * <p>The above drawing illustrates in which area points are
771 * considered “inside” a QuadCurve2D.
772 * @see #contains(double, double)
774 public boolean contains(double x, double y, double w, double h)
776 if (! getBounds2D().intersects(x, y, w, h))
779 /* Does any edge intersect? */
780 if (getAxisIntersections(x, y, true, w) != 0 /* top */
781 || getAxisIntersections(x, y + h, true, w) != 0 /* bottom */
782 || getAxisIntersections(x + w, y, false, h) != 0 /* right */
783 || getAxisIntersections(x, y, false, h) != 0) /* left */
786 /* No intersections, is any point inside? */
787 if ((getAxisIntersections(x, y, true, BIG_VALUE) & 1) != 0)
794 * Determines whether a Rectangle2D is entirely inside the area that is
795 * bounded by the curve and the straight line connecting its end points.
796 * @see #contains(double, double, double, double)
798 public boolean contains(Rectangle2D r)
800 return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
804 * Determines the smallest rectangle that encloses the
805 * curve’s start, end and control point. As the illustration
806 * below shows, the invisible control point may cause the bounds to
807 * be much larger than the area that is actually covered by the
810 * <p><img src="doc-files/QuadCurve2D-2.png" width="350" height="180"
811 * alt="An illustration of the bounds of a QuadCurve2D" />
813 public Rectangle getBounds()
815 return getBounds2D().getBounds();
818 public PathIterator getPathIterator(final AffineTransform at)
820 return new PathIterator()
822 /** Current coordinate. */
823 private int current = 0;
825 public int getWindingRule()
827 return WIND_NON_ZERO;
830 public boolean isDone()
840 public int currentSegment(float[] coords)
846 coords[0] = (float) getX1();
847 coords[1] = (float) getY1();
851 coords[0] = (float) getCtrlX();
852 coords[1] = (float) getCtrlY();
853 coords[2] = (float) getX2();
854 coords[3] = (float) getY2();
858 throw new NoSuchElementException("quad iterator out of bounds");
861 at.transform(coords, 0, coords, 0, 2);
865 public int currentSegment(double[] coords)
876 coords[0] = getCtrlX();
877 coords[1] = getCtrlY();
883 throw new NoSuchElementException("quad iterator out of bounds");
886 at.transform(coords, 0, coords, 0, 2);
892 public PathIterator getPathIterator(AffineTransform at, double flatness)
894 return new FlatteningPathIterator(getPathIterator(at), flatness);
898 * Creates a new curve with the same contents as this one.
902 public Object clone()
906 return super.clone();
908 catch (CloneNotSupportedException e)
910 throw (Error) new InternalError().initCause(e); // Impossible
915 * Helper method used by contains() and intersects() methods
916 * Return the number of curve/line intersections on a given axis
917 * extending from a certain point. useYaxis is true for using the Y axis,
918 * @param x x coordinate of the origin point
919 * @param y y coordinate of the origin point
920 * @param useYaxis axis to follow, if true the positive Y axis is used,
921 * false uses the positive X axis.
923 * This is an implementation of the line-crossings algorithm,
924 * Detailed in an article on Eric Haines' page:
925 * http://www.acm.org/tog/editors/erich/ptinpoly/
927 private int getAxisIntersections(double x, double y, boolean useYaxis,
937 double[] r = new double[3];
961 /* If the axis intersects a start/endpoint, shift it up by some small
962 amount to guarantee the line is 'inside'
963 If this is not done,bad behaviour may result for points on that axis. */
964 if (a0 == 0.0 || a2 == 0.0)
966 double small = getFlatness() * EPSILON;
975 r[1] = 2 * (a1 - a0);
976 r[2] = (a2 - 2 * a1 + a0);
978 nRoots = solveQuadratic(r);
979 for (int i = 0; i < nRoots; i++)
982 if (t >= 0.0 && t <= 1.0)
984 double crossing = t * t * (b2 - 2 * b1 + b0) + 2 * t * (b1 - b0)
986 /* single root is always doubly degenerate in quads */
987 if (crossing > 0 && crossing < distance)
988 nCrossings += (nRoots == 1) ? 2 : 1;
994 if (Line2D.linesIntersect(b0, a0, b2, a2, EPSILON, 0.0, distance, 0.0))
999 if (Line2D.linesIntersect(a0, b0, a2, b2, 0.0, EPSILON, 0.0, distance))
1003 return (nCrossings);
1007 * A two-dimensional curve that is parameterized with a quadratic
1008 * function and stores coordinate values in double-precision
1009 * floating-point format.
1011 * @see QuadCurve2D.Float
1013 * @author Eric Blake (ebb9@email.byu.edu)
1014 * @author Sascha Brawer (brawer@dandelis.ch)
1016 public static class Double extends QuadCurve2D
1019 * The <i>x</i> coordinate of the curve’s start point.
1024 * The <i>y</i> coordinate of the curve’s start point.
1029 * The <i>x</i> coordinate of the curve’s control point.
1031 public double ctrlx;
1034 * The <i>y</i> coordinate of the curve’s control point.
1036 public double ctrly;
1039 * The <i>x</i> coordinate of the curve’s end point.
1044 * The <i>y</i> coordinate of the curve’s end point.
1049 * Constructs a new QuadCurve2D that stores its coordinate values
1050 * in double-precision floating-point format. All points are
1051 * initially at position (0, 0).
1058 * Constructs a new QuadCurve2D that stores its coordinate values
1059 * in double-precision floating-point format, specifying the
1060 * initial position of each point.
1062 * @param x1 the <i>x</i> coordinate of the curve’s start
1065 * @param y1 the <i>y</i> coordinate of the curve’s start
1068 * @param cx the <i>x</i> coordinate of the curve’s control
1071 * @param cy the <i>y</i> coordinate of the curve’s control
1074 * @param x2 the <i>x</i> coordinate of the curve’s end
1077 * @param y2 the <i>y</i> coordinate of the curve’s end
1080 public Double(double x1, double y1, double cx, double cy, double x2,
1092 * Returns the <i>x</i> coordinate of the curve’s start
1095 public double getX1()
1101 * Returns the <i>y</i> coordinate of the curve’s start
1104 public double getY1()
1110 * Returns the curve’s start point.
1112 public Point2D getP1()
1114 return new Point2D.Double(x1, y1);
1118 * Returns the <i>x</i> coordinate of the curve’s control
1121 public double getCtrlX()
1127 * Returns the <i>y</i> coordinate of the curve’s control
1130 public double getCtrlY()
1136 * Returns the curve’s control point.
1138 public Point2D getCtrlPt()
1140 return new Point2D.Double(ctrlx, ctrly);
1144 * Returns the <i>x</i> coordinate of the curve’s end
1147 public double getX2()
1153 * Returns the <i>y</i> coordinate of the curve’s end
1156 public double getY2()
1162 * Returns the curve’s end point.
1164 public Point2D getP2()
1166 return new Point2D.Double(x2, y2);
1170 * Changes the geometry of the curve.
1172 * @param x1 the <i>x</i> coordinate of the curve’s new
1175 * @param y1 the <i>y</i> coordinate of the curve’s new
1178 * @param cx the <i>x</i> coordinate of the curve’s new
1181 * @param cy the <i>y</i> coordinate of the curve’s new
1184 * @param x2 the <i>x</i> coordinate of the curve’s new
1187 * @param y2 the <i>y</i> coordinate of the curve’s new
1190 public void setCurve(double x1, double y1, double cx, double cy,
1191 double x2, double y2)
1202 * Determines the smallest rectangle that encloses the
1203 * curve’s start, end and control point. As the
1204 * illustration below shows, the invisible control point may cause
1205 * the bounds to be much larger than the area that is actually
1206 * covered by the curve.
1208 * <p><img src="doc-files/QuadCurve2D-2.png" width="350" height="180"
1209 * alt="An illustration of the bounds of a QuadCurve2D" />
1211 public Rectangle2D getBounds2D()
1213 double nx1 = Math.min(Math.min(x1, ctrlx), x2);
1214 double ny1 = Math.min(Math.min(y1, ctrly), y2);
1215 double nx2 = Math.max(Math.max(x1, ctrlx), x2);
1216 double ny2 = Math.max(Math.max(y1, ctrly), y2);
1217 return new Rectangle2D.Double(nx1, ny1, nx2 - nx1, ny2 - ny1);
1222 * A two-dimensional curve that is parameterized with a quadratic
1223 * function and stores coordinate values in single-precision
1224 * floating-point format.
1226 * @see QuadCurve2D.Double
1228 * @author Eric Blake (ebb9@email.byu.edu)
1229 * @author Sascha Brawer (brawer@dandelis.ch)
1231 public static class Float extends QuadCurve2D
1234 * The <i>x</i> coordinate of the curve’s start point.
1239 * The <i>y</i> coordinate of the curve’s start point.
1244 * The <i>x</i> coordinate of the curve’s control point.
1249 * The <i>y</i> coordinate of the curve’s control point.
1254 * The <i>x</i> coordinate of the curve’s end point.
1259 * The <i>y</i> coordinate of the curve’s end point.
1264 * Constructs a new QuadCurve2D that stores its coordinate values
1265 * in single-precision floating-point format. All points are
1266 * initially at position (0, 0).
1273 * Constructs a new QuadCurve2D that stores its coordinate values
1274 * in single-precision floating-point format, specifying the
1275 * initial position of each point.
1277 * @param x1 the <i>x</i> coordinate of the curve’s start
1280 * @param y1 the <i>y</i> coordinate of the curve’s start
1283 * @param cx the <i>x</i> coordinate of the curve’s control
1286 * @param cy the <i>y</i> coordinate of the curve’s control
1289 * @param x2 the <i>x</i> coordinate of the curve’s end
1292 * @param y2 the <i>y</i> coordinate of the curve’s end
1295 public Float(float x1, float y1, float cx, float cy, float x2, float y2)
1306 * Returns the <i>x</i> coordinate of the curve’s start
1309 public double getX1()
1315 * Returns the <i>y</i> coordinate of the curve’s start
1318 public double getY1()
1324 * Returns the curve’s start point.
1326 public Point2D getP1()
1328 return new Point2D.Float(x1, y1);
1332 * Returns the <i>x</i> coordinate of the curve’s control
1335 public double getCtrlX()
1341 * Returns the <i>y</i> coordinate of the curve’s control
1344 public double getCtrlY()
1350 * Returns the curve’s control point.
1352 public Point2D getCtrlPt()
1354 return new Point2D.Float(ctrlx, ctrly);
1358 * Returns the <i>x</i> coordinate of the curve’s end
1361 public double getX2()
1367 * Returns the <i>y</i> coordinate of the curve’s end
1370 public double getY2()
1376 * Returns the curve’s end point.
1378 public Point2D getP2()
1380 return new Point2D.Float(x2, y2);
1384 * Changes the geometry of the curve, specifying coordinate values
1385 * as double-precision floating-point numbers.
1387 * @param x1 the <i>x</i> coordinate of the curve’s new
1390 * @param y1 the <i>y</i> coordinate of the curve’s new
1393 * @param cx the <i>x</i> coordinate of the curve’s new
1396 * @param cy the <i>y</i> coordinate of the curve’s new
1399 * @param x2 the <i>x</i> coordinate of the curve’s new
1402 * @param y2 the <i>y</i> coordinate of the curve’s new
1405 public void setCurve(double x1, double y1, double cx, double cy,
1406 double x2, double y2)
1408 this.x1 = (float) x1;
1409 this.y1 = (float) y1;
1412 this.x2 = (float) x2;
1413 this.y2 = (float) y2;
1417 * Changes the geometry of the curve, specifying coordinate values
1418 * as single-precision floating-point numbers.
1420 * @param x1 the <i>x</i> coordinate of the curve’s new
1423 * @param y1 the <i>y</i> coordinate of the curve’s new
1426 * @param cx the <i>x</i> coordinate of the curve’s new
1429 * @param cy the <i>y</i> coordinate of the curve’s new
1432 * @param x2 the <i>x</i> coordinate of the curve’s new
1435 * @param y2 the <i>y</i> coordinate of the curve’s new
1438 public void setCurve(float x1, float y1, float cx, float cy, float x2,
1450 * Determines the smallest rectangle that encloses the
1451 * curve’s start, end and control point. As the
1452 * illustration below shows, the invisible control point may cause
1453 * the bounds to be much larger than the area that is actually
1454 * covered by the curve.
1456 * <p><img src="doc-files/QuadCurve2D-2.png" width="350" height="180"
1457 * alt="An illustration of the bounds of a QuadCurve2D" />
1459 public Rectangle2D getBounds2D()
1461 float nx1 = (float) Math.min(Math.min(x1, ctrlx), x2);
1462 float ny1 = (float) Math.min(Math.min(y1, ctrly), y2);
1463 float nx2 = (float) Math.max(Math.max(x1, ctrlx), x2);
1464 float ny2 = (float) Math.max(Math.max(y1, ctrly), y2);
1465 return new Rectangle2D.Float(nx1, ny1, nx2 - nx1, ny2 - ny1);