# Is there a function to convert compass direction to cardinal direction?

## Recommended Posts

Just like the title reads, is there a way to convert direction in degrees to cardinal ( north, south, north east, etc...).

Or do you need to create it from scratch?

Thank you.

##### Share on other sites

Thank you, I'll give it a try

##### Share on other sites

Try this one:

```PC_fn_compass_direction =
{
params ["_direction"];
private ["_compass_direction"];

_compass_direction = "";

switch (true) do
{
case (round ((_direction)/22.5) == 0): { _compass_direction = "north" };
case (round ((_direction)/22.5) == 1): { _compass_direction = "north-north-east" };
case (round ((_direction)/22.5) == 2): { _compass_direction = "north-east" }; // and so one
case (round ((_direction)/22.5) == 3): { _compass_direction = localize "STR_PC_groupchat_153" };
case (round ((_direction)/22.5) == 4): { _compass_direction = localize "STR_PC_groupchat_154" };
case (round ((_direction)/22.5) == 5): { _compass_direction = localize "STR_PC_groupchat_155" };
case (round ((_direction)/22.5) == 6): { _compass_direction = localize "STR_PC_groupchat_156" };
case (round ((_direction)/22.5) == 7): { _compass_direction = localize "STR_PC_groupchat_157" };
case (round ((_direction)/22.5) == 8): { _compass_direction = localize "STR_PC_groupchat_158" };
case (round ((_direction)/22.5) == 9): { _compass_direction = localize "STR_PC_groupchat_159" };
case (round ((_direction)/22.5) == 10): { _compass_direction = localize "STR_PC_groupchat_160" };
case (round ((_direction)/22.5) == 11): { _compass_direction = localize "STR_PC_groupchat_161" };
case (round ((_direction)/22.5) == 12): { _compass_direction = localize "STR_PC_groupchat_162" };
case (round ((_direction)/22.5) == 13): { _compass_direction = localize "STR_PC_groupchat_163" };
case (round ((_direction)/22.5) == 14): { _compass_direction = localize "STR_PC_groupchat_164" };
case (round ((_direction)/22.5) == 15): { _compass_direction = localize "STR_PC_groupchat_165" };
case (round ((_direction)/22.5) == 16): { _compass_direction = localize "STR_PC_groupchat_166" };
};
_compass_direction;
};```

call it like this:

`[_dir] call PC_fn_compass_direction; // _dir = azimuth;`

Change localized strings by yourself according to degrees like you see in the first three cases.

##### Share on other sites
```PC_fn_compass_direction =
{
params ["_direction"];

private _sector = round (_direction / 22.5);

[
"north",
"north-north-east",
"north-east",
localize "STR_PC_groupchat_153",
localize "STR_PC_groupchat_154",
localize "STR_PC_groupchat_155",
localize "STR_PC_groupchat_156",
localize "STR_PC_groupchat_157",
localize "STR_PC_groupchat_158",
localize "STR_PC_groupchat_159",
localize "STR_PC_groupchat_160",
localize "STR_PC_groupchat_161",
localize "STR_PC_groupchat_162",
localize "STR_PC_groupchat_163",
localize "STR_PC_groupchat_164",
localize "STR_PC_groupchat_165",
localize "STR_PC_groupchat_166"
] select _sector;
};```

Edited by R3vo
fixed return value
• 1

##### Share on other sites
```cardinaldirs = ["north", "northeast", "east", "southeast", "south", "southwest", "west", "northwest", "north"];

tky_fnc_cardinaldirection =
{
params ["_dir"];
private ["_cardinaldir"];
_cardinaldir = cardinaldirs # (([_dir, 45] call BIS_fnc_roundDir) /45);
_cardinaldir
};```

• 1