Skip to content

Commit

Permalink
Add example for dist and bearing
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Jul 26, 2020
1 parent 1d1e72c commit f24c71f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
14 changes: 14 additions & 0 deletions dev/VisualStudio/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,21 @@ extern void run_tests();

int
main() {
lwgps_float_t distance, bearing;
run_tests();

/* Calculate distance and bearing */
lwgps_distance_bearing(40.6, -73.7, 48.3, 11.7, &distance, &bearing);

printf("Distance: %lf meters\r\n", (double)distance);
printf("Bearing: %lf degrees\r\n", (double)bearing);

lwgps_distance_bearing(48.3, 11.7, 40.6, -73.7, &distance, &bearing);
printf("Distance: %lf meters\r\n", (double)distance);
printf("Bearing: %lf degrees\r\n", (double)bearing);

return 0;
}

/* JFK: 40.642569, -73.783790 */
/* Munich: 48.353962, 11.775114 */
10 changes: 10 additions & 0 deletions docs/examples/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,15 @@ Data are later processed outside interrupt context.
:linenos:
:caption: Example of buffer

Distance and bearing
^^^^^^^^^^^^^^^^^^^^

Library provides calculation of distance and bearing between `2` coordinates on earth.

.. literalinclude:: ../../examples/example_dist_bear.c
:language: c
:linenos:
:caption: Distance and bearing calculation

.. toctree::
:maxdepth: 2
24 changes: 24 additions & 0 deletions examples/example_dist_bear.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "lwgps/lwgps.h"

/* Distance and bearing results */
lwgps_float_t dist, bear;

/* New York coordinates */
lwgps_float_t lat1 = 40.685721;
lwgps_float_t lon1 = -73.820465;

/* Munich coordinates */
lwgps_float_t lat2 = 48.150906;
lwgps_float_t lon2 = 11.554176;

/* Go from New York to Munich */
/* Calculate distance and bearing related to north */
lwgps_distance_bearing(lat1, lon1, lat2, lon2, &dist, &bear);
printf("Distance: %f meters\r\n", (float)dist);
printf("Initial bearing: %f degrees\r\n", (float)bear);

/* Go from Munich to New York */
/* Calculate distance and bearing related to north */
lwgps_distance_bearing(lat2, lon2, lat1, lon1, &dist, &bear);
printf("Distance: %f meters\r\n", (float)dist);
printf("Initial bearing: %f degrees\r\n", (float)bear);
10 changes: 5 additions & 5 deletions lwgps/src/lwgps/lwgps.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ prv_parse_number(lwgps_t* gh, const char* t) {
if (t == NULL) {
t = gh->p.term_str;
}
for (; t != NULL && *t == ' '; t++) {} /* Strip leading spaces */
for (; t != NULL && *t == ' '; ++t) {} /* Strip leading spaces */

minus = (*t == '-' ? (t++, 1) : 0);
for (; t != NULL && CIN(*t); t++) {
minus = (*t == '-' ? (++t, 1) : 0);
for (; t != NULL && CIN(*t); ++t) {
res = 10 * res + CTN(*t);
}
return minus ? -res : res;
Expand All @@ -91,7 +91,7 @@ prv_parse_float_number(lwgps_t* gh, const char* t) {
if (t == NULL) {
t = gh->p.term_str;
}
for (; t != NULL && *t == ' '; t++) {} /* Strip leading spaces */
for (; t != NULL && *t == ' '; ++t) {} /* Strip leading spaces */

#if LWGPS_CFG_DOUBLE
res = strtod(t, NULL); /* Parse string to double */
Expand Down Expand Up @@ -514,7 +514,7 @@ lwgps_distance_bearing(lwgps_float_t las, lwgps_float_t los, lwgps_float_t lae,
if (d != NULL) {
/*
* a = sin(df / 2)^2 + cos(las) * cos(lae) * sin(dfi / 2)^2
* *d = RADIUS * 2 * atan(a / (1 - a)) * 1000 (for meters)
* *d = RADIUS * 2 * atan(sqrt(a) / sqrt(1 - a)) * 1000 (for meters)
*/
#if LWGPS_CFG_DOUBLE
a = FLT(sin(df * 0.5) * sin(df * 0.5) + sin(dfi * 0.5) * sin(dfi * 0.5) * cos(las) * cos(lae));
Expand Down

0 comments on commit f24c71f

Please sign in to comment.