Advertisement
tatactic

Trigonometry

May 2nd, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.utils
  2. {
  3.     import flash.display.DisplayObjectContainer;
  4.     import flash.display.Graphics;
  5.     import flash.display.Sprite;
  6.     import flash.geom.Point;
  7.     /**
  8.      * SOH Sinus (Opposé / Hypoténuse)<br/>
  9.      * CAH Cosinus (Adjacent / Hypoténuse)<br/>
  10.      * TOA = Tangente (Opposé / Adjacent)<br/>
  11.      */
  12.     public final class Trigono
  13.     {
  14.         private static var p1:Point;
  15.         private static var p2:Point;
  16.         private static var p3:Point;
  17.         private static var distance:Number;
  18.         private static var targetObject:Sprite;
  19.         private static var hypotenuse:Number;
  20.         private static var dx:Number;
  21.         private static var dy:Number;
  22.         private static var g:Graphics;
  23.         private static var smallDist:Number;
  24.         private static var bigDist:Number;
  25.         private static const GREEN:int = 0x00ff00;
  26.         private static const BLUE:int = 0x0000ff;
  27.         private static const RED:int = 0xff0000;
  28.        
  29.         public static function get hypo():Number{
  30.             if(!hypotenuse){
  31.                 getDist();
  32.             }
  33.             return hypotenuse;
  34.         }
  35.         public static function get small():Number{
  36.             if(!hypotenuse){
  37.                 getDist();
  38.             }
  39.             return smallDist;
  40.         }
  41.         public static function get big():Number{
  42.             if(!hypotenuse){
  43.                 getDist();
  44.             }
  45.             return bigDist;
  46.         }
  47.         public static function get thirdPoint():Point{
  48.             if(!p3){
  49.                 computeEdges();
  50.             }
  51.             return p3;
  52.         }
  53.         public static function rad2Deg(alpha:Number):Number{
  54.             return alpha*180/Math.PI;
  55.         }
  56.         public static function setPositions(o1:DisplayObjectContainer,o2:DisplayObjectContainer):void{
  57.             p1 = new Point(o1.x,o1.y);
  58.             p2 = new Point(o2.x,o2.y);
  59.         }
  60.         public static function getAngle():Number{
  61.             var rotation = rad2Deg(Math.atan2(dy, dx));
  62.             return rotation;
  63.         }
  64.         private static function getDist():Number{
  65.             // the distanc may be positive or negative
  66.             dx = p2.x - p1.x;
  67.             dy = p2.y - p1.y;
  68.             if(dx<0 || dy<0){
  69.                 distance = -Math.sqrt(Math.pow(dx,2)+Math.pow(dy,2));
  70.             }else{
  71.                 distance = Math.sqrt(Math.pow(dx,2)+Math.pow(dy,2));
  72.             }
  73.             hypotenuse = Math.abs(distance);
  74.             computeEdges();
  75.             return hypotenuse;
  76.         }
  77.         public static function setTarget(t:Sprite):void{
  78.             targetObject = t;
  79.         }
  80.         private static function drawHypotenuse():void{
  81.             if(!targetObject){
  82.                 throw new Error("   You must first call setTarget(t:Sprite) before to call drawHypotenuse()");
  83.             }
  84.             getDist();
  85.             g = (targetObject as Sprite).graphics;
  86.             g.lineStyle(1,RED,1);
  87.             g.moveTo(p1.x,p1.y);
  88.             g.lineTo(p2.x,p2.y);
  89.         }
  90.         public static function computeEdges():void{
  91.             if(!hypotenuse){
  92.                 getDist();
  93.             }
  94.             if(Math.abs(dx) > Math.abs(dy)){
  95.                 bigDist = p2.x - p1.x;
  96.                 smallDist = p2.y - p1.y;
  97.                 p3 = new Point(p2.x,p1.y);
  98.             }else{
  99.                 bigDist = p2.y - p1.y;
  100.                 smallDist = p2.x - p1.x;
  101.                 p3 = new Point(p1.x,p2.y);
  102.             }
  103.            
  104.         }
  105.         public static function drawEdges():void{
  106.             if(!targetObject){
  107.                 throw new Error("   You must first call setTarget(t:Sprite) before to call drawEdges()");
  108.             }
  109.             getDist();
  110.             g = (targetObject as Sprite).graphics;
  111.             if(Math.abs(dx) > Math.abs(dy)){
  112.                 // bigDistance
  113.                 bigDist = p2.x - p1.x;
  114.                 smallDist = p2.y - p1.y;
  115.                 g.lineStyle(2,GREEN,1);
  116.                 g.moveTo(p2.x,p2.y);
  117.                 g.lineTo(p2.x,p1.y);
  118.                 g.lineStyle(2,BLUE,1);
  119.                 g.moveTo(p1.x,p1.y);
  120.                 g.lineTo(p2.x,p1.y);
  121.             }else{
  122.                 // smallDistance
  123.                 bigDist = p2.y - p1.y;
  124.                 smallDist = p2.x - p1.x;
  125.                 g.lineStyle(2,GREEN,1);
  126.                 g.moveTo(p1.x,p2.y);
  127.                 g.lineTo(p2.x,p2.y);
  128.                 g.lineStyle(2,BLUE,1);
  129.                 g.moveTo(p1.x,p1.y);
  130.                 g.lineTo(p1.x,p2.y);
  131.             }
  132.         }
  133.     }
  134.    
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement