Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-29-2011, 10:32 AM   #1
OmerHassan
Senior Member
 
Join Date: Mar 2009
Location: Islamabad, Pakistan
Posts: 207
Send a message via Skype™ to OmerHassan
Default Find bearing without using conditionals

I want to calculate the angle a line makes with the positive x-axis in a clockwise direction. It's a lot like a bearing except instead of North, I want the angle it makes with positive x-axis. The image illustrates what I'm after.



Below is the code I wrote to achieve this. It works fine but I am just wondering if there's a way to reduce all these if statements.

ActionScript Code:
/**  * @return Bearing, in radians, of <code>p2</code> from <code>p1</code>  */ public static function bearing(p1:Point, p2:Point):Number {     var dy:Number = p2.y - p1.y;     var dx:Number = p2.x - p1.x;     var theta:Number = Math.atan(dy/dx);         if (theta < 0)         if (p2.y > p1.y)             theta += Math.PI;         else             theta += MathHelper._2_PI;     else if (p2.y < p1.y)         theta += Math.PI;         return theta; }

Edit: MathHelper._2_PI = 2 * Math.PI

Last edited by OmerHassan; 11-29-2011 at 10:39 AM.
OmerHassan is offline   Reply With Quote
Old 11-29-2011, 11:24 AM   #2
henke37
Senior Member
 
henke37's Avatar
 
Join Date: Mar 2009
Location: Sweden
Posts: 10,679
Send a message via Skype™ to henke37
Default

Have you tried the atan2 function?
__________________
Signature: I wrote a pair of articles about the timeline.
henke37 is offline   Reply With Quote
Old 11-29-2011, 11:31 AM   #3
OmerHassan
Senior Member
 
Join Date: Mar 2009
Location: Islamabad, Pakistan
Posts: 207
Send a message via Skype™ to OmerHassan
Default

atan2 looks like what I am after. Thanks, henke!
OmerHassan is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:37 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.