summaryrefslogtreecommitdiffstats
path: root/divesite.c
diff options
context:
space:
mode:
authorGravatar Anton Lundin <glance@acc.umu.se>2015-06-13 11:54:33 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-06-13 21:08:11 -0700
commit7a594e3ec5e66f30a1604d492f3bb1af1259afd7 (patch)
treeb0fb86b70ad06b5386db0518d1d70df07c54bfe3 /divesite.c
parent9ec155fad05d53aaac42c7a8b9460b7eb6a0c803 (diff)
downloadsubsurface-7a594e3ec5e66f30a1604d492f3bb1af1259afd7.tar.gz
Simplify distance calculations
This simplifies the distance calculations and removes a dependency. This version uses propper math instead of my to simple previous version. Signed-off-by: Anton Lundin <glance@acc.umu.se> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'divesite.c')
-rw-r--r--divesite.c23
1 files changed, 19 insertions, 4 deletions
diff --git a/divesite.c b/divesite.c
index 3caa7b3ba..e096c7172 100644
--- a/divesite.c
+++ b/divesite.c
@@ -2,6 +2,8 @@
#include "divesite.h"
#include "dive.h"
+#include <math.h>
+
struct dive_site_table dive_site_table;
/* there could be multiple sites of the same name - return the first one */
@@ -34,8 +36,21 @@ uint32_t get_dive_site_uuid_by_gps(degrees_t latitude, degrees_t longitude, stru
return 0;
}
-/* this is in globe.cpp, so including the .h file is a pain */
-extern double getDistance(int lat1, int lon1, int lat2, int lon2);
+// Calculate the distance in meters between two coordinates.
+static unsigned int get_distance(degrees_t lat1, degrees_t lon1, degrees_t lat2, degrees_t lon2)
+{
+ double lat1_r = udeg_to_radians(lat1.udeg);
+ double lat2_r = udeg_to_radians(lat2.udeg);
+ double lat_d_r = udeg_to_radians(lat2.udeg-lat1.udeg);
+ double lon_d_r = udeg_to_radians(lon2.udeg-lon1.udeg);
+
+ double a = sin(lat_d_r/2) * sin(lat_d_r/2) +
+ cos(lat2_r) * cos(lat2_r) * sin(lon_d_r/2) * sin(lon_d_r/2);
+ double c = 2 * atan2(sqrt(a), sqrt(1.0 - a));
+
+ // Earth radious in metres
+ return 6371000 * c;
+}
/* find the closest one, no more than distance meters away - if more than one at same distance, pick the first */
uint32_t get_dive_site_uuid_by_gps_proximity(degrees_t latitude, degrees_t longitude, int distance, struct dive_site **dsp)
@@ -43,10 +58,10 @@ uint32_t get_dive_site_uuid_by_gps_proximity(degrees_t latitude, degrees_t longi
int i;
int uuid = 0;
struct dive_site *ds;
- double cur_distance, min_distance = distance + 0.001;
+ unsigned int cur_distance, min_distance = distance;
for_each_dive_site (i, ds) {
if (dive_site_has_gps_location(ds) &&
- (cur_distance = getDistance(ds->latitude.udeg, ds->longitude.udeg, latitude.udeg, longitude.udeg)) < min_distance) {
+ (cur_distance = get_distance(ds->latitude, ds->longitude, latitude, longitude)) < min_distance) {
min_distance = cur_distance;
uuid = ds->uuid;
if (dsp)