Re: convert degrees to heading

From: Date: Fri, 16 Jul 2004 22:36:59 +0000
Subject: Re: convert degrees to heading
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-190769@lists.php.net to get a copy of this message
In article <56B29E34-D776-11D8-BB8A-0003931DAC94@renefournier.com>, 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.) Might need some corrections, but the idea should be clear: function degrees2compass($degrees) { $compass = array('N', 'NNW', 'NNE', 'NE', ...); $index = $heading_degrees / sizeof($compass); return $compass[$index]; } Or you could use a switch -- Tim Van Wassenhove <http://home.mysth.be/~timvw>

« previous php.general (#190769) next »