地球上の 2 点間の距離と方位角を計算するプログラムを作成しようとしています。次に、距離と方位角を取得したら、ダイクストラのアルゴリズムを使用して最短経路を見つけるグラフ データ構造を作成する必要があります。
テキスト ファイルの数行を次に示します。各行は、2 つの都市の位置の座標を表し、2 つの都市間の距離を見つけます。
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)
分と秒は重要ですか?
これは、この Web サイト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;
}