## Recommended Posts

Hello All,

I just had a need to convert azimuths to a compass directions so made this little function so I'd end up with a one liner in my script. It's not fancy by any means but does the job. It returns a string containing "North" or "South" or "South South East" etc...

I know it can be more efficient...as it is it has to check every "if". I have a fancier algorithm somewhere to do this I think...but this was quick and easy to do with a calculator and serves my purpose. If you don't like it...don't use it!!

Setup the function in your init.sqf like this (in this example it is in a folder called "func") :-

`az2cardinal = compile PreProcessfile "func\fn_az2cardinal.sqf";`

Call the function like this:-

`_compdir = [_ang] call az2cardinal;`

Examples:-

For _ang = 175.5 degrees then _compdir would be the string "South"

For _ang = 33.0 degrees then _compdir would be the string "North North East"

For _ang = 310.75 degrees then _compdir would be the string "North West" and so on...

The function itself:-

"fn_az2cardinal.sqf"

```//***********************************************
//** Function "fn_az2cardinal.sqf".
//** Twirly 
//***********************************************

private ["_ang","_compass"];

_ang = _this select 0;

if (_ang>348.75 and _ang<=360 or _ang >=0 and _ang <11.25) then {_compass = "North"};
if (_ang>11.25 and _ang<=33.75) then {_compass = "North North East"};
if (_ang>33.75 and _ang<=56.55) then {_compass = "North East"};
if (_ang>56.55 and _ang<=78.75) then {_compass = "East North East"};
if (_ang>78.75 and _ang<=101.25) then {_compass = "East"};
if (_ang>101.25 and _ang<=123.75) then {_compass = "East South East"};
if (_ang>123.75 and _ang<=146.25) then {_compass = "South East"};
if (_ang>146.25 and _ang<=168.75) then {_compass = "South South East"};
if (_ang>168.75 and _ang<=191.25) then {_compass = "South"};
if (_ang>191.25 and _ang<=213.75) then {_compass = "South South West"};
if (_ang>213.75 and _ang<=236.25) then {_compass = "South West"};
if (_ang>236.25 and _ang<=258.75) then {_compass = "West South West"};
if (_ang>258.75 and _ang<=281.25) then {_compass = "West"};
if (_ang>281.25 and _ang<=303.75) then {_compass = "West North West"};
if (_ang>303.75 and _ang<=326.25) then {_compass = "North West"};
if (_ang>326.25 and _ang<=348.75) then {_compass = "North North West"};

_compass```

If you have any problems please let me know.

Twirly.

Edited by twirly

##### Share on other sites

Nice one! I was thinking of something like this for an enemy spawning script, so that enemies could be announced eg ' insurgents spotted NNW 1200m'. You've saved me some brainpower.

##### Share on other sites

I had a bit more of a think and came up with a hopefully more efficient way to do it:

```private ["_ang","_compass"];
_ang = _this select 0;
_ang = _ang + 11.25;
if (_ang > 360) then {_ang = _ang - 360};
_points = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"];
_num = floor (_ang / 22.5);
_compass = _points select _num;
_compass```

I'm not in front of my arma box, so can't confirm that it'll work though...

Edited by tpw

##### Share on other sites
I had a bit more of a think and came up with a hopefully more efficient way to do it:

```private ["_ang","_compass"];
_ang = _this select 0;
_ang = _ang + 11.25;
if (_ang > 360) then {_ang = _ang - 360};
_points = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"];
_num = floor (_ang /[color="Red"] 22.5[/color]);
_compass = _points select _num;
_compass```

I'm not in front of my arma box, so can't confirm that it'll work though...

Works sweet man......simply replaced my clunky code with this and it worked great.....except for the little typo with the underscore in the 22.5!

Good one. Nice elegant solution.

##### Share on other sites

Cheers bro. You must have caught it before I put the edit in.