gpt4 book ai didi

c - Haversine 方位角计算的结果不准确

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:07:31 26 4
gpt4 key购买 nike

我正在尝试实现 Haversine我正在编写的一个小 GPS 程序中的公式。距离计算似乎是正确的。但是,我认为方位是以弧度计算的,我不知道如何将结果正确转换为罗盘方向(0 表示北,90 表示东,等等)。

任何帮助将不胜感激,所有这些关于余弦和反正切的讨论让我很头疼!我是程序员,不是数学家!

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void FindDistance (double latHome, double lonHome, double latDest, double lonDest)
{
double pi=3.141592653589793;
int R=6371; //Radius of the Earth in kilometers
//Keep the parameters passed to the function immutable
double latHomeTmp=(pi/180)*(latHome);
double latDestTmp=(pi/180)*(latDest);
double differenceLon= (pi/180)*(lonDest- lonHome);
double differenceLat=(pi/180)*(latDest- latHome);
double a= sin (differenceLat/2.)*sin (differenceLat/2.)+cos (latHomeTmp)*cos (latDestTmp)*sin (differenceLon/2.)*sin (differenceLon/2.);
double c=2*atan2 (sqrt (a), sqrt (1-a));
double Distance=R*c;
printf ("Distance is %f\n", Distance);

double RadBearing=atan2 (sin (differenceLon)*cos (latDestTmp), cos (latHomeTmp)*sin (latDestTmp)-sin (latHomeTmp)*cos (latDestTmp)*cos (differenceLon));
double DegBearing=RadBearing*57.2958;
if (DegBearing<0) DegBearing=360+DegBearing;
printf ("Bearing is %f\n", DegBearing);
} //Function FindDistance

int main (void) {
puts ("LA to NY");
FindDistance (34.052235, -118.243683, 40.748817, -73.985428);
puts ("NY to LA");
FindDistance (40.748817, -73.985428, 34.052235, -118.243683);
} //Function main

gcc -o gps -lm gps.c

它返回从 LA 到 NY 的方位角 65,以及从 NY 到 LA 的方位角 273。

If we add the bearings together, we get 338 which can't be right - shouldn't it equal 360?
Or am I completely out to lunch?

无论如何,如您所见,我总是同时计算距离和方位。如果您还可以建议一种清理代码的方法,使其不执行不必要的计算,那将非常出色!我在一个小型微处理器上运行它,我喜欢在其中计算每个周期!

最佳答案

没问题。

考虑两个纬度相同但经度不同的地点。在缩短大圆弧路线时,一个不在另一个的正东 (90)。第一个到期的也不是(西 270)。轴承不一定是互补的。

FindDistance(34.0, -118.0, 34.0, -73.0);
FindDistance(34.0, -73.0, 34.0, -118.0);

Distance is 4113.598081
Bearing is 76.958824
Distance is 4113.598081
Bearing is 283.041176

@user3386109增加更多好资料。

根据 site建议 @M Oehm你的代码是正确的。


根据 OP 的要求,一些模组的速度可能会略有提高。

void FindDistance(double latHome, double lonHome, double latDest,
double lonDest) {
// A few extra digits sometimes is worth it - rare is adding more digits a problem
// double pi=3.141592653589793;
static const double pi_d180 = 3.1415926535897932384626433832795 / 180;
static const double d180_pi = 180 / 3.1415926535897932384626433832795;

// int R=6371; //Radius of the Earth in kilometers
// (Compiler may do this all ready)
static const double R = 6371.0; // better to make FP to avoid the need to convert

//Keep the parameters passed to the function immutable
double latHomeTmp = pi_d180 * (latHome);
double latDestTmp = pi_d180 * (latDest);
double differenceLon = pi_d180 * (lonDest - lonHome);
double differenceLat = pi_d180 * (latDest - latHome);

double a = sin(differenceLat / 2.) * sin(differenceLat / 2.)
+ cos(latHomeTmp) * cos(latDestTmp) * sin(differenceLon / 2.)
* sin(differenceLon / 2.);

double c = 2 * atan2(sqrt(a), sqrt(1 - a));
double Distance = R * c;
printf("Distance is %f\n", Distance);

double RadBearing = atan2(sin(differenceLon) * cos(latDestTmp),
cos(latHomeTmp) * sin(latDestTmp)
- sin(latHomeTmp) * cos(latDestTmp) * cos(differenceLon));

// double DegBearing = RadBearing * 57.2958;
double DegBearing = RadBearing * d180_pi;

// Why is this even needed?
if (DegBearing < 0) DegBearing = 360 + DegBearing;

printf("Bearing is %f\n", DegBearing);
} //Function FindDistance

关于c - Haversine 方位角计算的结果不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36317018/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com