A math problem.

Oct 20, 2009 14:33

Given two arbitrary angles (between 0 and 360) how do you determine whether the shortest distance is clockwise or counter-clockwise?

Leave a comment

Comments 6

skyesurfer October 20 2009, 21:53:37 UTC
*Tilts head* from your phrasing, I'm not entirely sure I understand your question. But based on my interpretation of it:

If you are facing angle1 and need to turn to angle2, then assuming the angle increases as you go clockwise... [Pardon my half-assed pseudo-code]

if (angle1 > angle2){
if angle1 - angle2 < 180, turn counterclockwise
else clockwise
}

if (angle2 > angle1){
if angle2 - angle1 > 180, turn counterclockwise
else clockwise
}

There's probably a way to simplify that but I'm lazy, and I'm sure you get the gist lol.

Reply

keless October 20 2009, 22:04:59 UTC
That works. I also ended up with another result that uses less "if" statements:

float delta = A2 - A1;
if( delta > 180.0f ) {
//clockwise
delta = 360.0f - delta;
}else {
//counter clockwise
}

Reply


skyesurfer February 11 2010, 05:12:56 UTC
.....you always find the best stuff. XD

Reply

keless February 11 2010, 21:26:00 UTC
I'm working on a project where I convert japanese flash games that only require 1 button to play into iphone games.

There are two crane machine-type games.

Reply


Leave a comment

Up