Home Tutorials Forums Articles Blogs Movies Library Employment Press

 ActionScript.org Forums Find bearing without using conditionals
 Register FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

 11-29-2011, 10:32 AM #1 OmerHassan Senior Member   Join Date: Mar 2009 Location: Islamabad, Pakistan Posts: 207 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 p2 from p1  */ 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.
 11-29-2011, 11:24 AM #2 henke37 Senior Member     Join Date: Mar 2009 Location: Sweden Posts: 10,833 Have you tried the atan2 function? __________________ Signature: I wrote a pair of articles about the timeline.
 11-29-2011, 11:31 AM #3 OmerHassan Senior Member   Join Date: Mar 2009 Location: Islamabad, Pakistan Posts: 207 atan2 looks like what I am after. Thanks, henke!

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home ActionScript Forums Group     ActionScript 3.0     ActionScript 2.0     ActionScript 1.0 (and below)     Simple Stuff (Newbies) Flash General Questions     Flash 10 General Questions     Flash 9 General Questions     Flash 8 General Questions     Other Flash General Questions Flex     Flex 2, 3 & 4     Flex 1 Extensions and Plugins     Components     JSFL - Extending Flash Desktop, Mobile and non-browser Environments     AIR (Apollo)     FlashLite / Portable Devices Development     Projectors and CDs Supporting Technologies     HTML and JavaScript     haXe     Server-Side Scripting     Flash Remoting     Flash Media Server General     Best Practices     Gaming and Game Development     Animation and Effects     Flashants Support Forum Community Boards     General Chat     Just for Kicks Challenges     Detention Flash In Action     Site Check     Cool Sites     Widgets Decommissioned     Projects and Positions CMS Forums     Announcements Board     Content Postings / Updates     Product Review Requests     CMS Technical Questions     Process Questions     Collaboration & Suggested Articles

All times are GMT. The time now is 12:56 AM.

///