For example: angle 10 is cross 270 ~ 390 , because 270 to 390 is inclue 270 ~ 360 and 0 ~ 30.
InRange(0, 180, 90);//true;
InRange(180, 270, 90);//false;
InRange(270, 390, 10);//false; <== but this is in range , How to fix?
And my method:
public bool InRange(int start, int end, int angle)
{
if (angle >= start && angle <= end)
{
return true;
}
return false;
}
CodePudding user response:
Be careful!
The follwoging is not in range:
InRange(270, 390, 10);//false;
Because:
if (10 >= 270 && 10<= 390)
=> false
Because
10 is not bigger then 270
You should debug your program the next time, to find fast the bug.
CodePudding user response:
Be sure that range is normalized (390>270)
Find middle angle m = (270 390)/2
and half-angle h = (390-270)/2
Compare cosine of difference of needed angle a
and middle one m
with cosine of half-angle h
(convert degrees to radians if needed in your language). This approach helps to avoid problems with periodicity etc
if cos(a - m) >= cos(h)
{a inside the range}