Skip to content

Instantly share code, notes, and snippets.

@caubry
Last active December 13, 2015 17:09
Show Gist options
  • Save caubry/4945827 to your computer and use it in GitHub Desktop.
Save caubry/4945827 to your computer and use it in GitHub Desktop.
private function calculateAngle( xPos:Number, yPos:Number ):Number
{
var pA:Point = new Point( 240, 180 );
var pB:Point = new Point( xPos, yPos );
var AP:Point = new Point( 0, 10 );
var BP:Point = new Point( ( pA.x - pB.x ), ( pA.y - pB.y ) );
var D:Number = ( AP.x * BP.x ) + ( AP.y * BP.y );
var A:Number = Math.sqrt( Math.pow( AP.x, 2 ) + Math.pow( AP.y, 2 ) );
var B:Number = Math.sqrt( Math.pow( BP.x, 2 ) + Math.pow( BP.y, 2 ) );
var angle:Number = Math.acos( D / ( A * B ) ) * 180 / Math.PI;
if ( BP.x > 0 )
{
angle *= -1;
}
return angle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment