I am attempting to write a function that will tell me if an angle lies within 2 other angles. When say ‘if an angle lies within 2 other angles’ I mean for example, if I have the 2 angles 0 and 90 then 45 would lie between those angles but -20(or 99) would not.
My Problem: My function doesn’t seem to be detecting when 2 angles lie within 2 angles when it should. I’m not sure if my function works for negative angles aswell?
What do I need to change to get my function working correctly?
bool is_angle_between(int target, int angle1, int angle2)
{
// Post: Return true if target lies between the 2 angles
int iTarget = (360 + (target % 360)) % 360;
int iAngle1 = (3600000 + angle1) % 360;
int iAngle2 = (3600000 + angle2) % 360;
if (iAngle1 < iAngle2)
if (iAngle1 <= iTarget && iTarget <= iAngle2)
return true;
else if (iAngle1 <= iTarget || iTarget <= iAngle2)
return true;
return false;
}
Seems like this should work:
Note that the braces here help ensure that the else matches the if you intend it to. Your original code is indented as you want it to match, but that isn’t how it’s parsed.