Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.utils
- {
- import flash.display.DisplayObjectContainer;
- import flash.display.Graphics;
- import flash.display.Sprite;
- import flash.geom.Point;
- /**
- * SOH Sinus (Opposé / Hypoténuse)<br/>
- * CAH Cosinus (Adjacent / Hypoténuse)<br/>
- * TOA = Tangente (Opposé / Adjacent)<br/>
- */
- public final class Trigono
- {
- private static var p1:Point;
- private static var p2:Point;
- private static var p3:Point;
- private static var distance:Number;
- private static var targetObject:Sprite;
- private static var hypotenuse:Number;
- private static var dx:Number;
- private static var dy:Number;
- private static var g:Graphics;
- private static var smallDist:Number;
- private static var bigDist:Number;
- private static const GREEN:int = 0x00ff00;
- private static const BLUE:int = 0x0000ff;
- private static const RED:int = 0xff0000;
- public static function get hypo():Number{
- if(!hypotenuse){
- getDist();
- }
- return hypotenuse;
- }
- public static function get small():Number{
- if(!hypotenuse){
- getDist();
- }
- return smallDist;
- }
- public static function get big():Number{
- if(!hypotenuse){
- getDist();
- }
- return bigDist;
- }
- public static function get thirdPoint():Point{
- if(!p3){
- computeEdges();
- }
- return p3;
- }
- public static function rad2Deg(alpha:Number):Number{
- return alpha*180/Math.PI;
- }
- public static function setPositions(o1:DisplayObjectContainer,o2:DisplayObjectContainer):void{
- p1 = new Point(o1.x,o1.y);
- p2 = new Point(o2.x,o2.y);
- }
- public static function getAngle():Number{
- var rotation = rad2Deg(Math.atan2(dy, dx));
- return rotation;
- }
- private static function getDist():Number{
- // the distanc may be positive or negative
- dx = p2.x - p1.x;
- dy = p2.y - p1.y;
- if(dx<0 || dy<0){
- distance = -Math.sqrt(Math.pow(dx,2)+Math.pow(dy,2));
- }else{
- distance = Math.sqrt(Math.pow(dx,2)+Math.pow(dy,2));
- }
- hypotenuse = Math.abs(distance);
- computeEdges();
- return hypotenuse;
- }
- public static function setTarget(t:Sprite):void{
- targetObject = t;
- }
- private static function drawHypotenuse():void{
- if(!targetObject){
- throw new Error(" You must first call setTarget(t:Sprite) before to call drawHypotenuse()");
- }
- getDist();
- g = (targetObject as Sprite).graphics;
- g.lineStyle(1,RED,1);
- g.moveTo(p1.x,p1.y);
- g.lineTo(p2.x,p2.y);
- }
- public static function computeEdges():void{
- if(!hypotenuse){
- getDist();
- }
- if(Math.abs(dx) > Math.abs(dy)){
- bigDist = p2.x - p1.x;
- smallDist = p2.y - p1.y;
- p3 = new Point(p2.x,p1.y);
- }else{
- bigDist = p2.y - p1.y;
- smallDist = p2.x - p1.x;
- p3 = new Point(p1.x,p2.y);
- }
- }
- public static function drawEdges():void{
- if(!targetObject){
- throw new Error(" You must first call setTarget(t:Sprite) before to call drawEdges()");
- }
- getDist();
- g = (targetObject as Sprite).graphics;
- if(Math.abs(dx) > Math.abs(dy)){
- // bigDistance
- bigDist = p2.x - p1.x;
- smallDist = p2.y - p1.y;
- g.lineStyle(2,GREEN,1);
- g.moveTo(p2.x,p2.y);
- g.lineTo(p2.x,p1.y);
- g.lineStyle(2,BLUE,1);
- g.moveTo(p1.x,p1.y);
- g.lineTo(p2.x,p1.y);
- }else{
- // smallDistance
- bigDist = p2.y - p1.y;
- smallDist = p2.x - p1.x;
- g.lineStyle(2,GREEN,1);
- g.moveTo(p1.x,p2.y);
- g.lineTo(p2.x,p2.y);
- g.lineStyle(2,BLUE,1);
- g.moveTo(p1.x,p1.y);
- g.lineTo(p1.x,p2.y);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement