Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #365 Convert an angle to a direction

Convert a degree, x, of 360, to a Compass direction, y.

For example, 123.4 deg is "South-east".

enum Point {
    N, NE, E, SE, S, SW, W, NW;
    static Point parse(double x) {
        int i = (int) ((x / 45) + .5);
        return values()[i % 8];
    }
}
String y = Point.parse(x).name();

Note, add 0.5 since North also accounts for x > 337.5.

New implementation...
< >
reilas