Sign in to follow this  
Lollyn00b

Finding out if an angle is clockwise from another angle.

Recommended Posts

Hello, I'm making a prototype in Game Maker (Later to be done in C++) of a game idea I had, and I've run into trouble as far as seeing if an angle is clockwise from another angle. I'm trying to write a function to find the difference between two angles, regardless of the 0-360 crossover. I've gotten that part to work fine, but I'd also like to have the function return the angle difference with clockwise/counterclockwise included. For example, I'd like "functionname( 10, 350)" to return -20, but "functionname( 350, 10)" to return +20. Sorry if this is all a bit confusing, I'm kind of tired at the moment. Here's what I have so far (it finds the angle difference, but doesn't include the sign): var a1,a2,ret; a1=argument0; a2=argument1; ret=a2-a1; if (ret < 0){ ret=a1-a2; } if (ret > 180){ ret=360-ret; } return ret;

Share this post


Link to post
Share on other sites
Well the way you have it described, I would find the angle regardless then determine whether it's cw or ccw.

// I'm going to assume all arguments are < 360.
float angle = a2 - a1; // find the angle between the 2.

if(angle > 180)
{
angle = 360 - angle;
return -angle;
}
else if(angle < -180)
{
angle = 360 + angle;
return angle;
else
return -angle;


As a side note, angles are usually dealt with in radians ( PI = 3.14) in programming not degrees. And to find the orientation of 2 vectors (which most of the world uses) you would do the signed area of a triangle which is half the determinant (cross product). So:

vec2D vA, vB; // 2d coordinates ie x and y.

float area = 0.5 * CrossProduct(vA,vB);

if(area > 0) clockwise
else if(area < 0) ccw
else degenerate triangle ( 0 area ie straight line )

(I might have the sign check backwards, test it out)




Share this post


Link to post
Share on other sites
If you want to entirely get rid of if-statements, you could do something like this (assuming "mod" is the modulus operator in Game Maker; I don't really know):
return ((a1 - a2 + 540) mod 360) - 180;
Initially, (a1 - a2) can be anywhere from -360 to +360. However, you want to limit your range to [-180, +180] (actually, the code above is [-180, +179], since +180 is the same as -180). The modulus operator is really useful at taking a non-negative integer, and making sure it is in the range [0, N - 1]. We want our range to start at -180, though, not 0. So to do that, we would add 180 to our original value, do the modulus, and then subtract 180 again. But adding 180 to our original number won't always produce a non-negative number (modulus might act differently in different languages or platforms when working on negative numbers). But if we add an extra 360 to it (which doesn't change the actual value of the angle), we can guarantee that it is always a positive value, and thus we get a total of adding 540. The "mod 360" ensures that the value is in the range [0, 359]. Then we just subtract 180 to get it into the final range [-180, +179].

Disclaimer: I did a few tests on paper, but didn't actually test it out more thoroughly than that. Regardless, this can be done without conditional statements, even if my code above has a few problems.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this