diff --git a/src/grl-coordinates.adb b/src/grl-coordinates.adb index aeae82b..1a04e23 100644 --- a/src/grl-coordinates.adb +++ b/src/grl-coordinates.adb @@ -1,3 +1,5 @@ +with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; + package body Grl.Coordinates is function To_DD (DM_Base : Coordinate) return Coordinate is @@ -37,4 +39,39 @@ package body Grl.Coordinates is return DD_Target; end To_DD; + function Plane_Distance (Origin, Destination : Coordinate) return H is + Heuristic : H; + A : H; + DD_Origin : constant Coordinate := To_DD (Origin); + DD_Destination : constant Coordinate := To_DD (Destination); + Earth_Radius : constant H := 6_371_000.0; -- M + Delta_Phi : constant H := -- Latitude difference + H (DD_Destination.DD.Latitude - DD_Origin.DD.Latitude); + Delta_Lambda : constant H := -- Longitude difference + H (DD_Destination.DD.Longitude - DD_Origin.DD.Longitude); + + -- @name Hav + -- @return Float + -- @parameter Theta:Float + -- @variable Fraction : Float + -- @description Compute Haversine function. + function Hav (Theta : H) return Float is + Fraction : Float; + begin + Fraction := Float (Theta) / 2.0; + return Sin (Fraction)**2; + end Hav; + begin + + A := + H (Hav (Delta_Phi) + + Cos (Float (DD_Origin.DD.Latitude)) * + Cos (Float (DD_Destination.DD.Latitude)) * Hav (Delta_Lambda)); + + -- Haversine formula + Heuristic := Earth_Radius * H (2.0 * Arcsin (Sqrt (Float (A)))); + + return Heuristic; + end Plane_Distance; + end Grl.Coordinates; diff --git a/src/grl-coordinates.ads b/src/grl-coordinates.ads index 0210e25..dfed010 100644 --- a/src/grl-coordinates.ads +++ b/src/grl-coordinates.ads @@ -62,7 +62,7 @@ is Format_DMS.Cardinal_Lat in North | South and then Format_DMS.Cardinal_Lon in East | West; - -- DDM + -- DDM type Format_DDM is record Degree_Lat : DM_Degree_Lat; Degree_Lon : DM_Degree_Lon; @@ -118,4 +118,12 @@ is (DM_Base.Form = DD) or else (DM_Base.Form = DMS) or else (DM_Base.Form = DDM); + -- @name Plane_Distance + -- @return H + -- @parameter Origin:Coordinate + -- @parameter Destination:Coordinate + -- @variable Heuristic : H + -- @description Calculate heuristic between coordinates. + function Plane_Distance (Origin, Destination : Coordinate) return H; + end Grl.Coordinates;