我正在尝试制作一个程序来计算地球上两点之间的距离以及方位角。然后,一旦我得到距离和方位角,我将不得不创建一个图形数据结构,该结构将使用 Dijkstra 算法来找到最短路径。
这是文本文件中的几行。每条线代表两个城市位置的坐标,它将找到两者之间的距离:
Cairo= 30:2:39 N 31:14:8 E |Cape Town= 33:55:29 S 18:25:26 E
Cape Town= 33:55:29 S 18:25:26 E |Cairo= 30:2:39 N 31:14:8 E
Cairo= 30:2:39 N 31:14:8 E |Lagos= 6:27:11 N 3:23:45 E
Lagos= 6:27:11 N 3:23:45 E |Cairo= 30:2:39 N 31:14:8 E
Lagos= 6:27:11 N 3:23:45 E |Cape Town= 33:55:29 S 18:25:26 E
Cape Town= 33:55:29 S 18:25:26 E |Lagos= 6:27:11 N 3:23:45 E
Lagos= 6:27:11 N 3:23:45 E |Birmingham= 52:20:10 N 1:53:25 E
Birmingham= 52:20:10 N 1:53:25 E |Lagos= 6:27:11 N 3:23:45 E
这是格式:
<lat> d:m:s <N|S>,
where d = degrees, m = minutes, s = seconds (max(d) == 90)
<lon> is d:m:s <E|W> (max(d) == 180)
分钟和秒重要吗?
这是我在本网站http://www.krysstal.com/sphertrig.html上使用余弦规则的距离函数:
void findDistance(float x1, float y1, float x2, float y2) {
float a,b,c;
float distance;
/*
if (latDir == 'W' or lonDir == 'S') {
//change to negative
}
*/
a = y2-y1;
b = 90-x1;
c = 90-x2;
printf("\na = %f b = %f c = %f",a,b,c);
//convert to radians for trig functions
a = a * DEG_TO_RAD;
b = b * DEG_TO_RAD;
c = c * DEG_TO_RAD;
printf("\na = %f b = %f c = %f",a,b,c);
distance = cos(b)*cos(c)+sin(b)*sin(c)*cos(a);
printf("\nCos(distance) in radians = %f",distance);
distance = acos(distance);
float distDegree = distance*RAD_TO_DEG;
printf("\nCos(distance) in degrees = %f",distDegree);
distance = EARTH_CIRCUM * distDegree/360;
printf("\ndistance = %f",distance);
//return distance;
}