1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4  
     5  import org.aswing.geom.Dimension;
     6  import org.aswing.geom.Point;
     7  
     8  /**
     9   * A Rectangle with x, y coordinats and with, height ranges.
    10   * @author iiley
    11   */
    12  class org.aswing.geom.Rectangle{
    13  	public var x:Number;
    14  	public var y:Number;
    15  	public var width:Number;
    16  	public var height:Number;
    17  	
    18  	/**
    19  	 * <br>
    20  	 * Rectangle(x:Number, y:Number, width:Number, height:Number)<br>
    21  	 * Rectangle(rect:Rectangle)<br>
    22  	 */
    23  	public function Rectangle(x, y:Number, width:Number, height:Number){
    24  		setRect(x, y, width, height);
    25  	}
    26  	
    27  	/**
    28  	 * <br>
    29  	 * setRect(x:Number, y:Number, width:Number, height:Number)<br>
    30  	 * setRect(rect:Rectangle)<br>
    31  	 */
    32  	public function setRect(x, y:Number, width:Number, height:Number):Void{
    33  		if(x instanceof Rectangle){
    34  			this.x = x.x;
    35  			this.y = x.y;
    36  			this.width = x.width;
    37  			this.height = x.height;
    38  		}else{
    39  			if(x == undefined) x = 0;
    40  			if(y == undefined) y = 0;		
    41  			if(width == undefined) width = 0;
    42  			if(height == undefined) height = 0;
    43  			this.x = x;
    44  			this.y = y;
    45  			this.width = width;
    46  			this.height = height;
    47  		}
    48  	}
    49  	
    50  	/**
    51  	 * <br>
    52  	 * setLocation(x:Number, y:Number)<br>
    53  	 * setLocation(p:Point)<br>
    54  	 */
    55  	public function setLocation(x, y:Number):Void{
    56  		if(x instanceof Point){
    57  			this.x = x.x;
    58  			this.y = x.y;
    59  		}else{
    60  			this.x = x;
    61  			this.y = y;
    62  		}
    63  	}
    64  	
    65  	/**
    66  	 * <br>
    67  	 * setSize(width:Number, height:Number)<br>
    68  	 * setSize(dim:Dimension)<br>
    69  	 */
    70  	public function setSize(width, height:Number):Void{
    71  		if(width instanceof Dimension){
    72  			this.width = width.width;
    73  			this.height = width.height;
    74  		}else{
    75  			this.width = width;
    76  			this.height = height;
    77  		}
    78  	}
    79  	
    80  	public function getSize():Dimension{
    81  		return new Dimension(width, height);
    82  	}
    83  	
    84  	public function getLocation():Point{
    85  		return new Point(x, y);
    86  	}
    87  	
    88      /**
    89       * Computes the union of this <code>Rectangle</code> with the 
    90       * specified <code>Rectangle</code>. Returns a new 
    91       * <code>Rectangle</code> that 
    92       * represents the union of the two rectangles
    93       * @param r the specified <code>Rectangle</code>
    94       * @return    the smallest <code>Rectangle</code> containing both 
    95       *		  the specified <code>Rectangle</code> and this 
    96       *		  <code>Rectangle</code>.
    97       */
    98      public function union(r:Rectangle):Rectangle{
    99  		var x1:Number = Math.min(x, r.x);
   100  		var x2:Number = Math.max(x + width, r.x + r.width);
   101  		var y1:Number = Math.min(y, r.y);
   102  		var y2:Number = Math.max(y + height, r.y + r.height);
   103  		return new Rectangle(x1, y1, x2 - x1, y2 - y1);
   104      }
   105  	
   106  	public function leftTop():Point{
   107  		return new Point(x, y);
   108  	}
   109  	
   110  	public function rightTop():Point{
   111  		return new Point(x + width, y);
   112  	}
   113  	
   114  	public function leftBottom():Point{
   115  		return new Point(x, y + height);
   116  	}
   117  	
   118  	public function rightBottom():Point{
   119  		return new Point(x + width, y + height);
   120  	}
   121  	
   122  	public function containsPoint(p:Point):Boolean{
   123  		if(p.x < x || p.y < y || p.x > x+width || p.y > y+height){
   124  			return false;
   125  		}else{
   126  			return true;
   127  		}
   128  	}
   129  	
   130  	public function equals(o:Object):Boolean{
   131  		var r:Rectangle = Rectangle(o);
   132  		return x===r.x && y===r.y && width===r.width && height===r.height;
   133  	}
   134  		
   135  	public function toString():String{
   136  		return "Rectangle(x:"+x+",y:"+y+", width:"+width+",height:"+height+")";
   137  	}
   138  }
   139