从一个坐标到另一个坐标 [英] Bearing from one coordinate to another

查看:180
本文介绍了从一个坐标到另一个坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施了 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;
}


推荐答案

code()在错误的地方。

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.

您有:

You have:

 Math.toDegrees( (Math.atan2(y, x))+360 ) % 360;

但您需要:

But you need:

( 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.

这篇关于从一个坐标到另一个坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆