azimuth degrees into Cardinal Point

So I am trying to find out if there is a json file somewhere that would have the degrees and allow me to use it to turn degrees into north and north west. I am still working on my weather app but keep running into bumps in the road, like I had to reset my computer and lost what I was working on lol I am just using JavaScript
3 Replies
althepal78
althepal78•3mo ago
BTW I don't have any code for it yet because I was trying to search for that but did not have the information for it yet 🙂 just trying to figure out if there was a json I could use that had like every 11 degrees or something that I can use first
MarkBoots
MarkBoots•3mo ago
you could just create your own function
function getDirection(degree) {
const directions = [
{ dir: "N", from: 0, to: 11.25 },
{ dir: "NNE", from: 11.25, to: 33.75 },
{ dir: "NE", from: 33.75, to: 56.25 },
{ dir: "ENE", from: 56.25, to: 78.75 },
{ dir: "E", from: 78.75, to: 101.25 },
{ dir: "ESE", from: 101.25, to: 123.75 },
{ dir: "SE", from: 123.75, to: 146.25 },
{ dir: "SSE", from: 146.25, to: 168.75 },
{ dir: "S", from: 168.75, to: 191.25 },
{ dir: "SSW", from: 191.25, to: 213.75 },
{ dir: "SW", from: 213.75, to: 236.25 },
{ dir: "WSW", from: 236.25, to: 258.75 },
{ dir: "W", from: 258.75, to: 281.25 },
{ dir: "WNW", from: 281.25, to: 303.75 },
{ dir: "NW", from: 303.75, to: 326.25 },
{ dir: "NNW", from: 326.25, to: 348.75 },
{ dir: "N", from: 348.75, to: 360 },
];

const direction = directions.find(dir => degree >= dir.from && degree < dir.to);
return direction ? direction.dir : "Unknown";
}

console.log(getDirection(10)) // N
console.log(getDirection(135)) // SE
console.log(getDirection(200)) // SSW
console.log(getDirection(318)) // NW
function getDirection(degree) {
const directions = [
{ dir: "N", from: 0, to: 11.25 },
{ dir: "NNE", from: 11.25, to: 33.75 },
{ dir: "NE", from: 33.75, to: 56.25 },
{ dir: "ENE", from: 56.25, to: 78.75 },
{ dir: "E", from: 78.75, to: 101.25 },
{ dir: "ESE", from: 101.25, to: 123.75 },
{ dir: "SE", from: 123.75, to: 146.25 },
{ dir: "SSE", from: 146.25, to: 168.75 },
{ dir: "S", from: 168.75, to: 191.25 },
{ dir: "SSW", from: 191.25, to: 213.75 },
{ dir: "SW", from: 213.75, to: 236.25 },
{ dir: "WSW", from: 236.25, to: 258.75 },
{ dir: "W", from: 258.75, to: 281.25 },
{ dir: "WNW", from: 281.25, to: 303.75 },
{ dir: "NW", from: 303.75, to: 326.25 },
{ dir: "NNW", from: 326.25, to: 348.75 },
{ dir: "N", from: 348.75, to: 360 },
];

const direction = directions.find(dir => degree >= dir.from && degree < dir.to);
return direction ? direction.dir : "Unknown";
}

console.log(getDirection(10)) // N
console.log(getDirection(135)) // SE
console.log(getDirection(200)) // SSW
console.log(getDirection(318)) // NW
we can even shorten it with a bit of maths
function getDirection(degree) {
const dirs = ["N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"];
const index = Math.round(degree / 22.5) % 16;
return dirs[index];
}

// Example usage:
console.log(getDirection(10)) // N
console.log(getDirection(135)) // SE
console.log(getDirection(200)) // SSW
console.log(getDirection(318)) // NW
function getDirection(degree) {
const dirs = ["N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"];
const index = Math.round(degree / 22.5) % 16;
return dirs[index];
}

// Example usage:
console.log(getDirection(10)) // N
console.log(getDirection(135)) // SE
console.log(getDirection(200)) // SSW
console.log(getDirection(318)) // NW
althepal78
althepal78•2mo ago
wow this is genius I am not good with math and never saw something like this. even the dir : nw from to thing is very confusing I would look deeper into when I have time in the morning I had to leave earlier and just got back