1  /**
     2   * com.sekati.geom.Point
     3   * @version 1.0.5
     4   * @author jason m horwitz | sekati.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   */
     8  
     9  import com.sekati.core.CoreObject;
    10  import com.sekati.geom.IPoint;
    11  import com.sekati.geom.TrigoBase;
    12  /**
    13   * Create a Point Object
    14   * 
    15   * {@code Usage:
    16   * var Point = new com.sekati.geom.Point(10,10);
    17   * }
    18   */
    19  //
    20  class com.sekati.geom.Point extends CoreObject implements IPoint {
    21  	public var x:Number;
    22  	public var y:Number;
    23  	/**
    24  	 * Point Constructor
    25  	 * @param nX (Number)
    26  	 * @param nY (Number)
    27  	 */
    28  	public function Point(nX:Number, nY:Number) {
    29  		super( );
    30  		x = nX;
    31  		y = nY;
    32  	}
    33  	/**
    34  	 * check if two points match
    35  	 * @param p (Point)
    36  	 * @return Boolean
    37  	 */
    38  	public function isEqual(p:Point):Boolean {
    39  		return (p.x == x && p.y == y);
    40  	}
    41  	/**
    42  	 * get distance between two points
    43  	 * @param p (Point)
    44  	 * @return Number
    45  	 */
    46  	public function getDistance(p:Point):Number {
    47  		return TrigoBase.getDistance( this, p );
    48  	}
    49  	/**
    50  	 * Algo to give the grid based distance when only vertical & horizontal moves are allowed
    51  	 * @param p (Point)
    52  	 * @return Number
    53  	 */
    54  	public function getAbsoluteGridDistance(p:Point):Number {
    55  		return Math.abs( x - p.x ) + Math.abs( y - p.y );
    56  	}
    57  	/**
    58  	 * Algo to give the grid based distance when diagonal moves are allowed
    59  	 * by finding math.min of the differences, we're figuring out how many moves can be diagonal ones.
    60  	 * Then we can just substract that number from the normal .getAbsoluteGridDistance() method since diagonals take
    61  	 * 1 move instead of the usual 2
    62  	 * @param p (Point)
    63  	 * @return Number
    64  	 */
    65  	public function getAbsoluteGridDistanceAllowDiagonals(p:Point):Number {
    66  		var offset:Number = Math.min( Math.abs( x - p.x ), Math.abs( y - p.y ) );
    67  		return getAbsoluteGridDistance( p ) - offset;
    68  	}
    69  	/**
    70  	 * Get the angle degree between this point and a second point.
    71  	 * @param p (Point)
    72  	 * @return Number
    73  	 */
    74  	public function getAngle(p:Point):Number {
    75  		return Math.atan( (this.y - p.y) / (this.x - p.x) ) / (Math.PI / 180);
    76  	}
    77  	/**
    78  	 * Returns a new point based on this point with x and y offset values
    79  	 * @param nX (Number)
    80  	 * @param nY (Number)
    81  	 * @return Point
    82  	 */
    83  	public function displace(nX:Number, nY:Number):Point {
    84  		return new Point( x + nX, y + nY );
    85  	}
    86  	/**
    87  	 * Offset the Point object by a specified amount.
    88  	 * @param x (Number) horizontal offset
    89  	 * @param y (Number) vertical offset
    90  	 * @return Void
    91  	 */
    92  	public function offset(x:Number, y:Number):Void {
    93  		this.x += x;
    94  		this.y += y;
    95  	}
    96  	/**
    97  	 * Rotate this Point around another Point by the specified angle.
    98  	 * @param p (Point)
    99  	 * @param angle (Number)
   100  	 * @return Void
   101  	 */
   102  	public function rotate(p:Point, angle:Number):Void {
   103  		var radians:Number = TrigoBase.angle2radian( angle );	
   104  		var baseX:Number = this.x - p.x;
   105  		var baseY:Number = this.y - p.y;
   106  		this.x = (Math.cos( radians ) * baseX) - (Math.sin( radians ) * baseY) + p.x;
   107  		this.y = (Math.sin( radians ) * baseX) + (Math.cos( radians ) * baseY) + p.y;	
   108  	}	
   109  	/**
   110  	 * Clone this Point.
   111  	 * @return Point
   112  	 */
   113  	public function clone():Point {
   114  		return new Point( this.x, this.y );
   115  	}
   116  }