Rect.javaThis describes a rectangle, aligned with the screen axes, and calculates the intersection and bounding box of two rectangles.
001: //Rect.java
002: //Definition of class Rect
003: import java.awt.Graphics;
004: import java.awt.Color;
005:
006: /**
007: * <b>Rect</b> describes a two-dimensional rectangle.
008: * coordinates increase down and to the right
009: * positions are integers
010: */
011:
012: public class Rect
013: {
014: int xmin,ymin,xmax,ymax;
015: Color fillcolor = Color.white;
016:
017: // No-argument constructor
018: public Rect() { set( 0, 0, 1, 1 ); }
019:
020:
021: // Constructor
022: public Rect( int x1, int y1 , int x2, int y2 )
023: {
024: set(x1,y1,x2,y2);
025: }
026:
027: // copy constructor
028: public Rect( Rect src)
029: {
030: set(src.xmin,src.ymin,src.xmax,src.ymax);
031: }
032:
033: public int[] size() {
034: int[] mySize = new int[2];
035: mySize[0]=xmax-xmin+1;
036: mySize[1]=ymax-ymin+1;
037: return mySize;
038: }
039:
040:
041:
042: // Set x and y coordinates of Point
043: public Rect set( int x1, int y1, int x2, int y2 )
044: {
045: if (x1>x2) {
046: xmin = x2;
047: xmax = x1;
048: }
049: else {
050: xmin = x1;
051: xmax = x2;
052: }
053: if (y1>y2) {
054: ymin = y2;
055: ymax = y1;
056: }
057: else {
058: ymin = y1;
059: ymax = y2;
060: }
061: return this;
062: }
063:
064: public boolean contains(int a, int b){
065: if (a>xmax) return false;
066: if (a<xmin) return false;
067: if (b<ymin) return false;
068: if (b>ymax) return false;
069: return true;
070: }
071:
072: public boolean contains(Rect r)
073: {
074: if (r.xmin<xmin) return false;
075: if (r.xmax>xmax) return false;
076: if (r.ymin<ymin) return false;
077: if (r.ymax>ymax) return false;
078: return true;
079: }
080:
081:
082: static public Rect bounding(Rect a, Rect b)
083: {
084: int x1 = Math.min(a.xmin,b.xmin);
085: int y1 = Math.min(a.ymin,b.ymin);
086: int x2 = Math.max(a.xmax,b.xmax);
087: int y2 = Math.max(a.ymax,b.ymax);
088: return new Rect(x1,y1,x2,y2);
089: }
090:
091: static public Rect intersection(Rect a, Rect b)
092: {
093: if (a.xmin > b.xmax) return null;
094: if (a.xmax < b.xmin) return null;
095: if (a.ymin > b.ymax) return null;
096: if (a.ymax < b.ymin) return null;
097: int x1 = Math.max(a.xmin,b.xmin);
098: int y1 = Math.max(a.ymin,b.ymin);
099: int x2 = Math.min(a.xmax,b.xmax);
100: int y2 = Math.min(a.ymax,b.ymax);
101: return new Rect(x1,y1,x2,y2);
102: }
103:
104: public int area()
105: {
106: return (xmax-xmin+1)*(ymax-ymin+1);
107: }
108:
109:
110: // multiply point by a scalar
111: public Rect scale(double s)
112: {
113: xmax = xmin + (int) Math.round((xmax-xmin)*s);
114: ymax = ymin + (int) Math.round((ymax-ymin)*s);
115: return this;
116: }
117:
118:
119: // offset (translate) point by the amount (tx, ty)
120: public Rect translate(int tx, int ty)
121: {
122: xmin += tx;
123: ymin += ty;
124: xmax += tx;
125: ymax += ty;
126: return this;
127: }
128:
129:
130:
131: public void setSize(int width, int height)
132: {
133: if (width<1) width = 1;
134: if (height<1) height = 1;
135: xmax = xmin + width - 1;
136: ymax = ymin + height - 1;
137: }
138:
139: public void draw(Graphics g)
140: {
141: Color oldcolor = g.getColor();
142: if (fillcolor!=null){
143: g.setColor(fillcolor);
144: g.fillRect(xmin,ymin,xmax-xmin+1,ymax-ymin+1);
145: }
146: g.setColor(Color.black);
147: g.drawRect(xmin,ymin,xmax-xmin+1,ymax-ymin+1);
148: g.setColor(oldcolor);
149: }
150:
151: // convert the point into a String representation
152: public String toString()
153: { return String.format("[%d, %d; %d, %d]",xmin,ymin,xmax,ymax); }
154:
155: static public void main(String args[])
156: {
157: Rect a, b;
158: a = new Rect(20,50,220,250);
159: System.out.println("a = " + a);
160: b = new Rect(1,0,10,20);
161: System.out.println("b = " + b + String.format(" area %d",b.area()));
162:
163: b.translate(2,10);
164: System.out.println("New b location = " + b );
165: b.scale(3.0);
166: System.out.println("New b size (3x) = " + b );
167: b.scale(1.5);
168: System.out.println("New b size (1.5x) = " + b );
169: b.scale((1/1.5));
170: System.out.println("New b size (x/1.5) = " + b );
171: b.scale((1/3.0));
172: System.out.println("New (Original?) b size (x/3) = " + b );
173: b.setSize(10, 30);
174: System.out.println("New b size set to (10,30) = " + b );
175: System.out.println("b contains (4,13) = " + b.contains(4,13));
176: System.out.println("b contains (4,43) = " + b.contains(4,43));
177: System.out.println("Intersection of a = " + a + "\n\tand b = " + b);
178: System.out.println("\t= " + intersection(a, b));
179: System.out.println("Bounding Rect of a = " + a + "\n\tand b = " + b);
180: System.out.println("\t= " + bounding(a, b));
181:
182: b.scale(3.0);
183: System.out.println("New b size (3x) = " + b );
184:
185: System.out.println("Intersection of a = " + a + "\n\tand b = " + b);
186: System.out.println("\t= " + intersection(a, b));
187: System.out.println("Bounding Rect of a = " + a + "\n\tand b = " + b);
188: System.out.println("\t= " + bounding(a, b));
189:
190:
191: }
192:
193: }
a = [20, 50; 220, 250] b = [1, 0; 10, 20] area 210 New b location = [3, 10; 12, 30] New b size (3x) = [3, 10; 30, 70] New b size (1.5x) = [3, 10; 44, 100] New b size (x/1.5) = [3, 10; 30, 70] New (Original?) b size (x/3) = [3, 10; 12, 30] New b size set to (10,30) = [3, 10; 12, 39] b contains (4,13) = true b contains (4,43) = false Intersection of a = [20, 50; 220, 250] and b = [3, 10; 12, 39] = null Bounding Rect of a = [20, 50; 220, 250] and b = [3, 10; 12, 39] = [3, 10; 220, 250] New b size (3x) = [3, 10; 30, 97] Intersection of a = [20, 50; 220, 250] and b = [3, 10; 30, 97] = [20, 50; 30, 97] Bounding Rect of a = [20, 50; 220, 250] and b = [3, 10; 30, 97] = [3, 10; 220, 250]
Maintained by John Loomis, updated Sat Mar 08 19:21:59 2008