风玫瑰转换程度在基数点惠特PHP

时间:2017-06-02 12:31:24

标签: php

我做了一个脚本,以这种方式在PHP中转换基数点:

<?php
$deg = 0;
$cardinal = null;

$deg = str_replace(',','.',$argv[1]);
echo "Degree: ". $deg ."\t";

if(($deg>=0 && $deg <=  11.25) || ($deg > 348.75 && $deg <= 360) ){ $cardinal = 'N';}
elseif($deg > 11.25  && $deg <=  33.75  ){ $cardinal = 'NNE';}
elseif($deg > 33.75  && $deg <=  56.25  ){ $cardinal = 'NE';}
elseif($deg > 56.25  && $deg <=  78.75  ){ $cardinal = 'ENE';}
elseif($deg > 78.75  && $deg <=  101.25 ){ $cardinal = 'E';}
elseif($deg > 101.25 && $deg <=  123.75 ){ $cardinal = 'ESE';}
elseif($deg > 123.75 && $deg <=  146.25 ){ $cardinal = 'SE';}
elseif($deg > 146.25 && $deg <=  168.75 ){ $cardinal = 'SSE';}
elseif($deg > 168.75 && $deg <=  191.25 ){ $cardinal = 'S';}
elseif($deg > 191.25 && $deg <=  213.75 ){ $cardinal = 'SSW';}
elseif($deg > 213.75 && $deg <=  236.25 ){ $cardinal = 'SW';}
elseif($deg > 236.25 && $deg <=  258.75 ){ $cardinal = 'WSW';}
elseif($deg > 258.75 && $deg <=  281.25 ){ $cardinal = 'W';}
elseif($deg > 281.25 && $deg <=  303.75 ){ $cardinal = 'WNW';}
elseif($deg > 303.75 && $deg <=  326.25 ){ $cardinal = 'NW';}
elseif($deg > 326.25 && $deg <=  348.75 ){ $cardinal = 'NNW';}
else{ $cardinal = 'invalid';}

echo "Cardinal point: ". $cardinal ."\n";

所以我试过了:

`for i in $(seq 0 0.01 360);do php windrose.php $i;done`

这产生了想要的结果:

Degree: 0.00    Cardinal point: N
Degree: 0.01    Cardinal point: N
Degree: 0.02    Cardinal point: N
Degree: 0.03    Cardinal point: N
...
Degree: 11.24   Cardinal point: N
Degree: 11.25   Cardinal point: N
Degree: 11.26   Cardinal point: NNE
Degree: 11.27   Cardinal point: NNE
Degree: 11.28   Cardinal point: NNE
...
Degree: 33.74   Cardinal point: NNE
Degree: 33.75   Cardinal point: NNE
Degree: 33.76   Cardinal point: NE
Degree: 33.77   Cardinal point: NE

所以我想:“有可能做得更好吗?” 有一种最简单的方法吗?

1 个答案:

答案 0 :(得分:1)

不是我的代码从开始,但它在我使用的库中。

function windRose($item) {
     $winddir[]="N";
     $winddir[]="NNE";
     $winddir[]="NE";
     $winddir[]="ENE";
     $winddir[]="E";
     $winddir[]="ESE";
     $winddir[]="SE";
     $winddir[]="SSE";
     $winddir[]="S";
     $winddir[]="SSW";
     $winddir[]="SW";
     $winddir[]="WSW";
     $winddir[]="W";
     $winddir[]="WNW";
     $winddir[]="NW";
     $winddir[]="NNW";
     $winddir[]="N";
     return $winddir[round($item*16/360)];
}

echo windRose("220"); // returns SW

https://3v4l.org/X2JR9