これらの座標を入力すると間違った出力が得られます。関数が間違った値を出力します
static double ReadCoordinateFromConsole(double lat1, double lon1, double
lat2, double lon2)
{
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2 - lat1);
var dLon = deg2rad(lon2 - lon1);
var a =
Math.Sin(dLat/2) * Math.Sin(dLat/2) +
Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) *
Math.Sin(dLon/2) * Math.Sin(dLon/2);
var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
var d = R * c; // Distance in km
return d;
}
static double deg2rad(double deg)
{
return deg * (Math.PI/180);
}
次に、座標を入力する関数です。 41.507483 -99.43655438.504048 -98.315949。これらの座標は約347と等しいはずですが、代わりに出力が7022,88になっていますが、これは間違っており、理由は何も分かりません。
static double ReadDoubleFromConsole(string msg)
while (true)
{
Console.Write(msg);
string test = Console.ReadLine();
string[] words = test.Split(' ');
bool inputContainsNumber = Regex.IsMatch(words[0], @"^-*[0-9,\.]+$");
bool inputContainsNumber2 = Regex.IsMatch(words[1], @"^-*[0-9,\.]+$");
bool inputContainsNumber3 = Regex.IsMatch(words[2], @"^-*[0-9,\.]+$");
bool inputContainsNumber4 = Regex.IsMatch(words[3], @"^-*[0-9,\.]+$");
if(inputContainsNumber && inputContainsNumber2 && inputContainsNumber3
&& inputContainsNumber4)
{
double test1 = double.Parse(words[0]);
double test2 = double.Parse(words[1]);
double test3 = double.Parse(words[2]);
double test4 = double.Parse(words[3]);
double test5 = ReadCoordinateFromConsole(test1, test2, test3,
test4);
return test5;
}
Console.WriteLine("hmm, doesn't look correct - try again");
}
}
あなたは(https://www.google.com [ステップスルーするためにデバッガを使用]みました/ search?q = +デバッガ+ in + visual + studio)を使用して、各ステップで計算を確認します。 – Reddog
実装しようとしている数式は何ですか?また、あなたのメソッドが 'ReadCoordinateFromConsole'と呼ばれる理由は何ですか? –
同じ入力を使って、正確なコードを使用しましたが、347.328を得ました...そして、数式が正しいように見えるので、間違っていることを理解できません。 –