1 7 8 package java.awt.geom; 9 10 import java.awt.Shape ; 11 import java.awt.Rectangle ; 12 13 30 public abstract class Line2D implements Shape , Cloneable { 31 34 public static class Float extends Line2D { 35 38 public float x1; 39 40 43 public float y1; 44 45 48 public float x2; 49 50 53 public float y2; 54 55 58 public Float() { 59 } 60 61 66 public Float(float X1, float Y1, float X2, float Y2) { 67 setLine(X1, Y1, X2, Y2); 68 } 69 70 76 public Float(Point2D p1, Point2D p2) { 77 setLine(p1, p2); 78 } 79 80 85 public double getX1() { 86 return (double) x1; 87 } 88 89 94 public double getY1() { 95 return (double) y1; 96 } 97 98 103 public Point2D getP1() { 104 return new Point2D.Float (x1, y1); 105 } 106 107 112 public double getX2() { 113 return (double) x2; 114 } 115 116 121 122 public double getY2() { 123 return (double) y2; 124 } 125 126 131 public Point2D getP2() { 132 return new Point2D.Float (x2, y2); 133 } 134 135 141 public void setLine(double X1, double Y1, double X2, double Y2) { 142 this.x1 = (float) X1; 143 this.y1 = (float) Y1; 144 this.x2 = (float) X2; 145 this.y2 = (float) Y2; 146 } 147 148 154 public void setLine(float X1, float Y1, float X2, float Y2) { 155 this.x1 = X1; 156 this.y1 = Y1; 157 this.x2 = X2; 158 this.y2 = Y2; 159 } 160 161 167 public Rectangle2D getBounds2D() { 168 float x, y, w, h; 169 if (x1 < x2) { 170 x = x1; 171 w = x2 - x1; 172 } else { 173 x = x2; 174 w = x1 - x2; 175 } 176 if (y1 < y2) { 177 y = y1; 178 h = y2 - y1; 179 } else { 180 y = y2; 181 h = y1 - y2; 182 } 183 return new Rectangle2D.Float (x, y, w, h); 184 } 185 } 186 187 190 public static class Double extends Line2D { 191 194 public double x1; 195 196 199 public double y1; 200 201 204 public double x2; 205 206 209 public double y2; 210 211 214 public Double() { 215 } 216 217 223 public Double(double X1, double Y1, double X2, double Y2) { 224 setLine(X1, Y1, X2, Y2); 225 } 226 227 232 public Double(Point2D p1, Point2D p2) { 233 setLine(p1, p2); 234 } 235 236 241 public double getX1() { 242 return x1; 243 } 244 245 250 public double getY1() { 251 return y1; 252 } 253 254 260 public Point2D getP1() { 261 return new Point2D.Double (x1, y1); 262 } 263 264 269 public double getX2() { 270 return x2; 271 } 272 273 278 public double getY2() { 279 return y2; 280 } 281 282 288 public Point2D getP2() { 289 return new Point2D.Double (x2, y2); 290 } 291 292 298 public void setLine(double X1, double Y1, double X2, double Y2) { 299 this.x1 = X1; 300 this.y1 = Y1; 301 this.x2 = X2; 302 this.y2 = Y2; 303 } 304 305 311 public Rectangle2D getBounds2D() { 312 double x, y, w, h; 313 if (x1 < x2) { 314 x = x1; 315 w = x2 - x1; 316 } else { 317 x = x2; 318 w = x1 - x2; 319 } 320 if (y1 < y2) { 321 y = y1; 322 h = y2 - y1; 323 } else { 324 y = y2; 325 h = y1 - y2; 326 } 327 return new Rectangle2D.Double (x, y, w, h); 328 } 329 } 330 331 341 protected Line2D() { 342 } 343 344 349 public abstract double getX1(); 350 351 356 public abstract double getY1(); 357 358 364 public abstract Point2D getP1(); 365 366 371 public abstract double getX2(); 372 373 378 public abstract double getY2(); 379 380 385 public abstract Point2D getP2(); 386 387 393 public abstract void setLine(double X1, double Y1, double X2, double Y2); 394 395 400 public void setLine(Point2D p1, Point2D p2) { 401 setLine(p1.getX(), p1.getY(), p2.getX(), p2.getY()); 402 } 403 404 409 public void setLine(Line2D l) { 410 setLine(l.getX1(), l.getY1(), l.getX2(), l.getY2()); 411 } 412 413 447 public static int relativeCCW(double X1, double Y1, 448 double X2, double Y2, 449 double PX, double PY) { 450 X2 -= X1; 451 Y2 -= Y1; 452 PX -= X1; 453 PY -= Y1; 454 double ccw = PX * Y2 - PY * X2; 455 if (ccw == 0.0) { 456 ccw = PX * X2 + PY * Y2; 463 if (ccw > 0.0) { 464 PX -= X2; 472 PY -= Y2; 473 ccw = PX * X2 + PY * Y2; 474 if (ccw < 0.0) { 475 ccw = 0.0; 476 } 477 } 478 } 479 return (ccw < 0.0) ? -1 : ((ccw > 0.0) ? 1 : 0); 480 } 481 482 494 public int relativeCCW(double PX, double PY) { 495 return relativeCCW(getX1(), getY1(), getX2(), getY2(), PX, PY); 496 } 497 498 511 public int relativeCCW(Point2D p) { 512 return relativeCCW(getX1(), getY1(), getX2(), getY2(), 513 p.getX(), p.getY()); 514 } 515 516 532 public static boolean linesIntersect(double X1, double Y1, 533 double X2, double Y2, 534 double X3, double Y3, 535 double X4, double Y4) { 536 return ((relativeCCW(X1, Y1, X2, Y2, X3, Y3) * 537 relativeCCW(X1, Y1, X2, Y2, X4, Y4) <= 0) 538 && (relativeCCW(X3, Y3, X4, Y4, X1, Y1) * 539 relativeCCW(X3, Y3, X4, Y4, X2, Y2) <= 0)); 540 } 541 542 552 public boolean intersectsLine(double X1, double Y1, double X2, double Y2) { 553 return linesIntersect(X1, Y1, X2, Y2, 554 getX1(), getY1(), getX2(), getY2()); 555 } 556 557 564 public boolean intersectsLine(Line2D l) { 565 return linesIntersect(l.getX1(), l.getY1(), l.getX2(), l.getY2(), 566 getX1(), getY1(), getX2(), getY2()); 567 } 568 569 585 public static double ptSegDistSq(double X1, double Y1, 586 double X2, double Y2, 587 double PX, double PY) { 588 X2 -= X1; 591 Y2 -= Y1; 592 PX -= X1; 594 PY -= Y1; 595 double dotprod = PX * X2 + PY * Y2; 596 double projlenSq; 597 if (dotprod <= 0.0) { 598 projlenSq = 0.0; 602 } else { 603 PX = X2 - PX; 609 PY = Y2 - PY; 610 dotprod = PX * X2 + PY * Y2; 611 if (dotprod <= 0.0) { 612 projlenSq = 0.0; 616 } else { 617 projlenSq = dotprod * dotprod / (X2 * X2 + Y2 * Y2); 622 } 623 } 624 double lenSq = PX * PX + PY * PY - projlenSq; 629 if (lenSq < 0) { 630 lenSq = 0; 631 } 632 return lenSq; 633 } 634 635 651 public static double ptSegDist(double X1, double Y1, 652 double X2, double Y2, 653 double PX, double PY) { 654 return Math.sqrt(ptSegDistSq(X1, Y1, X2, Y2, PX, PY)); 655 } 656 657 669 public double ptSegDistSq(double PX, double PY) { 670 return ptSegDistSq(getX1(), getY1(), getX2(), getY2(), PX, PY); 671 } 672 673 687 public double ptSegDistSq(Point2D pt) { 688 return ptSegDistSq(getX1(), getY1(), getX2(), getY2(), 689 pt.getX(), pt.getY()); 690 } 691 692 704 public double ptSegDist(double PX, double PY) { 705 return ptSegDist(getX1(), getY1(), getX2(), getY2(), PX, PY); 706 } 707 708 722 public double ptSegDist(Point2D pt) { 723 return ptSegDist(getX1(), getY1(), getX2(), getY2(), 724 pt.getX(), pt.getY()); 725 } 726 727 743 public static double ptLineDistSq(double X1, double Y1, 744 double X2, double Y2, 745 double PX, double PY) { 746 X2 -= X1; 749 Y2 -= Y1; 750 PX -= X1; 752 PY -= Y1; 753 double dotprod = PX * X2 + PY * Y2; 754 double projlenSq = dotprod * dotprod / (X2 * X2 + Y2 * Y2); 758 double lenSq = PX * PX + PY * PY - projlenSq; 761 if (lenSq < 0) { 762 lenSq = 0; 763 } 764 return lenSq; 765 } 766 767 783 public static double ptLineDist(double X1, double Y1, 784 double X2, double Y2, 785 double PX, double PY) { 786 return Math.sqrt(ptLineDistSq(X1, Y1, X2, Y2, PX, PY)); 787 } 788 789 801 public double ptLineDistSq(double PX, double PY) { 802 return ptLineDistSq(getX1(), getY1(), getX2(), getY2(), PX, PY); 803 } 804 805 819 public double ptLineDistSq(Point2D pt) { 820 return ptLineDistSq(getX1(), getY1(), getX2(), getY2(), 821 pt.getX(), pt.getY()); 822 } 823 824 836 public double ptLineDist(double PX, double PY) { 837 return ptLineDist(getX1(), getY1(), getX2(), getY2(), PX, PY); 838 } 839 840 851 public double ptLineDist(Point2D pt) { 852 return ptLineDist(getX1(), getY1(), getX2(), getY2(), 853 pt.getX(), pt.getY()); 854 } 855 856 866 public boolean contains(double x, double y) { 867 return false; 868 } 869 870 880 public boolean contains(Point2D p) { 881 return false; 882 } 883 884 895 public boolean intersects(double x, double y, double w, double h) { 896 return intersects(new Rectangle2D.Double (x, y, w, h)); 897 } 898 899 907 public boolean intersects(Rectangle2D r) { 908 return r.intersectsLine(getX1(), getY1(), getX2(), getY2()); 909 } 910 911 924 public boolean contains(double x, double y, double w, double h) { 925 return false; 926 } 927 928 938 public boolean contains(Rectangle2D r) { 939 return false; 940 } 941 942 947 public Rectangle getBounds() { 948 return getBounds2D().getBounds(); 949 } 950 951 963 public PathIterator getPathIterator(AffineTransform at) { 964 return new LineIterator (this, at); 965 } 966 967 984 public PathIterator getPathIterator(AffineTransform at, double flatness) { 985 return new LineIterator (this, at); 986 } 987 988 996 public Object clone() { 997 try { 998 return super.clone(); 999 } catch (CloneNotSupportedException e) { 1000 throw new InternalError (); 1002 } 1003 } 1004} 1005 | Popular Tags |