From 0590079fb57a552f0c280c1fd1d51bc827169e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Fillod=2C=20F8CFE?= Date: Thu, 22 Aug 2002 23:42:20 +0000 Subject: [PATCH] fixed locator and dms conversion and also got rid of trunc usage, fixed misnamed 'bearing' references git-svn-id: https://hamlib.svn.sourceforge.net/svnroot/hamlib/trunk@1134 7ae35d74-ebe9-4afe-98af-79ac388436b8 --- configure.ac | 3 --- lib/trunc.c | 13 ------------ src/locator.c | 55 +++++++++++++++++++++++++++---------------------- tests/testloc.c | 34 +++++++++++++++++++++--------- 4 files changed, 54 insertions(+), 51 deletions(-) delete mode 100644 lib/trunc.c diff --git a/configure.ac b/configure.ac index 5efefad52..d46eae6f7 100644 --- a/configure.ac +++ b/configure.ac @@ -70,8 +70,6 @@ dnl Checks for libraries. AC_CHECK_FUNC(sin, [MATH_LIBS=""], [MATH_LIBS="-lm"]) AC_CHECK_LIB(ieee, main, [MATH_LIBS="-lieee $MATH_LIBS"], [], []) -# trunc is part of C99 only -AC_CHECK_LIB([c], [trunc], [], [AC_LIBOBJ(trunc)], [$MATH_LIBS]) #-------------------------------------------------------------------- # Interactive UNIX requires -linet instead of -lsocket, plus it # needs net/errno.h to define the socket-related error codes. @@ -309,7 +307,6 @@ AC_SUBST(ROT_BACKENDEPS) AC_SUBST(BINDING_LIST) -AC_LIBSOURCES([trunc.c]) AC_SUBST(INCLUDES) AC_CONFIG_FILES([Makefile diff --git a/lib/trunc.c b/lib/trunc.c deleted file mode 100644 index 5b87a2200..000000000 --- a/lib/trunc.c +++ /dev/null @@ -1,13 +0,0 @@ -#include - -#ifndef HAVE_TRUNC - -#include - -double -trunc (double x) -{ - return round(x); /* FIXME !! */ -} - -#endif /* !HAVE_TRUNC */ diff --git a/src/locator.c b/src/locator.c index 982717f1f..16617c358 100644 --- a/src/locator.c +++ b/src/locator.c @@ -2,16 +2,16 @@ * \file src/locator.c * \brief Ham Radio Control Libraries interface * \author Stephane Fillod - * \date 2000-2001 + * \date 2000-2002 * * Hamlib interface is a frontend implementing wrapper functions. */ /* * Hamlib Interface - locator and bearing conversion calls - * Copyright (c) 2001 by Stephane Fillod + * Copyright (c) 2001-2002 by Stephane Fillod * - * $Id: locator.c,v 1.2 2001-12-28 20:33:27 fillods Exp $ + * $Id: locator.c,v 1.3 2002-08-22 23:42:20 fillods Exp $ * * Code to determine bearing and range was taken from the Great Circle, * by S. R. Sampson, N5OWK. @@ -73,7 +73,10 @@ */ double dms2dec(int degrees, int minutes, int seconds) { + if (degrees >= 0) return (double)degrees + (double)minutes/60. + (double)seconds/3600.; + else + return (double)degrees + 1. - (double)minutes/60. - (double)seconds/3600.; } /** @@ -108,7 +111,6 @@ void dec2dms(double dec, int *degrees, int *minutes, int *seconds) sec = (int)floor(st); if (deg < 0 && sec != 0) sec = 60 - sec; - *degrees = deg; *minutes = min; *seconds = sec; @@ -159,12 +161,12 @@ int locator2longlat(double *longitude, double *latitude, const char *locator) } *longitude = 20.0 * (loc[0]-'A') - 180.0 + 2.0 * (loc[2]-'0') + - (loc[4]-'A')/12.0; + (loc[4]-'A')/12.0; if (loc[0] <= 'I' && (loc[2] != '0' || loc[4] != 'A')) *longitude += 1; *latitude = 10.0 * (loc[1]-'A') - 90.0 + (loc[3]-'0') + - (loc[5]-'A')/24.0; + (loc[5]-'A')/24.0; return 0; } @@ -185,48 +187,51 @@ int locator2longlat(double *longitude, double *latitude, const char *locator) void longlat2locator(double longitude, double latitude, char *locator) { double tmp; + int deg, min, sec; + + tmp = fmod(longitude, 360) + 180.; - tmp = longitude + 180.; locator[0] = 'A' + (int)floor(tmp/20.); tmp = fmod(tmp, 20.); locator[2] = '0' + (int)floor(tmp/2.); - tmp = 12.5*fmod(tmp, 2.); + tmp = 12.*fabs(floor(longitude)-longitude); locator[4] = 'A' + (int)floor(tmp); - tmp = latitude + 90.; - locator[1] = 'A' + (int)(tmp/10.); + tmp = fmod(latitude, 360) + 90.; + + locator[1] = 'A' + (int)floor(tmp/10.); tmp = fmod(tmp, 10.); - locator[3] = '0' + (int)tmp; - tmp = 25. * (tmp - trunc(tmp)); - locator[5] = 'A' + (int)tmp; + locator[3] = '0' + (int)floor(tmp); + tmp = 25. * fabs(floor(latitude)-latitude); + locator[5] = 'A' + (int)floor(tmp); } /** - * \brief Calculate the bearing and azimuth between two points. + * \brief Calculate the distance and bearing between two points. * \param lon1 The local longitude, decimal degrees * \param lat1 The local latitude, decimal degrees * \param lon2 The remote longitude, decimal degrees * \param lat2 The remote latitude, decimal degrees - * \param bearing The location where to store the bearing - * \param azimuth The location where to store the azimuth + * \param distance The location where to store the distance + * \param azimuth The location where to store the bearing * * Calculate the QRB between \a lat1,\a lat1 and - * \a lon2,\a lat2, and return the bearing in kilometers and + * \a lon2,\a lat2, and return the distance in kilometers and * azimuth in decimal degrees for the short path. * * This version also takes into consideration the two points * being close enough to be in the near-field, and the antipodal points, * which are easily calculated. * - * \sa bearing_long_path(), azimuth_long_path() + * \sa distance_long_path(), azimuth_long_path() */ int qrb(double lon1, double lat1, double lon2, double lat2, - double *bearing, double *azimuth) + double *distance, double *azimuth) { double delta_long, tmp, arc, cosaz, az; - if (!bearing || !azimuth) + if (!distance || !azimuth) return -1; if ((lat1 > 90.0 || lat1 < -90.0) || (lat2 > 90.0 || lat2 < -90.0)) @@ -261,7 +266,7 @@ int qrb(double lon1, double lat1, double lon2, double lat2, if (tmp > .999999) { /* Station points coincide, use an Omni! */ - *bearing = 0.0; + *distance = 0.0; *azimuth = 0.0; return 0; } @@ -274,7 +279,7 @@ int qrb(double lon1, double lat1, double lon2, double lat2, * and you get 10800 nm, or whatever units... */ - *bearing = 180.0*ARC_IN_KM; + *distance = 180.0*ARC_IN_KM; *azimuth = 0.0; return 0; } @@ -289,7 +294,7 @@ int qrb(double lon1, double lat1, double lon2, double lat2, /* Short Path */ - *bearing = ARC_IN_KM * RADIAN * arc; + *distance = ARC_IN_KM * RADIAN * arc; /* * Long Path @@ -320,9 +325,9 @@ int qrb(double lon1, double lat1, double lon2, double lat2, return 0; } -double bearing_long_path(double bearing) +double distance_long_path(double distance) { - return (ARC_IN_KM * 360.0) - bearing; + return (ARC_IN_KM * 360.0) - distance; } double azimuth_long_path(double azimuth) diff --git a/tests/testloc.c b/tests/testloc.c index 180b87002..1bc6f0381 100644 --- a/tests/testloc.c +++ b/tests/testloc.c @@ -16,7 +16,7 @@ int main (int argc, char *argv[]) { char recodedloc[8], *loc1, *loc2; double lon1, lat1, lon2, lat2; - double bearing, az; + double distance, az; int deg, min, sec; int retcode; @@ -28,12 +28,19 @@ int main (int argc, char *argv[]) loc1 = argv[1]; loc2 = argc > 2 ? argv[2] : NULL; - retcode = locator2longlat(&lon1, &lat1, loc1); printf("Locator1: %s\n", loc1); + retcode = locator2longlat(&lon1, &lat1, loc1); + dec2dms(lon1, °, &min, &sec); - printf(" Longitude: %f, %d° %d' %d\"\n", lon1, deg, min, sec); + printf(" Longitude: %f, %d° %d' %d\"\n", lon1, deg, min, sec); + lon1 = dms2dec(deg, min, sec); + printf(" Recoded lon: %f\n", lon1); + dec2dms(lat1, °, &min, &sec); - printf(" Latitude: %f, %d° %d' %d\"\n", lat1, deg, min, sec); + printf(" Latitude: %f, %d° %d' %d\"\n", lat1, deg, min, sec); + lat1 = dms2dec(deg, min, sec); + printf(" Recoded lat: %f\n", lat1); + longlat2locator(lon1, lat1, recodedloc); recodedloc[6] = '\0'; printf(" Recoded: %s\n", recodedloc); @@ -41,24 +48,31 @@ int main (int argc, char *argv[]) if (loc2 == NULL) exit(0); - retcode = locator2longlat(&lon2, &lat2, loc2); printf("\nLocator2: %s\n", loc2); + retcode = locator2longlat(&lon2, &lat2, loc2); + dec2dms(lon2, °, &min, &sec); - printf(" Longitude: %f, %d° %d' %d\"\n", lon2, deg, min, sec); + printf(" Longitude: %f, %d° %d' %d\"\n", lon2, deg, min, sec); + lon2 = dms2dec(deg, min, sec); + printf(" Recoded lon: %f\n", lon2); + dec2dms(lat2, °, &min, &sec); - printf(" Latitude: %f, %d° %d' %d\"\n", lat2, deg, min, sec); + printf(" Latitude: %f, %d° %d' %d\"\n", lat2, deg, min, sec); + lat2 = dms2dec(deg, min, sec); + printf(" Recoded lat: %f\n", lat2); + longlat2locator(lon2, lat2, recodedloc); recodedloc[6] = '\0'; printf(" Recoded: %s\n", recodedloc); - retcode = qrb(lon1, lat1, lon2, lat2, &bearing, &az); + retcode = qrb(lon1, lat1, lon2, lat2, &distance, &az); if (retcode != 0) { fprintf(stderr, "QRB error: %d\n", retcode); exit(2); } dec2dms(az, °, &min, &sec); - printf("\nBearing: %.2fkm\n", bearing); - printf("Azimuth: %f, %d° %d' %d\"\n", az, deg, min, sec); + printf("\nDistance: %.2fkm\n", distance); + printf("Bearing: %f, %d° %d' %d\"\n", az, deg, min, sec); return 0; }