Here's a quick script I wrote for it:
<?php

$deg = $_GET['deg']; // get from URL, for testing atm

$deg %= 360; // make sure it doesn't break on eg 560 degrees

$deg = ((ceil(($deg-22.5)/45))*45+22.5); // break it up into parts we know, there might be an easier way though, just too tired to see it.

switch($deg) {
    case 22.5:
        echo 'W';
        break;

    case 67.5:
        echo 'NW';
        break;

    case 112.5:
        echo 'N';
        break;

    case 157.5:
        echo 'NE';
        break;

    case 202.5:
        echo 'E';
        break;

    case 247.5:
        echo 'SE';
        break;

    case 292.5:
        echo 'S';
        break;

    case 337.5:
        echo 'SW';
        break;

    default:
        echo 'W';
        break;
}
?>
René fournier wrote:

I have to write a little function to convert a direction from degrees to a compass -type heading. 0 = West. 90 = North. E.g.:

from:
135 degrees

to:
NW

Now, I was planning to write a series of if statements to evaluate e.g.,

if ($heading_degrees < 112.5 && $heading_degrees > 67.5) {
    $heading_compass = "N";
    }

The works, but it will require

N, NNW, NNE, NE, NW, ENE, NWW... many IF statements.

Can anyone think of a programatically more elegant and efficient way of converting this type of data? (I suppose this is not really a problem, just a curiosity.)

...Rene

-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to