# Getting the compass point given longitude, latitude and direction in degrees

This is one solution to your problem (though your example is wrong according to wikipedia, as @bennos points out).

package com.ianturton;

import java.util.ArrayList;

/**
* given a direction in degrees (from North) return the Compass Point on a 32
* element compass rose.
*
* @author ian
*/

public class CompassRose {
private static final int SIZE = 32;
static final float STEP = 360f / SIZE;
static final float HALF_STEP = STEP / 2.0f;

static final String points[] = { "N", "NbE", "NNE", "NEbN", "NE", "NEbE", "ENE", "EbN", "E", "EbS", "ESE", "SEbE",
"SE", "SEbS", "SSE", "SbE", "S", "SbW", "SSW", "SWbS", "SW", "SWbW", "WSW", "WbS", "W", "WbN", "WNW", "NWbW",
"NW", "NWbN", "NNW", "NbW" };
static ArrayList<Direction> directions = new ArrayList<>();

static {
int k = 0;
for (float i = 0; i < 360; i += (360.0 / SIZE), k++) {
Direction d = new Direction(points[k], i);
}
}

public static String getDirection(float heading) {
while (heading < 0) {
}
while (heading > 360) {
}
for (Direction d : directions) {
return d.name;
}
}
}

public static void main(String[] args) {

for (int i = 0; i < 360; i += 10) {
System.out.println(i + " " + CompassRose.getDirection(i));
}
}

static public class Direction {
private float start;
private float end;
private float centre;
final String name;

Direction(String name, float centre) {
this.name = name;
this.centre = centre;
start = this.centre - HALF_STEP;
end = this.centre + HALF_STEP;
}

public boolean contains(float degree) {
return (start < degree && end > degree);
}
}

}