1 // ---
  2 // Copyright (c) 2010 Francesco Cottone, http://www.kesiev.com/
  3 // ---
  4 
  5 /**
  6  * @namespace Trigo module provides some math stuff for moving objects in a 
  7  * direction or following a round path.
  8  */
  9 var trigo={
 10 	ANGLE_RIGHT:0,
 11 	ANGLE_DOWN:Math.PI*0.5,
 12 	ANGLE_LEFT:Math.PI,
 13 	ANGLE_UP:Math.PI*1.5555555,
 14  
 15  /**
 16  * Adds two angles together (radians).
 17  * @param {Float} a Base angle.
 18  * @param {Float} add The angle you're adding to the base angle.
 19  * @returns The resultant angle, always between 0 and 2*pi.
 20  */
 21 	addAngle:function(a,add) {
 22 		a=(a+add)%(Math.PI*2);
 23 		if (a<0) return (Math.PI*2)+a; else return a;
 24 	},
 25   /**
 26   * Gets the distance between two points.
 27   * @param {Object} p1 This is an object containing x and y params for the first point.
 28   * @param {Object} p2 This is an object containing x and y params for the second point.
 29   * @returns The distance between p1 and p2.
 30   */
 31 	getDistance:function(p1,p2) {
 32 		return Math.sqrt(Math.pow(p2.x-p1.x,2)+Math.pow(p2.y-p1.y,2))
 33 	},
 34   
 35   /**
 36   * Calculates the angle between two points.
 37   * @param {Object} p1 This is an object containing x and y params for the first point.
 38   * @param {Object} p2 This is an object containing x and y params for the second point.
 39   * @param {Float} transl (Optional) Adds an angle (in radians) to the result. Defaults to 0.
 40   * @returns The angle between points p1 and p2, plus transl.
 41   */
 42 	getAngle:function(p1,p2,transl) {
 43 		return this.addAngle(Math.atan2(p2.y-p1.y,p2.x-p1.x),(transl?transl:0));
 44 	},
 45   
 46   /**
 47   * Translates a point by a vector defined by angle and distance. This does not return a value but rather modifies the x and y values of p1.
 48   * @param {Object} p1 This is an object containing x and y params for the point.
 49   * @param {Float} a The angle of translation (rad).
 50   * @param {Float} d The distance of translation.
 51   */  
 52 	translate:function(p1,a,d) {
 53 		p1.x=p1.x+Math.cos(a)*d;
 54 		p1.y=p1.y+Math.sin(a)*d;
 55 	},
 56   
 57   /**
 58   * Translates an x component of a coordinate by a vector defined by angle and distance. This returns its component translation.
 59   * @param {Float} x1 This is an x coordinate.
 60   * @param {Float} a The angle of translation (rad).
 61   * @param {Float} d The distance of translation.
 62   */    
 63 	translateX:function(x1,a,d) {
 64 		return x1+Math.cos(a)*d
 65 	},
 66 
 67   /**
 68   * Translates a y component of a coordinate by a vector defined by angle and distance. This returns its component translation.
 69   * @param {Float} y1 This is a y coordinate.
 70   * @param {Float} a The angle of translation (rad).
 71   * @param {Float} d The distance of translation.
 72   */    
 73 	translateY:function(y1,a,d) {
 74 		return y1+Math.sin(a)*d;
 75 	}
 76 }
 77