Re: RE: [PHP] convert degrees to heading
Hi Trevor, Very nice solution, Dave. I like simple, elegant and effective solutions like this. I was wracking my brain yesterday to come up with something like this, but my brain wasn't working. Hah. Good job! Thanks for the compliments - my head is now a bit bigger :-) I thought 0 degrees should have been east but thought you were using a southern hemisphere compass :-) Anyway, I went back and checked the original post :- I have to write a little function to convert a direction=20 from degrees to a compass -type heading. 0 =3D West. 90 =3D North. E.g.: =20 Something like this... it'll account for 360 degrees, too.=20 No different=20 that a bunch of IF statements, though... which is what I coded for - which only goes to prove that the system will only be as good as the specification :-) Regards, Dave php/trevor-2004-09-15.tx [EMAIL PROTECTED] php-general,[EMAIL PROTECTED] ++ | Dave Restall, IIRC Limited, PO Box 46, Skelton, Cleveland, TS12 2GT. | | Tel. 0845 10 80 151Mob. +44 (0) 7973 831245 Int. +44 (0) 1287 653003 | | email : [EMAIL PROTECTED] [EMAIL PROTECTED] Web : http://www.iirc.net | ++ | The last person that quit or was fired will be held responsible for| | everything that goes wrong -- until the next person quits or is fired. | ++ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] convert degrees to heading
Hi, Alternatively, try :- $Compass = array('West', 'North Westerly', 'North', 'North Easterly', 'East', 'South Easterly', 'South', 'South Westerly'); print $Compass[round($Degrees / 45)] . \n; This can be expanded easily by adding 'North North West' etc. at the relevant points in the array and changing the 45 to 22.5. No messy horrible switches :-) TTFN, Dave php/2004-09-14.tx [EMAIL PROTECTED], php-general,[EMAIL PROTECTED] ++ | Dave Restall, IIRC Limited, PO Box 46, Skelton, Cleveland, TS12 2GT. | | Tel. 0845 10 80 151Mob. +44 (0) 7973 831245 Int. +44 (0) 1287 653003 | | email : [EMAIL PROTECTED] [EMAIL PROTECTED] Web : http://www.iirc.net | ++ | One picture is worth more than ten thousand words. | | -- Chinese proverb | ++ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] convert degrees to heading
Hi, Alternatively, try :- $Compass = array('West', 'North Westerly', 'North', 'North Easterly', 'East', 'South Easterly', 'South', 'South Westerly'); print $Compass[round($Degrees / 45)] . \n; This can be expanded easily by adding 'North North West' etc. at the relevant points in the array and changing the 45 to 22.5. No messy horrible switches :-) Oh and I forgot to mention, it also deals fairly well with weird degrees such as 48.7. Dave php/2004-09-14.2.tx[EMAIL PROTECTED], php-general,[EMAIL PROTECTED] ++ | Dave Restall, IIRC Limited, PO Box 46, Skelton, Cleveland, TS12 2GT. | | Tel. 0845 10 80 151Mob. +44 (0) 7973 831245 Int. +44 (0) 1287 653003 | | email : [EMAIL PROTECTED] [EMAIL PROTECTED] Web : http://www.iirc.net | ++ | Azh nazg durbatal^uk, azh nazg gimbatul, Azh nazg thrakatal^uk agh | | burzum ishi krimpatul! | | -- J. R. R. Tolkien| ++ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] convert degrees to heading
On 13 September 2004 20:23, Gryffyn, Trevor wrote: Nice! I didn't know you could do that with switch. I was wondering why conditionals were left out of PHP's switch statement, but hoped someone would prove me wrong. Thanks John! Very helpful! -TG -Original Message- From: John Holmes [mailto:[EMAIL PROTECTED] Sent: Monday, September 13, 2004 3:26 PM To: Gryffyn, Trevor; php Cc: René Fournier Subject: Re: [PHP] convert degrees to heading I have to write a little function to convert a direction from degrees to a compass -type heading. 0 = West. 90 = North. E.g.: Something like this... it'll account for 360 degrees, too. No different that a bunch of IF statements, though... ?php $dir = 378; switch($dir = $dir%360) { case 0 = $dir $dir 90: echo 'northeasterly'; break; case 90 = $dir $dir 180: echo 'southeasterly'; break; case 180 = $dir $dir 270: echo 'southwesterly'; break; case 270 = $dir $dir 360: echo 'northwesterly'; break; } That will actually give the wrong answer for $dir==0, since then the switch value will equate to (bool)FALSE, which will fail to match case 0 = $dir $dir 90 because that evaluates to (bool)TRUE in this situation. I'm guessing you'll get 'southeasterly' as that's the first FALSE value, but theoretically you could get any one of the other values since all the other conditions evaluate to FALSE! If you're using this kind of condition-matching switch statement, it's absolutely necessary to use switch(TRUE) to make it clear exactly what's going on; so the above should be: $dir %= 360; switch (TRUE) { etc } Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Headingley Campus, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] convert degrees to heading
Very nice solution, Dave. I like simple, elegant and effective solutions like this. I was wracking my brain yesterday to come up with something like this, but my brain wasn't working. Hah. Good job! I think your compass directions are a bit off though.hah. West shouldn't be 0 degrees, it should be 270 normally (unless someone's using a compass system that I'm not familiar with). Here's a variation of your script that displays all 360 degrees (for validation purposes): $compass = array(N,NNE,NE,NEE,E,SEE,SE,SSE,S,SSW,SW,SWW,W ,NWW,NW,NNW); for ($degrees=0;$degrees360;$degrees++) { $compcount = round($degrees / 22.5); $compdir = $compass[$compcount]; echo $degrees degrees is roughly $compdir on the compassbr\n; } Yeah, I don't know what east of northeast is, so I just labeled it NEE :) Great thinking though. You still get the prize on this one. -TG -Original Message- From: Dave Restall - System Administrator,,, [mailto:[EMAIL PROTECTED] Sent: Tuesday, September 14, 2004 4:14 AM To: [EMAIL PROTECTED]; [EMAIL PROTECTED]; Gryffyn, Trevor Subject: Re: [PHP] convert degrees to heading Hi, Alternatively, try :- $Compass = array('West', 'North Westerly', 'North', 'North Easterly', 'East', 'South Easterly', 'South', 'South Westerly'); print $Compass[round($Degrees / 45)] . \n; This can be expanded easily by adding 'North North West' etc. at the relevant points in the array and changing the 45 to 22.5. No messy horrible switches :-) TTFN, Dave php/2004-09-14.tx [EMAIL PROTECTED], php-general,[EMAIL PROTECTED] +- ---+ | Dave Restall, IIRC Limited, PO Box 46, Skelton, Cleveland, TS12 2GT. | | Tel. 0845 10 80 151Mob. +44 (0) 7973 831245 Int. +44 (0) 1287 653003 | | email : [EMAIL PROTECTED] [EMAIL PROTECTED] Web : http://www.iirc.net | +--- -+ | One picture is worth more than ten thousand words. | | -- Chinese proverb | +--- -+ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] convert degrees to heading
The cleanest looking multiple if scenario is to use a Switch statement. Unfortunately I don't believe PHP's switch will do varied conditions, only equality statements: $j = 5; switch ($j) { case 6: echo first; break; case 6: echo second; break; case 5: echo third; break; default: echo fourth; break; } This breaks at case 6. Removing the second condition, the switch statement echos third since $j == 5. In some other languages, you could put your range of values in the case statements, but not PHP I guess. I think in this case, you're stuck with a bunch of if/elseif statements. Only 16 of them though, right? :) -TG -Original Message- From: René Fournier [mailto:[EMAIL PROTECTED] Sent: Friday, July 16, 2004 6:20 PM To: php Subject: [PHP] convert degrees to heading 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] convert degrees to heading
I have to write a little function to convert a direction from degrees to a compass -type heading. 0 = West. 90 = North. E.g.: Something like this... it'll account for 360 degrees, too. No different that a bunch of IF statements, though... ?php $dir = 378; switch($dir = $dir%360) { case 0 = $dir $dir 90: echo 'northeasterly'; break; case 90 = $dir $dir 180: echo 'southeasterly'; break; case 180 = $dir $dir 270: echo 'southwesterly'; break; case 270 = $dir $dir 360: echo 'northwesterly'; break; } ? ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] convert degrees to heading
Nice! I didn't know you could do that with switch. I was wondering why conditionals were left out of PHP's switch statement, but hoped someone would prove me wrong. Thanks John! Very helpful! -TG -Original Message- From: John Holmes [mailto:[EMAIL PROTECTED] Sent: Monday, September 13, 2004 3:26 PM To: Gryffyn, Trevor; php Cc: René Fournier Subject: Re: [PHP] convert degrees to heading I have to write a little function to convert a direction from degrees to a compass -type heading. 0 = West. 90 = North. E.g.: Something like this... it'll account for 360 degrees, too. No different that a bunch of IF statements, though... ?php $dir = 378; switch($dir = $dir%360) { case 0 = $dir $dir 90: echo 'northeasterly'; break; case 90 = $dir $dir 180: echo 'southeasterly'; break; case 180 = $dir $dir 270: echo 'southwesterly'; break; case 270 = $dir $dir 360: echo 'northwesterly'; break; } ? ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] convert degrees to heading
[snip] The cleanest looking multiple if scenario is to use a Switch statement. Unfortunately I don't believe PHP's switch will do varied conditions, only equality statements: $j = 5; switch ($j) { case 6: echo first; break; case 6: echo second; break; case 5: echo third; break; default: echo fourth; break; } [/snip] Hmmm, what about this...seems to work fine $j = 7; switch($j){ case $j 6: echo $j . is less than 6\n; break; } Alter the value of $j to see the results, the above example forgets to compare $j in each case. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] convert degrees to heading
On Tuesday 14 September 2004 02:18, Gryffyn, Trevor wrote: In some other languages, you could put your range of values in the case statements, but not PHP I guess. switch(true) { case (expr1) : //do something; break; case (expr2) : //do something else; break; } Season to taste. -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* Very few profundities can be expressed in less than 80 characters. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] convert degrees to heading
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