1 31 32 package org.antlr.xjlib.appkit.gview.base; 33 34 import java.awt.*; 35 36 public class Rect { 37 38 public Rectangle r; 39 40 public Rect(Rectangle r) { 41 this.r = r; 42 } 43 44 public Rect(Vector2D c1, Vector2D c2) { 45 create(c1, c2, 0, 0); 46 } 47 48 public Rect(Vector2D c1, Vector2D c2, double min_dx, double min_dy) { 49 create(c1, c2, min_dx, min_dy); 50 } 51 52 public void create(Vector2D c1, Vector2D c2, double min_dx, double min_dy) { 53 double x = Math.min(c1.getX(), c2.getX()); 54 double y = Math.min(c1.getY(), c2.getY()); 55 double dx = Math.max(min_dx, Math.abs(c1.getX()-c2.getX())); 56 double dy = Math.max(min_dy, Math.abs(c1.getY()-c2.getY())); 57 r = new Rectangle((int)x, (int)y, (int)dx, (int)dy); 58 } 59 60 public Rect(double x, double y, double dx, double dy) { 61 if(dx<0) { 62 x += dx; 63 dx = -dx; 64 } 65 if(dy<0) { 66 y += dy; 67 dy = -dy; 68 } 69 70 r = new Rectangle((int)x, (int)y, (int)dx, (int)dy); 71 } 72 73 public Rect union(Rect r) { 74 return new Rect((Rectangle)rectangle().createUnion(r.rectangle())); 75 } 76 77 public Rectangle rectangle() { 78 return r; 79 } 80 81 public static boolean intersect(Rect r1, Rect r2) { 82 return r1.rectangle().intersects(r2.rectangle()); 83 } 84 85 public boolean contains(double x, double y) { 86 return r.contains(x, y); 87 } 88 89 public boolean contains(Point p) { 90 return r.contains(p); 91 } 92 93 public void inset(double f) { 94 r.x += f; 95 r.y += f; 96 r.width -= 2*f; 97 r.height -= 2*f; 98 } 99 100 public String toString() { 101 if(r == null) 102 return super.toString(); 103 else 104 return r.toString(); 105 } 106 } 107 | Popular Tags |