1 7 8 package java.awt.geom; 9 10 23 public abstract class Rectangle2D extends RectangularShape { 24 29 public static final int OUT_LEFT = 1; 30 31 36 public static final int OUT_TOP = 2; 37 38 43 public static final int OUT_RIGHT = 4; 44 45 50 public static final int OUT_BOTTOM = 8; 51 52 57 public static class Float extends Rectangle2D { 58 62 public float x; 63 64 68 public float y; 69 70 74 public float width; 75 76 80 public float height; 81 82 87 public Float() { 88 } 89 90 102 public Float(float x, float y, float w, float h) { 103 setRect(x, y, w, h); 104 } 105 106 112 public double getX() { 113 return (double) x; 114 } 115 116 122 public double getY() { 123 return (double) y; 124 } 125 126 132 public double getWidth() { 133 return (double) width; 134 } 135 136 142 public double getHeight() { 143 return (double) height; 144 } 145 146 153 public boolean isEmpty() { 154 return (width <= 0.0f) || (height <= 0.0f); 155 } 156 157 169 public void setRect(float x, float y, float w, float h) { 170 this.x = x; 171 this.y = y; 172 this.width = w; 173 this.height = h; 174 } 175 176 188 public void setRect(double x, double y, double w, double h) { 189 this.x = (float) x; 190 this.y = (float) y; 191 this.width = (float) w; 192 this.height = (float) h; 193 } 194 195 201 public void setRect(Rectangle2D r) { 202 this.x = (float) r.getX(); 203 this.y = (float) r.getY(); 204 this.width = (float) r.getWidth(); 205 this.height = (float) r.getHeight(); 206 } 207 208 223 public int outcode(double x, double y) { 224 234 int out = 0; 235 if (this.width <= 0) { 236 out |= OUT_LEFT | OUT_RIGHT; 237 } else if (x < this.x) { 238 out |= OUT_LEFT; 239 } else if (x > this.x + (double) this.width) { 240 out |= OUT_RIGHT; 241 } 242 if (this.height <= 0) { 243 out |= OUT_TOP | OUT_BOTTOM; 244 } else if (y < this.y) { 245 out |= OUT_TOP; 246 } else if (y > this.y + (double) this.height) { 247 out |= OUT_BOTTOM; 248 } 249 return out; 250 } 251 252 258 public Rectangle2D getBounds2D() { 259 return new Float (x, y, width, height); 260 } 261 262 275 public Rectangle2D createIntersection(Rectangle2D r) { 276 Rectangle2D dest; 277 if (r instanceof Float ) { 278 dest = new Rectangle2D.Float (); 279 } else { 280 dest = new Rectangle2D.Double (); 281 } 282 Rectangle2D.intersect(this, r, dest); 283 return dest; 284 } 285 286 297 public Rectangle2D createUnion(Rectangle2D r) { 298 Rectangle2D dest; 299 if (r instanceof Float ) { 300 dest = new Rectangle2D.Float (); 301 } else { 302 dest = new Rectangle2D.Double (); 303 } 304 Rectangle2D.union(this, r, dest); 305 return dest; 306 } 307 308 315 public String toString() { 316 return getClass().getName() 317 + "[x=" + x + 318 ",y=" + y + 319 ",w=" + width + 320 ",h=" + height + "]"; 321 } 322 } 323 324 329 public static class Double extends Rectangle2D { 330 334 public double x; 335 336 340 public double y; 341 342 346 public double width; 347 348 352 public double height; 353 354 359 public Double() { 360 } 361 362 373 public Double(double x, double y, double w, double h) { 374 setRect(x, y, w, h); 375 } 376 377 383 public double getX() { 384 return x; 385 } 386 387 393 public double getY() { 394 return y; 395 } 396 397 403 public double getWidth() { 404 return width; 405 } 406 407 413 public double getHeight() { 414 return height; 415 } 416 417 424 public boolean isEmpty() { 425 return (width <= 0.0) || (height <= 0.0); 426 } 427 428 439 public void setRect(double x, double y, double w, double h) { 440 this.x = x; 441 this.y = y; 442 this.width = w; 443 this.height = h; 444 } 445 446 452 public void setRect(Rectangle2D r) { 453 this.x = r.getX(); 454 this.y = r.getY(); 455 this.width = r.getWidth(); 456 this.height = r.getHeight(); 457 } 458 459 474 public int outcode(double x, double y) { 475 int out = 0; 476 if (this.width <= 0) { 477 out |= OUT_LEFT | OUT_RIGHT; 478 } else if (x < this.x) { 479 out |= OUT_LEFT; 480 } else if (x > this.x + this.width) { 481 out |= OUT_RIGHT; 482 } 483 if (this.height <= 0) { 484 out |= OUT_TOP | OUT_BOTTOM; 485 } else if (y < this.y) { 486 out |= OUT_TOP; 487 } else if (y > this.y + this.height) { 488 out |= OUT_BOTTOM; 489 } 490 return out; 491 } 492 493 499 public Rectangle2D getBounds2D() { 500 return new Double (x, y, width, height); 501 } 502 503 514 public Rectangle2D createIntersection(Rectangle2D r) { 515 Rectangle2D dest = new Rectangle2D.Double (); 516 Rectangle2D.intersect(this, r, dest); 517 return dest; 518 } 519 520 531 public Rectangle2D createUnion(Rectangle2D r) { 532 Rectangle2D dest = new Rectangle2D.Double (); 533 Rectangle2D.union(this, r, dest); 534 return dest; 535 } 536 537 544 public String toString() { 545 return getClass().getName() 546 + "[x=" + x + 547 ",y=" + y + 548 ",w=" + width + 549 ",h=" + height + "]"; 550 } 551 } 552 553 564 protected Rectangle2D() { 565 } 566 567 579 public abstract void setRect(double x, double y, double w, double h); 580 581 587 public void setRect(Rectangle2D r) { 588 setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight()); 589 } 590 591 603 public boolean intersectsLine(double x1, double y1, double x2, double y2) { 604 int out1, out2; 605 if ((out2 = outcode(x2, y2)) == 0) { 606 return true; 607 } 608 while ((out1 = outcode(x1, y1)) != 0) { 609 if ((out1 & out2) != 0) { 610 return false; 611 } 612 if ((out1 & (OUT_LEFT | OUT_RIGHT)) != 0) { 613 double x = getX(); 614 if ((out1 & OUT_RIGHT) != 0) { 615 x += getWidth(); 616 } 617 y1 = y1 + (x - x1) * (y2 - y1) / (x2 - x1); 618 x1 = x; 619 } else { 620 double y = getY(); 621 if ((out1 & OUT_BOTTOM) != 0) { 622 y += getHeight(); 623 } 624 x1 = x1 + (y - y1) * (x2 - x1) / (y2 - y1); 625 y1 = y; 626 } 627 } 628 return true; 629 } 630 631 641 public boolean intersectsLine(Line2D l) { 642 return intersectsLine(l.getX1(), l.getY1(), l.getX2(), l.getY2()); 643 } 644 645 660 public abstract int outcode(double x, double y); 661 662 677 public int outcode(Point2D p) { 678 return outcode(p.getX(), p.getY()); 679 } 680 681 693 public void setFrame(double x, double y, double w, double h) { 694 setRect(x, y, w, h); 695 } 696 697 703 public Rectangle2D getBounds2D() { 704 return (Rectangle2D ) clone(); 705 } 706 707 716 public boolean contains(double x, double y) { 717 double x0 = getX(); 718 double y0 = getY(); 719 return (x >= x0 && 720 y >= y0 && 721 x < x0 + getWidth() && 722 y < y0 + getHeight()); 723 } 724 725 740 public boolean intersects(double x, double y, double w, double h) { 741 if (isEmpty() || w <= 0 || h <= 0) { 742 return false; 743 } 744 double x0 = getX(); 745 double y0 = getY(); 746 return (x + w > x0 && 747 y + h > y0 && 748 x < x0 + getWidth() && 749 y < y0 + getHeight()); 750 } 751 752 766 public boolean contains(double x, double y, double w, double h) { 767 if (isEmpty() || w <= 0 || h <= 0) { 768 return false; 769 } 770 double x0 = getX(); 771 double y0 = getY(); 772 return (x >= x0 && 773 y >= y0 && 774 (x + w) <= x0 + getWidth() && 775 (y + h) <= y0 + getHeight()); 776 } 777 778 789 public abstract Rectangle2D createIntersection(Rectangle2D r); 790 791 807 public static void intersect(Rectangle2D src1, 808 Rectangle2D src2, 809 Rectangle2D dest) { 810 double x1 = Math.max(src1.getMinX(), src2.getMinX()); 811 double y1 = Math.max(src1.getMinY(), src2.getMinY()); 812 double x2 = Math.min(src1.getMaxX(), src2.getMaxX()); 813 double y2 = Math.min(src1.getMaxY(), src2.getMaxY()); 814 dest.setFrame(x1, y1, x2-x1, y2-y1); 815 } 816 817 828 public abstract Rectangle2D createUnion(Rectangle2D r); 829 830 846 public static void union(Rectangle2D src1, 847 Rectangle2D src2, 848 Rectangle2D dest) { 849 double x1 = Math.min(src1.getMinX(), src2.getMinX()); 850 double y1 = Math.min(src1.getMinY(), src2.getMinY()); 851 double x2 = Math.max(src1.getMaxX(), src2.getMaxX()); 852 double y2 = Math.max(src1.getMaxY(), src2.getMaxY()); 853 dest.setFrameFromDiagonal(x1, y1, x2, y2); 854 } 855 856 874 public void add(double newx, double newy) { 875 double x1 = Math.min(getMinX(), newx); 876 double x2 = Math.max(getMaxX(), newx); 877 double y1 = Math.min(getMinY(), newy); 878 double y2 = Math.max(getMaxY(), newy); 879 setRect(x1, y1, x2 - x1, y2 - y1); 880 } 881 882 900 public void add(Point2D pt) { 901 add(pt.getX(), pt.getY()); 902 } 903 904 912 public void add(Rectangle2D r) { 913 double x1 = Math.min(getMinX(), r.getMinX()); 914 double x2 = Math.max(getMaxX(), r.getMaxX()); 915 double y1 = Math.min(getMinY(), r.getMinY()); 916 double y2 = Math.max(getMaxY(), r.getMaxY()); 917 setRect(x1, y1, x2 - x1, y2 - y1); 918 } 919 920 936 public PathIterator getPathIterator(AffineTransform at) { 937 return new RectIterator (this, at); 938 } 939 940 961 public PathIterator getPathIterator(AffineTransform at, double flatness) { 962 return new RectIterator (this, at); 963 } 964 965 969 public int hashCode() { 970 long bits = java.lang.Double.doubleToLongBits(getX()); 971 bits += java.lang.Double.doubleToLongBits(getY()) * 37; 972 bits += java.lang.Double.doubleToLongBits(getWidth()) * 43; 973 bits += java.lang.Double.doubleToLongBits(getHeight()) * 47; 974 return (((int) bits) ^ ((int) (bits >> 32))); 975 } 976 977 990 public boolean equals(Object obj) { 991 if (obj == this) { 992 return true; 993 } 994 if (obj instanceof Rectangle2D ) { 995 Rectangle2D r2d = (Rectangle2D ) obj; 996 return ((getX() == r2d.getX()) && 997 (getY() == r2d.getY()) && 998 (getWidth() == r2d.getWidth()) && 999 (getHeight() == r2d.getHeight())); 1000 } 1001 return false; 1002 } 1003} 1004 | Popular Tags |