= 2*pi() ){ $theta -= 2*pi(); } while( $theta < 0 ){ $theta += 2*pi(); } return $theta; } // 2点の方位角を方角の名称に変換。 // 引数はラジアンで,0~2π。北が0で時計回りに角度が増える。 // @author:rwanda_go_tan function translate_azimuth_to_direction_name( $azimuth_rad ) { // 方角リスト $direction_names = [ "北", "北東", "東", "南東", "南", "南西", "西", "北西", "北" // π/16 だけずらす分を,この末尾要素で回収する ]; // NOTE: 方位を8分割している。 // つまり,- π/16 ~ + π/16 を「北」とみなす。 // 方位角を配列のインデックスに変換 $direction_index = floor( ( // - π/16 を北とみなすために ( $azimuth_rad + ( pi() ) / 16.0 ) / ( 2 * pi() ) ) * 8.0 // 2πの回転を,全方位の個数のスキャンに対応させる ); // 方角の名称 $direction_name = $direction_names[ $direction_index ]; return $direction_name; } // ---------- サンプル座標データ ---------- // ハワイ語たん // https://gogakutan.jishotter.info/detail/hawaii_tan $hawaigotan = [ "lat" => 19.8967662, "lng" => -155.5827818 ]; // パプア諸語たん // https://gogakutan.jishotter.info/detail/papuan_tan $paputan = [ "lat" => -6.314993, "lng" => 143.95555 ]; // ---------- 動作デモ ---------- // ヒュベニの公式で最短距離を出す $distance_hybany = get_hubeny_distance( $hawaigotan["lat"], $hawaigotan["lng"], $paputan["lat"], $paputan["lng"] ); echo "最短距離 = " . ( $distance_hybany / 1000.0 ) . " km.


"; // 2点の方位角を出す $azimuth_rad = get_azimuth_of_two_points( $hawaigotan["lat"], $hawaigotan["lng"], $paputan["lat"], $paputan["lng"] ); echo "方位角 = " . $azimuth_rad . " rad.


"; // 2点の方角名称を出す $direction_name = translate_azimuth_to_direction_name( $azimuth_rad ); echo "方位名称 = " . $direction_name . " .


"; /* 上記サンプルの出力: 最短距離 = 7285.1919373465 km. 方位角 = 4.4095462906492 rad. 方位名称 = 南西 . */ ?>