Une des fonctions les plus communes de la géographie et des systèmes modernes, c'est le calcul de la distance géographique entre deux coordonnées de Longitude et de Latitude. Il n'y a aucune nécessité de grande connaissance en trigonométrie pour arriver à se genre de calcul dans le format qu'on le souhaite, Km, Miles ou Miles Nautiques. Ainsi, si vous savez les coordonnées suivantes:
Ville | Latitude | Longitude |
---|---|---|
Montréal | 45 31N | 73 34O |
Paris | 48 50N | 2 20E |
Tout d'abord, il faut créer un projet «Standard EXE», et effectuer «Remove->Form1» et enfin «Add->Module». Après, on insère le code dans le module. A l'aide du code source Visual Basic avec les API Windows suivant, vous trouvez la réponse que vous souhaitez:
Private Const PI = 3.14159265358979
Private hConsoleOut As Long
Private Declare Function AllocConsole Lib "kernel32" () As Long
Private Declare Function FreeConsole Lib "kernel32" () As Long
Private Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long
Private Declare Function SetConsoleTextAttribute Lib "kernel32" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
Private Declare Function WriteFile Lib "kernel32" _
(ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, _
lpNumberOfBytesWritten As Long, lpOverlapped As Any) As Long
Sub InitConsole()
AllocConsole
hConsoleOut = GetStdHandle(-11& )
SetConsoleTextAttribute hConsoleOut, 7
End Sub
Private Sub ConsolePrint(MsgOut As String)
Dim lWritten As Long
If WriteFile(hConsoleOut, ByVal MsgOut, Len(MsgOut), lWritten, ByVal 0) = 0 Then
End If
End Sub
Function ACos(a As Double) As Double
If Abs(a) = 1 Then
ACos = (1 - a) * PI / 2
Else
ACos = Atn(-a / Sqr(1 - a * a)) + 2 * Atn(1)
End If
End Function
Function CoordToDeltaKm(Q1Latitude As Double, Q1LatiDeg As Double, Q1LatiDirection As String, Q1Longitude As Double, Q1LongDeg As Double, Q1LongDirection As String, Q2Latitude As Double, Q2LatiDeg As Double, Q2LatiDirection As String, Q2Longitude As Double, Q2LongDeg As Double, Q2LongDirection As String) As Double
a1 = (Q1Latitude + (Q1LatiDeg / 60)) * PI / 180
If Q1LatiDirection = "N" Then
a1 = -a1
End If
b1 = (Q1Longitude + (Q1LongDeg / 60)) * PI / 180
If Q1LongDirection = "O" Then
b1 = -b1
End If
a2 = (Q2Latitude + (Q2LatiDeg / 60)) * PI / 180
If Q2LatiDirection = "N" Then
a2 = -a2
End If
b2 = (Q2Longitude + (Q2LongDeg / 60)) * PI / 180
If Q2LongDirection = "O" Then
b2 = -b2
End If
RawDelta = ACos(Cos(a1) * Cos(b1) * Cos(a2) * Cos(b2) + Cos(a1) * Sin(b1) * Cos(a2) * Sin(b2) + Sin(a1) * Sin(a2))
CoordToDeltaKm = RawDelta * 6378#
End Function
Function CoordToDeltaStatuteMiles(Q1Latitude As Double, Q1LatiDeg As Double, Q1LatiDirection As String, Q1Longitude As Double, Q1LongDeg As Double, Q1LongDirection As String, Q2Latitude As Double, Q2LatiDeg As Double, Q2LatiDirection As String, Q2Longitude As Double, Q2LongDeg As Double, Q2LongDirection As String) As Double
a1 = (Q1Latitude + (Q1LatiDeg / 60)) * PI / 180
If Q1LatiDirection = "N" Then
a1 = -a1
End If
b1 = (Q1Longitude + (Q1LongDeg / 60)) * PI / 180
If Q1LongDirection = "O" Then
b1 = -b1
End If
a2 = (Q2Latitude + (Q2LatiDeg / 60)) * PI / 180
If Q2LatiDirection = "N" Then
a2 = -a2
End If
b2 = (Q2Longitude + (Q2LongDeg / 60)) * PI / 180
If Q2LongDirection = "O" Then
b2 = -b2
End If
RawDelta = ACos(Cos(a1) * Cos(b1) * Cos(a2) * Cos(b2) + Cos(a1) * Sin(b1) * Cos(a2) * Sin(b2) + Sin(a1) * Sin(a2))
CoordToDeltaStatuteMiles = RawDelta * 3963.1
End Function
Function CoordToDeltaNauticalMiles(Q1Latitude As Double, Q1LatiDeg As Double, Q1LatiDirection As String, Q1Longitude As Double, Q1LongDeg As Double, Q1LongDirection As String, Q2Latitude As Double, Q2LatiDeg As Double, Q2LatiDirection As String, Q2Longitude As Double, Q2LongDeg As Double, Q2LongDirection As String) As Double
a1 = (Q1Latitude + (Q1LatiDeg / 60)) * PI / 180
If Q1LatiDirection = "N" Then
a1 = -a1
End If
b1 = (Q1Longitude + (Q1LongDeg / 60)) * PI / 180
If Q1LongDirection = "O" Then
b1 = -b1
End If
a2 = (Q2Latitude + (Q2LatiDeg / 60)) * PI / 180
If Q2LatiDirection = "N" Then
a2 = -a2
End If
b2 = (Q2Longitude + (Q2LongDeg / 60)) * PI / 180
If Q2LongDirection = "O" Then
b2 = -b2
End If
RawDelta = ACos(Cos(a1) * Cos(b1) * Cos(a2) * Cos(b2) + Cos(a1) * Sin(b1) * Cos(a2) * Sin(b2) + Sin(a1) * Sin(a2))
CoordToDeltaNauticalMiles = RawDelta * 3443.9
End Function
Sub Main()
InitConsole
ConsolePrint ("Distance entre Montréal et Paris en Km: " &CStr(CoordToDeltaKm(45, 31, "N", 73, 34, "O", 48, 50, "N", 2, 20, "E")) &vbCrLf)
ConsolePrint ("Distance entre Montréal et Paris en Miles: " &CStr(CoordToDeltaStatuteMiles(45, 31, "N", 73, 34, "O", 48, 50, "N", 2, 20, "E")) &vbCrLf)
ConsolePrint ("Distance entre Montréal et Paris en Miles Nautique: " &CStr(CoordToDeltaNauticalMiles(45, 31, "N", 73, 34, "O", 48, 50, "N", 2, 20, "E")) &vbCrLf)
MsgBox "Pause"
FreeConsole
End Sub
on obtiendra le résultat suivant:
Distance entre Montréal et Paris en Km: 5510.16761889Distance entre Montréal et Paris en Miles: 3423.85470217
Distance entre Montréal et Paris en Miles Nautique: 2975.30044884
On ne peut pas voir le résultat directement dans le Visual Basic, il faut effectuer «File->Make nomdufichier.exe». Et lancer l'exécutable dans une fenêtre d'interpréteur de commande.
Comme le montre la capture d'écran suivant en l'exécutant directement la version compiler sous un prompt DOS: