001/*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005package armyc2.c5isr.graphics2d;
006
007/**
008 *
009*
010 */
011public abstract class Rectangle2D {
012    public double x=0;
013    public double y=0;
014    public double width=0;
015    public double height=0;
016    //do not instantiate the abstract class
017    protected Rectangle2D() {
018    }
019
020    public void add(double newx, double newy) {
021        double x1 = Math.min(getMinX(), newx);
022        double x2 = Math.max(getMaxX(), newx);
023        double y1 = Math.min(getMinY(), newy);
024        double y2 = Math.max(getMaxY(), newy);
025        setRect(x1, y1, x2 - x1, y2 - y1);
026    }
027
028    public Rectangle2D createIntersection(Rectangle2D r)
029    {
030        if(r.x>this.x+this.width)
031            return null;
032        if(r.x+r.width<this.x)
033            return null;
034        if(r.y>this.y+this.height)
035            return null;
036        if(r.y+r.height<this.y)
037            return null;
038        if(r.contains(this))
039            return this;
040        if(this.contains(r))
041            return r;
042        
043        //if it gets to this point we have a normal intersection
044        double x1=0,y1=0,x2=0,y2=0;
045        if(this.x<r.x)
046        {
047            x1=r.x;
048            x2=this.x+this.width;
049        }
050        else
051        {
052            x1=this.x;
053            x2=r.x+r.width;            
054        }
055        if(this.y<r.y)
056        {
057            y1=r.y;
058            y2=this.y+this.height;
059        }
060        else
061        {
062            y1=this.y;
063            y2=r.y+r.height;
064        }
065        return new Rectangle2D.Double(x1,y1,x2-x1,y2-y1);
066    }
067    public Rectangle2D createUnion(Rectangle2D r)
068    {
069        return null;
070    }
071    public double getX()
072    {
073        return x;
074    }
075    public double getY()
076    {
077        return y;
078    }
079    public double getMinX()
080    {
081        return x;
082    }
083    public double getMinY()
084    {
085        return y;
086    }
087    public double getMaxX()
088    {
089        return x+width;
090    }
091    public double getMaxY()
092    {
093        return y+height;
094    }
095    public double getHeight()
096    {
097        return height;
098    }
099    public double getWidth()
100    {
101        return width;
102    }
103    public boolean contains(double x1, double y1)
104    {
105        if(x<=x1 && x1<=x+width  && 
106                y<=y1 && y1<=y+height)
107            return true;
108        else return false;
109    }
110    public boolean intersects(Rectangle2D rect)
111    {
112        if(x+width<rect.x)
113            return false;
114        if(x>rect.x+rect.width)
115            return false;
116        if(y+height<rect.y)
117            return false;
118        if(y>rect.y+rect.height)
119            return false;
120        
121        return true;
122    }
123    public boolean intersects(int x1, int y1, int width1, int height1)
124    {
125        if(x+width<x1)
126            return false;
127        if(x>x1+width1)
128            return false;
129        if(y+height<y1)
130            return false;
131        if(y>y1+height1)
132            return false;
133        
134        return true;
135    }
136    public boolean contains(Rectangle2D rect)
137    {
138        double x1=rect.getX();
139        double y1=rect.getY();
140        if(this.contains(x1, y1))
141        {
142            x1+=rect.getWidth();
143            y1+=rect.getHeight();
144            if(this.contains(x1,y1))
145                return true;
146        }            
147        return false;
148    }
149    public boolean contains(Point2D pt)
150    {
151        if(x<=pt.getX() && pt.getX()<=x+width)
152            if(y<=pt.getY() && pt.getY()<=y+height)
153                return true;
154        
155        return false;
156    }
157    public boolean intersectsLine(Line2D line)
158    {        
159        return false;
160    }
161    public boolean contains(int x, int y, int width, int height)
162    {
163        double x1=x;
164        double y1=y;
165        if(this.contains(x1, y1))
166        {
167            x1+=width;
168            y1+=height;
169            if(this.contains(x1,y1))
170                return true;
171        }            
172        return false;
173    }
174    public boolean isEmpty()
175    {
176        if(width==0 && height==0)
177            return true;
178        else
179            return false;
180    }
181    public void setRect(double x1, double y1, double width1, double height1)
182    {
183        x=x1;
184        y=y1;
185        width=width1;
186        height=height1;
187    }
188    public void setRect(Rectangle2D r)
189    {
190        x=r.getX();
191        y=r.getY();
192        width=r.getWidth();
193        height=r.getHeight();
194    }
195
196    public static class Double extends Rectangle2D{
197        public Double()
198        {
199            x=0;
200            y=0;
201            width=0;
202            height=0;
203        }
204        public Double(double x1, double y1, double width1, double height1)                
205        {
206            x=x1;
207            y=y1;
208            width=width1;
209            height=height1;
210        }
211    }
212}