[PATCH] Simplify distance calculations

Anton Lundin glance at acc.umu.se
Fri Jun 12 14:26:51 PDT 2015


This simplifies the distance calculations and removes a dependency.

Signed-off-by: Anton Lundin <glance at acc.umu.se>
---
Now when Dirk is going crazy and adds all kinds of dependencies on
stuff which haven't even landed in those projects yet, someone needs to
remove some dependencies to.

And, yes, the world is flat.

 CMakeLists.txt  |  4 ++--
 divesite.c      | 14 ++++++++++----
 qt-ui/globe.cpp |  8 --------
 3 files changed, 12 insertions(+), 14 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f82ad37..c5e0391 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -128,8 +128,8 @@ if(NOT (insource OR insourcedir))
 endif()
 
 # configure Qt.
-find_package(Qt5 REQUIRED COMPONENTS Core Concurrent Widgets Network WebKitWidgets PrintSupport Svg Test LinguistTools Positioning)
-set(QT_LIBRARIES Qt5::Core Qt5::Concurrent Qt5::Widgets Qt5::Network Qt5::WebKitWidgets Qt5::PrintSupport Qt5::Svg Qt5::Positioning)
+find_package(Qt5 REQUIRED COMPONENTS Core Concurrent Widgets Network WebKitWidgets PrintSupport Svg Test LinguistTools)
+set(QT_LIBRARIES Qt5::Core Qt5::Concurrent Qt5::Widgets Qt5::Network Qt5::WebKitWidgets Qt5::PrintSupport Qt5::Svg)
 set(QT_TEST_LIBRARIES ${QT_LIBRARIES} Qt5::Test)
 
 # Generate the ssrf-config.h every 'make'
diff --git a/divesite.c b/divesite.c
index 3caa7b3..d775a46 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,12 @@ 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);
+// We could be propper and use the Haversine formula here, but hey, the world is flat dude.
+static unsigned int get_distance(degrees_t lat1, degrees_t lon1, degrees_t lat2, degrees_t lon2)
+{
+	// Earth radious is about 111.5 km where i live
+	return sqrt(lat1.udeg*lat2.udeg + lon1.udeg*lon2.udeg) / 10;
+}
 
 /* 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 +49,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)
diff --git a/qt-ui/globe.cpp b/qt-ui/globe.cpp
index 76d48b7..b8e0ec8 100644
--- a/qt-ui/globe.cpp
+++ b/qt-ui/globe.cpp
@@ -1,5 +1,4 @@
 #include "globe.h"
-#include <QGeoCoordinate>
 #ifndef NO_MARBLE
 #include "mainwindow.h"
 #include "helpers.h"
@@ -393,10 +392,3 @@ void GlobeGPS::reload()
 {
 }
 #endif
-
-extern "C" double getDistance(int lat1, int lon1, int lat2, int lon2)
-{
-	QGeoCoordinate c1(lat1 / 1000000.0, lon1 / 1000000.0);
-	QGeoCoordinate c2(lat2 / 1000000.0, lon2 / 1000000.0);
-	return c1.distanceTo(c2);
-}
-- 
2.1.4



More information about the subsurface mailing list