Determine if an angle is nearly upright

Viewed 21

I have a significant number of objects for which I have calculated their "tilt". I want to process those objects that are close to being upright (a tilt near 0, 90, 180, or 270 degrees). I have the following brute-force code that works fine:

angle = Math.Abs(angle);
var tooTilted = (angle > 000 + maxTilt && angle < 090 - maxTilt)
             || (angle > 090 + maxTilt && angle < 180 - maxTilt)
             || (angle > 180 + maxTilt && angle < 270 - maxTilt)
             || (angle > 270 + maxTilt && angle < 360 - maxTilt);

I have this itch in the back of my head making me think there's a geometric or trigonometric trick I could be using to perform the same test more succinctly.

1 Answers

angle % 90. Then you only need your first test case. % (mod) takes the remainder after being divided by 90.

angle = Math.Abs(angle) % 90;
var tooTilted = (angle > 000 + maxTilt && angle < 090 - maxTilt)
Related