1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4  
     5  /**
     6   * A point with x and y coordinates.
     7   * @author iiley
     8   */
     9  class org.aswing.geom.Point{
    10  	
    11  	public var x:Number = 0;
    12  	public var y:Number = 0;
    13  	
    14  	/**
    15  	 * Constructor
    16  	 * <br>
    17  	 * Point(x:Number, y:Number)<br>
    18  	 * Point(p:Point)<br>
    19  	 * Point()<br>
    20  	 */
    21  	public function Point(x, y:Number){
    22  		setLocation(x, y);
    23  	}
    24  
    25  	/**
    26  	 * <br>
    27  	 * setLocation(x:Number, y:Number)<br>
    28  	 * setLocation(p:Point)<br>
    29  	 */
    30  	public function setLocation(x, y:Number):Void{
    31  		if(x instanceof Point){
    32  			this.x = x.x;
    33  			this.y = x.y;
    34  		}else{
    35  			if(x == undefined) x = 0;
    36  			if(y == undefined) y = 0;
    37  			this.x = x;
    38  			this.y = y;		
    39  		}
    40  	}
    41  	
    42  	public function move(dx:Number, dy:Number):Point{
    43  		x += dx;
    44  		y += dy;
    45  		return this;
    46  	}
    47  	
    48  	public function moveRadians(angle:Number, distance:Number):Point{
    49  		x += Math.cos(angle)*distance;
    50  		y += Math.sin(angle)*distance;
    51  		return this;
    52  	}
    53  	
    54  	public function nextPoint(angle:Number, distance:Number):Point{
    55  		return new Point(x+Math.cos(angle)*distance, y+Math.sin(angle)*distance);
    56  	}
    57  	
    58  	/**
    59  	 * <br>
    60  	 * distanceSq(x:Number, y:Number)<br>
    61  	 * distanceSq(p:Point)<br>
    62  	 *
    63  	 * @return the distance square from this to p
    64  	 */
    65  	public function distanceSq(tx, ty:Number):Number{
    66  		var xx:Number;
    67  		var yy:Number;
    68  		if(tx instanceof Point){
    69  			xx = tx.x;
    70  			yy = tx.y;
    71  		}else{
    72  			xx = tx;
    73  			yy = ty;
    74  		}
    75  		return ((x-xx)*(x-xx)+(y-yy)*(y-yy));	
    76  	}
    77  
    78  	/**
    79  	 * <br>
    80  	 * distance(x:Number, y:Number)<br>
    81  	 * distance(p:Point)<br>
    82  	 *
    83  	 * @return the distance from this to p
    84  	 */
    85  	public function distance(tx, ty:Number):Number{
    86  		return Math.sqrt(distanceSq(tx, ty));
    87  	}
    88      
    89  	public function equals(o:Object):Boolean{
    90  		var p:Point = Point(o);
    91  		return x===p.x && y===p.y;
    92  	}    
    93      
    94  	public function toString():String{
    95  		return "Point("+x+","+y+")";
    96  	}	
    97  }
    98