001/*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005package armyc2.c2sd.graphics2d;
006
007/**
008 *
009 * @author Michael Deutch
010 */
011public class Rectangle implements Shape{
012    public int x=0;
013    public int y=0;
014    public int width=0;
015    public int height=0;
016    public Rectangle()
017    {
018        x=0;
019        y=0;
020        width=0;
021        height=0;
022    }
023    public Rectangle(Rectangle2D rect)
024    {
025        this((int) rect.x, (int) rect.y, (int) rect.width, (int) rect.height);
026    }
027
028    public Rectangle(Rectangle rect)
029    {
030        this(rect.x, rect.y, rect.width, rect.height);
031    }
032    public Rectangle(int x1, int y1, int width1, int height1)
033    {
034        x=x1;
035        y=y1;
036        width=width1;
037        height=height1;
038    }
039    public Rectangle getBounds()
040    {
041        return this;
042    }
043    public PathIterator getPathIterator(AffineTransform at)
044    {
045        return null;
046    }
047    public boolean intersects(Rectangle2D rect)
048    {
049        if(x+width<rect.x)
050            return false;
051        if(x>rect.x+rect.width)
052            return false;
053        if(y+height<rect.y)
054            return false;
055        if(y>rect.y+rect.height)
056            return false;
057        
058        return true;
059    }
060    public boolean intersects(Rectangle rect)
061    {
062        return this.intersects(rect.x, rect.y, rect.width, rect.height);
063    }
064    public boolean intersects(double x1, double y1, double width1, double height1)
065    {
066        if(x+width<x1)
067            return false;
068        if(x>x1+width1)
069            return false;
070        if(y+height<y1)
071            return false;
072        if(y>y1+height1)
073            return false;
074        
075        return true;
076    }
077    public boolean contains (int x1, int y1)
078    {
079        if(x<=x1 && x1<=x+width  && 
080                y<=y1 && y1<=y+height)
081            return true;
082        else return false;
083    }
084    public boolean contains (int x1, int y1, int width1, int height1)
085    {                             
086        if(this.contains(x1, y1) && this.contains(x1+width1, y1+height1))
087            return true;
088        else return false;
089    }
090    public boolean contains (Point2D pt)
091    {
092        if(x<=pt.getX() && pt.getX()<=x+width  && 
093                y<=pt.getY() && pt.getY()<=y+height)
094            return true;
095        else return false;
096    }
097    public Rectangle2D getBounds2D()
098    {
099        return new Rectangle2D.Double(x,y,width,height);
100    }
101     public int getX()
102    {
103        return x;
104    }
105    public int getY()
106    {
107        return y;
108    }
109    public int getMinX()
110    {
111        return x;
112    }
113    public int getMinY()
114    {
115        return y;
116    }
117    public int getMaxX()
118    {
119        return x+width;
120    }
121    public int getMaxY()
122    {
123        return y+height;
124    }
125    public int getHeight()
126    {
127        return height;
128    }
129    public int getWidth()
130    {
131        return width;
132    }
133    //must complete this function
134    public void grow(int h, int v)  
135    {
136        //return;
137    }
138    public void setRect(Rectangle rect)
139    {
140        x=rect.x;
141        y=rect.y;
142        width=rect.width;
143        height=rect.height;
144    }
145
146    public void setRect(int x, int y, int width, int height) {
147        this.x = x;
148        this.y = y;
149        this.width = width;
150        this.height = height;
151    }
152
153    public void setLocation(int x, int y) {
154        this.x = x;
155        this.y = y;
156    }
157
158    public void add(Point2D pt) {
159        int newx = (int) pt.getX();
160        int newy = (int) pt.getY();
161        if ((width | height) < 0) {
162            this.x = newx;
163            this.y = newy;
164            this.width = this.height = 0;
165            return;
166        }
167        int x1 = this.x;
168        int y1 = this.y;
169        long x2 = this.width;
170        long y2 = this.height;
171        x2 += x1;
172        y2 += y1;
173        if (x1 > newx) x1 = newx;
174        if (y1 > newy) y1 = newy;
175        if (x2 < newx) x2 = newx;
176        if (y2 < newy) y2 = newy;
177        x2 -= x1;
178        y2 -= y1;
179        if (x2 > Integer.MAX_VALUE) x2 = Integer.MAX_VALUE;
180        if (y2 > Integer.MAX_VALUE) y2 = Integer.MAX_VALUE;
181        this.x = x1;
182        this.y = y1;
183        this.width = (int) x2;
184        this.height = (int) y2;
185    }
186
187    public Rectangle union(Rectangle r) {
188        long tx2 = this.width;
189        long ty2 = this.height;
190        if ((tx2 | ty2) < 0) {
191            // This rectangle has negative dimensions...
192            // If r has non-negative dimensions then it is the answer.
193            // If r is non-existant (has a negative dimension), then both
194            // are non-existant and we can return any non-existant rectangle
195            // as an answer.  Thus, returning r meets that criterion.
196            // Either way, r is our answer.
197            return new Rectangle(r);
198        }
199        long rx2 = r.width;
200        long ry2 = r.height;
201        if ((rx2 | ry2) < 0) {
202            return new Rectangle(this);
203        }
204        int tx1 = this.x;
205        int ty1 = this.y;
206        tx2 += tx1;
207        ty2 += ty1;
208        int rx1 = r.x;
209        int ry1 = r.y;
210        rx2 += rx1;
211        ry2 += ry1;
212        if (tx1 > rx1) tx1 = rx1;
213        if (ty1 > ry1) ty1 = ry1;
214        if (tx2 < rx2) tx2 = rx2;
215        if (ty2 < ry2) ty2 = ry2;
216        tx2 -= tx1;
217        ty2 -= ty1;
218        // tx2,ty2 will never underflow since both original rectangles
219        // were already proven to be non-empty
220        // they might overflow, though...
221        if (tx2 > Integer.MAX_VALUE) tx2 = Integer.MAX_VALUE;
222        if (ty2 > Integer.MAX_VALUE) ty2 = Integer.MAX_VALUE;
223        return new Rectangle(tx1, ty1, (int) tx2, (int) ty2);
224    }
225}