問題描述
我從 http://www.movable-type.co.uk 實現了軸承"公式/scripts/latlong.html.但這似乎非常不準確 - 我懷疑我的實施中有一些錯誤.你能幫我找到它嗎?我的代碼如下:
I implemented the "bearing" formula from http://www.movable-type.co.uk/scripts/latlong.html. But it seems highly inaccurate - I suspect some mistakes in my implementation. Could you help me with finding it? My code is below:
protected static double bearing(double lat1, double lon1, double lat2, double lon2){
double longDiff= lon2-lon1;
double y = Math.sin(longDiff)*Math.cos(lat2);
double x = Math.cos(lat1)*Math.sin(lat2)-Math.sin(lat1)*Math.cos(lat2)*Math.cos(longDiff);
return Math.toDegrees((Math.atan2(y, x))+360)%360;
}
推薦答案
你只是把括號 ()
放錯地方了.
You just have your parentheses ()
in the wrong place.
您正在為弧度值添加度數,這不起作用.toDegrees()
將為您完成從弧度到度數的轉換,然后一旦您有度數的值,您就可以進行標準化.
You are adding degrees to a value in radians, which won't work. toDegrees()
will do the conversion from radians to degrees for you, then you do the normalisation once you have a value in degrees.
你有:
Math.toDegrees( (Math.atan2(y, x))+360 ) % 360;
但你需要:
( Math.toDegrees(Math.atan2(y, x)) + 360 ) % 360;
還請記住,Math.sin()
、Math.cos()
和所有其他三角函數的所有輸入都必須以弧度表示.如果您的輸入是度數,您需要先使用 Math.toRadians()
進行轉換.
Remember also that all inputs to Math.sin()
, Math.cos()
and all the other trigonometric functions must be in radians. If your inputs are degrees you'll need to convert them using Math.toRadians()
first.
這篇關于從一個坐標到另一個坐標的方位角的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!