/* DESCRIPTION utilities.entities.math.TrigCalc A trigonometric calculator. VERSION 1.0 TYPE Static Class HELPERS no helper classes needed METHODS RELATIVE Calc getRelAng(x1,y1, x2,y2); The relative angle between 2 points (gets radians, and then converts to angle). getRelRad(x1,y1, x2,y2); The relative radians between 2 points. getRelDist(x1,y1, x2,y2); The relative distance between 2 points. getRelAngPos(x,y, angle, distance); Returns an Object with the x,y coordinates of the arrival point, (ANGLE), {x:x,y:y}. getRelRadPos(x,y, radians, distance); Returns an Object with the x,y coordinates of the arrival point, (RADIANS), {x:x,y:y}. ABSOLUTE Calc (all is relative to ZERO) getAng(x,y); Returns the ANGLE. getRad(x,y); Returns the RADIANS. getDist(x,y); Returns the DISTANCE. getAngPos(angle,distance); Returns COORDINATES object {x:x,y:y} from angle and distance. getRadPos(radians,distance); Returns COORDINATES object {x:x,y:y} from radians and distance. USAGE var a = TrigCalc.getRelAngle(x1,x2, y1,y2); trace("angle is: " + a); var c = TrigCalc.getAngPos(angle, distance); trace("x: " + c.x + " y: " + c.y); */ /* ANALISYS */ class PippoFlash.TrigCalc { private static var radMult = Math.PI*2; // Constant to extrat radians from distance private static var angMult = 180/Math.PI; // Constant to convert radians to angle // RELATIVE Calculations ////////////////////////////////////////////////////////////////////////////////////// // Finds the ANGLE between 2 points static function getRelAng(x,y,x1,y1) :Number { return radToAng(getRelRad(x,y,x1,y1)); } // Finds the RADIANS between 2 points static function getRelRad(x,y,x1,y1) :Number { return (y1-y)<0 ? radMult-Math.acos((x1-x)/getRelDist(x,y,x1,y1)) : Math.acos((x1-x)/getRelDist(x,y,x1,y1)); } // Finds the DISTANCE between 2 points static function getRelDist(x,y,x1,y1) :Number { return Math.sqrt(Math.pow((x1-x),2)+Math.pow((y1-y),2)); } // Returns X,Y position from angle and distance static function getRelAngPos(x,y,a,d) :Object { return getRelRadPos(x,y,angToRad(a),d); } // Returns X,Y position from radians and distance static function getRelRadPos(x,y,r,d) :Object { return {x:x+(d*Math.cos(r)),y:y+(d*Math.sin(r))}; } // ABSOLUTE Calculations ////////////////////////////////////////////////////////////////////////////////////// // finds the ANGLE relative to 0 static function getAng(x,y) :Number { return radToAng(getRad(x,y)); } // finds the RADIANS relative to 0 static function getRad(x,y) :Number { return y<0 ? radMult-Math.acos(x/getDist(x,y)) : Math.acos(x/getDist(x,y)); } // finds the DISTANCE from 0 static function getDist(x,y) :Number { return Math.sqrt(Math.pow(x,2)+Math.pow(y,2)); } // Returns X,Y position from angle and distance static function getAngPos(a,d) :Object { return getRadPos(angToRad(a),d); } // Returns X,Y position from radians and distance static function getRadPos(r,d) :Object { return {x:d*Math.cos(r),y:d*Math.sin(r)}; } // CONVERSION ////////////////////////////////////////////////////////////////////////////////////////// // From ANGLE to RADIANS static function angToRad(a) :Number { return a/angMult; } // From RADIANS to ANGLE static function radToAng(r) :Number { return r*angMult; } // COMPLEX Calculations ////////////////////////////////////////////////////////////////////////////////// // Complex Functions static function rotateRad(x,y,rot) { var d = getDist(x,y); var r = (y<0 ? radMult-Math.acos(x/d) : Math.acos(x/d))+rot; return getRadPos(r,d); } }