Check whether an angle is between two other angles
Weirdly, I’ve had to do this a lot recently: given an angle, determine whether it’s bounded by two other angles. Technically, this is always true — just go the other direction around the circle — but usually I care about going in a particular direction.
Step one is to normalize all the angles so that they fit into the range [0, 2π]
(or [0, 360]
if you prefer degrees). We’ll use TypeScript to demonstrate:
const TWO_PI = Math.PI * 2;
export function normalize(angle: number) {
const clamped = angle % TWO_PI, // normalize into range [-2π, 2π]
positive = clamped + TWO_PI, // translate into range [0, 4π]
result = positive % TWO_PI; // normalize into range [0, 2π]
return result;
}
Three steps here:
- Modulo the angle by
2π
. If it’s positive, this compresses it into the range[0, 2π]
; f it’s negative, this compresses it into the range[-2π, 0]
. - Add
2π
. This increases the range to[0, 4π]
, ensuring it’s positive. - Modulo the angle by
2π
again, compressing it into the range[0, 2π]
.
Once all the angles are within the range [0, 2π]
, they can be checked:
export function between(min: number, theta: number, max: number) {
if (min < max) return min <= theta && theta < max;
else return min <= theta || theta < max;
}
- If
min
is less thanmax
, we can just ensure thattheta
is between them. - Otherwise, it means the angles wrap around
0
(for example,min
might be7π / 4
andmax
might beπ / 4
). In that case, we need to check whethertheta
is greater thanmin
(and implicitly less than2π
, which is the top of the range) or less thanmax
(and implicitly greater than0
, which is the bottom of the range).
This function assumes that min
is inclusive and max
is exclusive.