Skip to content

Instantly share code, notes, and snippets.

@kraskden
Created March 25, 2019 13:51
Show Gist options
  • Save kraskden/6c1762dd991bd220e887014128e284b0 to your computer and use it in GitHub Desktop.
Save kraskden/6c1762dd991bd220e887014128e284b0 to your computer and use it in GitHub Desktop.
Returns the array of 32 compass points and heading.
/**
* Returns the array of 32 compass points and heading.
* See details here:
* https://en.wikipedia.org/wiki/Points_of_the_compass#32_cardinal_points
*
* @return {array}
*
* Example of return :
* [
* { abbreviation : 'N', azimuth : 0.00 ,
* { abbreviation : 'NbE', azimuth : 11.25 },
* { abbreviation : 'NNE', azimuth : 22.50 },
* ...
* { abbreviation : 'NbW', azimuth : 348.75 }
* ]
*/
function Point(abbr, azimuth) {
this.abbreviation = abbr;
this.azimuth = azimuth;
}
function createCompassPoints() {
let res = new Array(33)
var sides = ['N', 'E', 'S', 'W']
sides.forEach((val, idx) => res[idx * 8] = val)
function rec_travel(start_str, finish_str, start_pos, finish_pos) {
let medium_pos = (finish_pos + start_pos) / 2;
if (!Number.isInteger(medium_pos))
return;
let medium_str = start_str + finish_str;
if (medium_str.length > 3) {
let main_idx = (finish_pos - start_pos > 0 ? Math.ceil : Math.trunc)(medium_pos / 8) % 4;
medium_str = start_str + 'b' + sides[main_idx];
}
if (!res[medium_pos]) {
res[medium_pos] = medium_str;
}
rec_travel(start_str, res[medium_pos], start_pos, medium_pos)
rec_travel(finish_str, res[medium_pos], finish_pos, medium_pos)
}
rec_travel('N', 'N', 0, 32);
return res.map((abbr, idx) => new Point(abbr, 11.25 * idx));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment