+/*
+ * Different Lat/Lon related function (conversion, distances, etc)
+ */
#define _GNU_SOURCE
#include <math.h>
#define LATLON_MAX 2147483646
#define LATLON_MIN -2147483646
+#define EARTH_RADIUS (3440.06479f)
+
/* Convert latitude to integerized+mercator projected value */
gint32
lat2mp_int(gdouble lat)
return lrint(lon/180*LATLON_MAX);
}
+/**
+ * Calculate the distance between two lat/lon pairs. The distance is returned
+ * in kilometer.
+ */
+gfloat calculate_distance(gfloat lat1, gfloat lon1, gfloat lat2, gfloat lon2)
+{
+gdouble dlat, dlon, slat, slon, a;
+
+/* Convert to radians. */
+lat1 *= (M_PI_4l / 180.f);
+lon1 *= (M_PI_4l / 180.f);
+lat2 *= (M_PI_4l / 180.f);
+lon2 *= (M_PI_4l / 180.f);
+
+dlat = lat2 - lat1;
+dlon = lon2 - lon1;
+
+slat = sinf(dlat / 2.f);
+slon = sinf(dlon / 2.f);
+a = (slat * slat) + (cosf(lat1) * cosf(lat2) * slon * slon);
+
+return ((2.f * atan2f(sqrtf(a), sqrtf(1.f - a))) * EARTH_RADIUS);
+}
+
vprintf("%s(): return\n", __PRETTY_FUNCTION__);
}
-/**
- * Calculate the distance between two lat/lon pairs. The distance is returned
- * in kilometers and should be converted using UNITS_CONVERT[_units].
- */
-gfloat calculate_distance(gfloat lat1, gfloat lon1, gfloat lat2, gfloat lon2)
-{
- gdouble dlat, dlon, slat, slon, a;
-
- /* Convert to radians. */
- lat1 *= (PI / 180.f);
- lon1 *= (PI / 180.f);
- lat2 *= (PI / 180.f);
- lon2 *= (PI / 180.f);
-
- dlat = lat2 - lat1;
- dlon = lon2 - lon1;
-
- slat = sinf(dlat / 2.f);
- slon = sinf(dlon / 2.f);
- a = (slat * slat) + (cosf(lat1) * cosf(lat2) * slon * slon);
-
- vprintf("%s(): return\n", __PRETTY_FUNCTION__);
- return ((2.f * atan2f(sqrtf(a), sqrtf(1.f - a))) * EARTH_RADIUS);
-}
-
/**
* Convert the float lat/lon/speed/heading data into integer units.
*/