| GNU Classpath (0.95) | |
| Frames | No Frames |
1: /* QuadCurve2D.java -- represents a parameterized quadratic curve in 2-D space 2: Copyright (C) 2002, 2003, 2004 Free Software Foundation 3: 4: This file is part of GNU Classpath. 5: 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) 9: any later version. 10: 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. 15: 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., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 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 24: combination. 25: 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. */ 37: 38: package java.awt.geom; 39: 40: import java.awt.Rectangle; 41: import java.awt.Shape; 42: import java.util.NoSuchElementException; 43: 44: /** 45: * A two-dimensional curve that is parameterized with a quadratic 46: * function. 47: * 48: * <p><img src="doc-files/QuadCurve2D-1.png" width="350" height="180" 49: * alt="A drawing of a QuadCurve2D" /> 50: * 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) 55: * 56: * @since 1.2 57: */ 58: public abstract class QuadCurve2D implements Shape, Cloneable 59: { 60: private static final double BIG_VALUE = java.lang.Double.MAX_VALUE / 10.0; 61: private static final double EPSILON = 1E-10; 62: 63: /** 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}. 67: */ 68: protected QuadCurve2D() 69: { 70: } 71: 72: /** 73: * Returns the <i>x</i> coordinate of the curve’s start 74: * point. 75: */ 76: public abstract double getX1(); 77: 78: /** 79: * Returns the <i>y</i> coordinate of the curve’s start 80: * point. 81: */ 82: public abstract double getY1(); 83: 84: /** 85: * Returns the curve’s start point. 86: */ 87: public abstract Point2D getP1(); 88: 89: /** 90: * Returns the <i>x</i> coordinate of the curve’s control 91: * point. 92: */ 93: public abstract double getCtrlX(); 94: 95: /** 96: * Returns the <i>y</i> coordinate of the curve’s control 97: * point. 98: */ 99: public abstract double getCtrlY(); 100: 101: /** 102: * Returns the curve’s control point. 103: */ 104: public abstract Point2D getCtrlPt(); 105: 106: /** 107: * Returns the <i>x</i> coordinate of the curve’s end 108: * point. 109: */ 110: public abstract double getX2(); 111: 112: /** 113: * Returns the <i>y</i> coordinate of the curve’s end 114: * point. 115: */ 116: public abstract double getY2(); 117: 118: /** 119: * Returns the curve’s end point. 120: */ 121: public abstract Point2D getP2(); 122: 123: /** 124: * Changes the curve geometry, separately specifying each coordinate 125: * value. 126: * 127: * @param x1 the <i>x</i> coordinate of the curve’s new start 128: * point. 129: * 130: * @param y1 the <i>y</i> coordinate of the curve’s new start 131: * point. 132: * 133: * @param cx the <i>x</i> coordinate of the curve’s new 134: * control point. 135: * 136: * @param cy the <i>y</i> coordinate of the curve’s new 137: * control point. 138: * 139: * @param x2 the <i>x</i> coordinate of the curve’s new end 140: * point. 141: * 142: * @param y2 the <i>y</i> coordinate of the curve’s new end 143: * point. 144: */ 145: public abstract void setCurve(double x1, double y1, double cx, double cy, 146: double x2, double y2); 147: 148: /** 149: * Changes the curve geometry, passing coordinate values in an 150: * array. 151: * 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>. 161: * 162: * @param offset the offset of the first coordinate value in 163: * <code>coords</code>. 164: */ 165: public void setCurve(double[] coords, int offset) 166: { 167: setCurve(coords[offset++], coords[offset++], coords[offset++], 168: coords[offset++], coords[offset++], coords[offset++]); 169: } 170: 171: /** 172: * Changes the curve geometry, specifying coordinate values in 173: * separate Point objects. 174: * 175: * <p><img src="doc-files/QuadCurve2D-1.png" width="350" height="180" 176: * alt="A drawing of a QuadCurve2D" /> 177: * 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 181: * geometry. 182: * 183: * @param p1 the new start point. 184: * @param c the new control point. 185: * @param p2 the new end point. 186: */ 187: public void setCurve(Point2D p1, Point2D c, Point2D p2) 188: { 189: setCurve(p1.getX(), p1.getY(), c.getX(), c.getY(), p2.getX(), p2.getY()); 190: } 191: 192: /** 193: * Changes the curve geometry, specifying coordinate values in an 194: * array of Point objects. 195: * 196: * <p><img src="doc-files/QuadCurve2D-1.png" width="350" height="180" 197: * alt="A drawing of a QuadCurve2D" /> 198: * 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. 202: * 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>. 207: * 208: * @param offset the offset of the start point in <code>pts</code>. 209: */ 210: public void setCurve(Point2D[] pts, int offset) 211: { 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()); 215: } 216: 217: /** 218: * Changes the geometry of the curve to that of another curve. 219: * 220: * @param c the curve whose coordinates will be copied. 221: */ 222: public void setCurve(QuadCurve2D c) 223: { 224: setCurve(c.getX1(), c.getY1(), c.getCtrlX(), c.getCtrlY(), c.getX2(), 225: c.getY2()); 226: } 227: 228: /** 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. 232: * 233: * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180" 234: * alt="A drawing that illustrates the flatness" /> 235: * 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. 240: * 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. 247: */ 248: public static double getFlatnessSq(double x1, double y1, double cx, 249: double cy, double x2, double y2) 250: { 251: return Line2D.ptSegDistSq(x1, y1, x2, y2, cx, cy); 252: } 253: 254: /** 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. 258: * 259: * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180" 260: * alt="A drawing that illustrates the flatness" /> 261: * 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 265: * the red line. 266: * 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. 273: */ 274: public static double getFlatness(double x1, double y1, double cx, double cy, 275: double x2, double y2) 276: { 277: return Line2D.ptSegDist(x1, y1, x2, y2, cx, cy); 278: } 279: 280: /** 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. 284: * 285: * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180" 286: * alt="A drawing that illustrates the flatness" /> 287: * 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. 292: * 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>. 302: * 303: * @param offset the offset of the first coordinate value in 304: * <code>coords</code>. 305: */ 306: public static double getFlatnessSq(double[] coords, int offset) 307: { 308: return Line2D.ptSegDistSq(coords[offset], coords[offset + 1], 309: coords[offset + 4], coords[offset + 5], 310: coords[offset + 2], coords[offset + 3]); 311: } 312: 313: /** 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. 317: * 318: * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180" 319: * alt="A drawing that illustrates the flatness" /> 320: * 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 324: * the red line. 325: * 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>. 335: * 336: * @param offset the offset of the first coordinate value in 337: * <code>coords</code>. 338: */ 339: public static double getFlatness(double[] coords, int offset) 340: { 341: return Line2D.ptSegDist(coords[offset], coords[offset + 1], 342: coords[offset + 4], coords[offset + 5], 343: coords[offset + 2], coords[offset + 3]); 344: } 345: 346: /** 347: * Calculates the squared flatness of this curve. The flatness is 348: * the distance of the control point to the line between start and 349: * end point. 350: * 351: * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180" 352: * alt="A drawing that illustrates the flatness" /> 353: * 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. 358: */ 359: public double getFlatnessSq() 360: { 361: return Line2D.ptSegDistSq(getX1(), getY1(), getX2(), getY2(), getCtrlX(), 362: getCtrlY()); 363: } 364: 365: /** 366: * Calculates the flatness of this curve. The flatness is the 367: * distance of the control point to the line between start and end 368: * point. 369: * 370: * <p><img src="doc-files/QuadCurve2D-4.png" width="350" height="180" 371: * alt="A drawing that illustrates the flatness" /> 372: * 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 376: * red line. 377: */ 378: public double getFlatness() 379: { 380: return Line2D.ptSegDist(getX1(), getY1(), getX2(), getY2(), getCtrlX(), 381: getCtrlY()); 382: } 383: 384: /** 385: * Subdivides this curve into two halves. 386: * 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" /> 390: * 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. 394: * 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. 398: */ 399: public void subdivide(QuadCurve2D left, QuadCurve2D right) 400: { 401: // Use empty slots at end to share single array. 402: double[] d = new double[] 403: { 404: getX1(), getY1(), getCtrlX(), getCtrlY(), getX2(), getY2(), 405: 0, 0, 0, 0 406: }; 407: subdivide(d, 0, d, 0, d, 4); 408: if (left != null) 409: left.setCurve(d, 0); 410: if (right != null) 411: right.setCurve(d, 4); 412: } 413: 414: /** 415: * Subdivides a quadratic curve into two halves. 416: * 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" /> 420: * 421: * @param src the curve to be subdivided. 422: * 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. 426: * 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. 430: */ 431: public static void subdivide(QuadCurve2D src, QuadCurve2D left, 432: QuadCurve2D right) 433: { 434: src.subdivide(left, right); 435: } 436: 437: /** 438: * Subdivides a quadratic curve into two halves, passing all 439: * coordinates in an array. 440: * 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" /> 444: * 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>. 449: * 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>. 458: * 459: * @param srcOff an offset into <code>src</code>, specifying 460: * the index of the start point’s <i>x</i> coordinate. 461: * 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>. 466: * 467: * @param leftOff an offset into <code>left</code>, specifying the 468: * index where the start point’s <i>x</i> coordinate will be 469: * stored. 470: * 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>. 475: * 476: * @param rightOff an offset into <code>right</code>, specifying the 477: * index where the start point’s <i>x</i> coordinate will be 478: * stored. 479: */ 480: public static void subdivide(double[] src, int srcOff, double[] left, 481: int leftOff, double[] right, int rightOff) 482: { 483: double x1; 484: double y1; 485: double xc; 486: double yc; 487: double x2; 488: double y2; 489: 490: x1 = src[srcOff]; 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]; 496: 497: if (left != null) 498: { 499: left[leftOff] = x1; 500: left[leftOff + 1] = y1; 501: } 502: 503: if (right != null) 504: { 505: right[rightOff + 4] = x2; 506: right[rightOff + 5] = y2; 507: } 508: 509: x1 = (x1 + xc) / 2; 510: x2 = (xc + x2) / 2; 511: xc = (x1 + x2) / 2; 512: y1 = (y1 + yc) / 2; 513: y2 = (y2 + yc) / 2; 514: yc = (y1 + y2) / 2; 515: 516: if (left != null) 517: { 518: left[leftOff + 2] = x1; 519: left[leftOff + 3] = y1; 520: left[leftOff + 4] = xc; 521: left[leftOff + 5] = yc; 522: } 523: 524: if (right != null) 525: { 526: right[rightOff] = xc; 527: right[rightOff + 1] = yc; 528: right[rightOff + 2] = x2; 529: right[rightOff + 3] = y2; 530: } 531: } 532: 533: /** 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: 537: * 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> 541: * = 0 542: * </blockquote> 543: * 544: * <p>For some background about solving quadratic equations, see the 545: * article <a href= 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 551: * Library</a>. 552: * 553: * @see #solveQuadratic(double[], double[]) 554: * @see CubicCurve2D#solveCubic(double[], double[]) 555: * 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. 559: * 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). 564: * 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>) 568: * 569: * @author Sascha Brawer (brawer@dandelis.ch) 570: * (adaptation to Java) 571: */ 572: public static int solveQuadratic(double[] eqn) 573: { 574: return solveQuadratic(eqn, eqn); 575: } 576: 577: /** 578: * Finds the non-complex roots of a quadratic equation. The 579: * following equation is being solved: 580: * 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> 584: * = 0 585: * </blockquote> 586: * 587: * <p>For some background about solving quadratic equations, see the 588: * article <a href= 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 594: * Library</a>. 595: * 596: * @see CubicCurve2D#solveCubic(double[],double[]) 597: * 598: * @param eqn an array with the coefficients of the equation. 599: * 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>. 604: * 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). 609: * 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>) 613: * 614: * @author Sascha Brawer (brawer@dandelis.ch) 615: * (adaptation to Java) 616: */ 617: public static int solveQuadratic(double[] eqn, double[] res) 618: { 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/ 622: // 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 626: // Foundation. 627: // 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 630: // result. 631: double a; 632: double b; 633: double c; 634: double disc; 635: 636: c = eqn[0]; 637: b = eqn[1]; 638: a = eqn[2]; 639: 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. 644: if (a == 0) 645: { 646: if (b == 0) 647: return -1; 648: 649: res[0] = -c / b; 650: return 1; 651: } 652: 653: disc = b * b - 4 * a * c; 654: 655: if (disc < 0) 656: return 0; 657: 658: if (disc == 0) 659: { 660: // The GNU Scientific Library returns two identical results here. 661: // We just return one. 662: res[0] = -0.5 * b / a; 663: return 1; 664: } 665: 666: // disc > 0 667: if (b == 0) 668: { 669: double r; 670: 671: r = Math.abs(0.5 * Math.sqrt(disc) / a); 672: res[0] = -r; 673: res[1] = r; 674: } 675: else 676: { 677: double sgnb; 678: double temp; 679: 680: sgnb = (b > 0 ? 1 : -1); 681: temp = -0.5 * (b + sgnb * Math.sqrt(disc)); 682: 683: // The GNU Scientific Library sorts the result here. We don't. 684: res[0] = temp / a; 685: res[1] = c / temp; 686: } 687: return 2; 688: } 689: 690: /** 691: * Determines whether a point is inside the area bounded 692: * by the curve and the straight line connecting its end points. 693: * 694: * <p><img src="doc-files/QuadCurve2D-5.png" width="350" height="180" 695: * alt="A drawing of the area spanned by the curve" /> 696: * 697: * <p>The above drawing illustrates in which area points are 698: * considered “inside” a QuadCurve2D. 699: */ 700: public boolean contains(double x, double y) 701: { 702: if (! getBounds2D().contains(x, y)) 703: return false; 704: 705: return ((getAxisIntersections(x, y, true, BIG_VALUE) & 1) != 0); 706: } 707: 708: /** 709: * Determines whether a point is inside the area bounded 710: * by the curve and the straight line connecting its end points. 711: * 712: * <p><img src="doc-files/QuadCurve2D-5.png" width="350" height="180" 713: * alt="A drawing of the area spanned by the curve" /> 714: * 715: * <p>The above drawing illustrates in which area points are 716: * considered “inside” a QuadCurve2D. 717: */ 718: public boolean contains(Point2D p) 719: { 720: return contains(p.getX(), p.getY()); 721: } 722: 723: /** 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. 726: * 727: * <p><img src="doc-files/QuadCurve2D-5.png" width="350" height="180" 728: * alt="A drawing of the area spanned by the curve" /> 729: * 730: * <p>The above drawing illustrates in which area points are 731: * considered “inside” in a CubicCurve2D. 732: */ 733: public boolean intersects(double x, double y, double w, double h) 734: { 735: if (! getBounds2D().contains(x, y, w, h)) 736: return false; 737: 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 */ 743: return true; 744: 745: /* No intersections, is any point inside? */ 746: if ((getAxisIntersections(x, y, true, BIG_VALUE) & 1) != 0) 747: return true; 748: 749: return false; 750: } 751: 752: /** 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) 756: */ 757: public boolean intersects(Rectangle2D r) 758: { 759: return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight()); 760: } 761: 762: /** 763: * Determines whether a rectangle is entirely inside the area bounded 764: * by the curve and the straight line connecting its end points. 765: * 766: * <p><img src="doc-files/QuadCurve2D-5.png" width="350" height="180" 767: * alt="A drawing of the area spanned by the curve" /> 768: * 769: * <p>The above drawing illustrates in which area points are 770: * considered “inside” a QuadCurve2D. 771: * @see #contains(double, double) 772: */ 773: public boolean contains(double x, double y, double w, double h) 774: { 775: if (! getBounds2D().intersects(x, y, w, h)) 776: return false; 777: 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 */ 783: return false; 784: 785: /* No intersections, is any point inside? */ 786: if ((getAxisIntersections(x, y, true, BIG_VALUE) & 1) != 0) 787: return true; 788: 789: return false; 790: } 791: 792: /** 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) 796: */ 797: public boolean contains(Rectangle2D r) 798: { 799: return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight()); 800: } 801: 802: /** 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 807: * curve. 808: * 809: * <p><img src="doc-files/QuadCurve2D-2.png" width="350" height="180" 810: * alt="An illustration of the bounds of a QuadCurve2D" /> 811: */ 812: public Rectangle getBounds() 813: { 814: return getBounds2D().getBounds(); 815: } 816: 817: public PathIterator getPathIterator(final AffineTransform at) 818: { 819: return new PathIterator() 820: { 821: /** Current coordinate. */ 822: private int current = 0; 823: 824: public int getWindingRule() 825: { 826: return WIND_NON_ZERO; 827: } 828: 829: public boolean isDone() 830: { 831: return current >= 2; 832: } 833: 834: public void next() 835: { 836: current++; 837: } 838: 839: public int currentSegment(float[] coords) 840: { 841: int result; 842: switch (current) 843: { 844: case 0: 845: coords[0] = (float) getX1(); 846: coords[1] = (float) getY1(); 847: result = SEG_MOVETO; 848: break; 849: case 1: 850: coords[0] = (float) getCtrlX(); 851: coords[1] = (float) getCtrlY(); 852: coords[2] = (float) getX2(); 853: coords[3] = (float) getY2(); 854: result = SEG_QUADTO; 855: break; 856: default: 857: throw new NoSuchElementException("quad iterator out of bounds"); 858: } 859: if (at != null) 860: at.transform(coords, 0, coords, 0, 2); 861: return result; 862: } 863: 864: public int currentSegment(double[] coords) 865: { 866: int result; 867: switch (current) 868: { 869: case 0: 870: coords[0] = getX1(); 871: coords[1] = getY1(); 872: result = SEG_MOVETO; 873: break; 874: