Advertisement
nutter666

MathExtras

Mar 14th, 2013
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package {
  2.  
  3.     public class MathExtras {
  4.        
  5.     /* MathExtras
  6.     A bunch of useful/commonly used maths functions that can easily be accessed from this class
  7.     */
  8.    
  9.  
  10.         public function MathExtras() {
  11.         }
  12.        
  13.         // degsToRads(number) - converts a degrees angle to a radian angle
  14.         public function degsToRads(degrees:Number):Number {
  15.             return degrees * Math.PI / 180;
  16.         }
  17.         // radsToDegs(number) - converts a radians angle to a degrees angle
  18.         public function radsToDegs(rads:Number):Number {
  19.             return rads / (Math.PI / 180);
  20.         }
  21.         // normaliseAngle(angle) - normalises an angle so it always falls between 0 and 360
  22.         public function normaliseAngle(angle:Number){
  23.         var temp = angle
  24.         while(temp < 0){
  25.         temp += 360
  26.         }
  27.         while(temp > 360){
  28.         temp -= 360
  29.         }
  30.         return temp
  31.         }
  32.  
  33.         // getDist(obj1,obj2) - returns the distance between 2 objects
  34.         public function getDist(firstObject:Object,secondObject:Object) {
  35.             var distance:Number = Math.sqrt( ( firstObject.x - secondObject.x ) * ( firstObject.x - secondObject.x ) + ( firstObject.y - secondObject.y ) * ( firstObject.y - secondObject.y ) );
  36.             return distance;
  37.         }
  38.  
  39.  
  40.         // angleToObject(obj1,obj2) - returns the angle which object1 needs to rotate to point face object2
  41.         public function angleToObject(obj:Object,obj2:Object):Number {
  42.             var dX:Number = obj2.x - obj.x
  43.             var dY:Number = obj2.y - obj.y
  44.             var angleDeg:Number = radsToDegs(Math.atan2(dY,dX));
  45.  
  46.             return angleDeg;
  47.  
  48.         }
  49.        
  50.         // roundToDecPlaces(number,places) - rounds the number to x decimal places
  51.         public function roundToDecPlaces(number:Number,places:int){
  52.         var val:int  = number * Math.pow(10,places)
  53.         return val / Math.pow(10,places)
  54.         }
  55.  
  56.     }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement