I am representing wind directions using integer values (an Enum) ranging from 0 for North, through to 15 for North-North-West.
I need to check if a given wind direction (integer value between 0 and 15) is within a certain range. I specify my WindDirectionFrom value first moving clockwise to WindDirectionTo to specify the range of allowable wind direction.
Obviously if WindDirectionFrom=0 and WindDirectionTo=4 (between N and E direction) and the wind direction is NE (2) the calculation is simply
int currentWindDirection = 2;
bool inRange = (WindDirectionFrom <= currentWindDirection && currentWindDirection <= WindDirectionTo);
//(0 <= 2 && 2 <= 4) simple enough...
However for a different case where say WindDirectionFrom=15, WindDirectionTo=4 and wind direction is NE (2) again, the calculation immediately breaks…
bool inRange = (WindDirectionFrom <= currentWindDirection && currentWindDirection <= WindDirectionTo);
//(15 <= 2 && 2 <= 4) oops :(
I’m sure this can’t be too difficult, but I’m having a real mental block with this one.
I would do it like this: