Skip to content

Commit

Permalink
Integrate
Browse files Browse the repository at this point in the history
  • Loading branch information
Wetitpig committed Aug 24, 2020
1 parent dbb206a commit 0ed3296
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 163 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
haversine
vincenty
bin/
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ CFLAGS=-O3 -Iinclude

LDFLAGS=-lm

all: haversine vincenty
HEADERS = constants.h functions.h haversine.h vincenty.h

haversine:
@mkdir -p bin
$(CC) $(CFLAGS) -o bin/haversine src/haversine.c src/math.c src/io.c $(LDFLAGS)
%.o: src/%.c $(HEADERS)
$(CC) $(CFLAGS) -c -o $@ $<

vincenty:
all: src/haversine.o src/vincenty.o src/main.o src/io.o src/math.o
@mkdir -p bin
$(CC) $(CFLAGS) -o bin/vincenty src/vincenty.c src/math.c src/io.c $(LDFLAGS)
$(CC) $(CFLAGS) -o bin/geodesic src/haversine.o src/vincenty.o src/main.o src/io.o src/math.o -lm
@strip bin/geodesic

clean:
rm -rf bin
rm -rf src/*.o
7 changes: 6 additions & 1 deletion include/functions.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#include "constants.h"

#ifndef __HAVE_FUNCTIONS_H__

#define __HAVE_FUNCTIONS_H__

int scan(int argc, char *argv, struct Coordinates *loc);
void print(int order, long double s, long double start, long double end);

long double sqr(long double operand);
long double atan2_modified(long double y, long double x);

#endif
10 changes: 10 additions & 0 deletions include/haversine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "constants.h"

#ifndef __HAVE_HAVERSINE_H__

#define __HAVE_HAVERSINE_H__

long double haversine_distance(struct Coordinates *location);
long double haversine_bearing(struct Coordinates *start, struct Coordinates *end);

#endif
15 changes: 15 additions & 0 deletions include/vincenty.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "constants.h"

#ifndef __HAVE_VINCENTY_H__

#define __HAVE_VINCENTY_H__

struct vincenty_result {
long double distance;
long double start;
long double end;
};

void vincenty(struct vincenty_result *result, struct Coordinates *location);

#endif
51 changes: 3 additions & 48 deletions src/haversine.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "constants.h"

#include "functions.h"
#include "haversine.h"

long double distance(struct Coordinates *location)
long double haversine_distance(struct Coordinates *location)
{
long double londiff, latdiff;
long double a, b;
Expand All @@ -20,7 +18,7 @@ long double distance(struct Coordinates *location)
return 2 * atan2_modified(sqrtl(a), sqrtl(1-a)) * RADIUS;
}

long double bearing(struct Coordinates *start, struct Coordinates *end)
long double haversine_bearing(struct Coordinates *start, struct Coordinates *end)
{
long double londiff, y, x;

Expand All @@ -29,46 +27,3 @@ long double bearing(struct Coordinates *start, struct Coordinates *end)
x = cosl(start->lat) * sinl(end->lat) - sinl(start->lat) * cosl(end->lat) * cosl(londiff);
return atan2_modified(y, x) / (RAD);
}

int main(int argc, char **argv)
{
if (argc == 2) {
printf("Usage:\n\t%s [coordinate 1] [coordinate 2] ...\n", argv[0]);
return 1;
}

struct Coordinates *location = malloc(sizeof(struct Coordinates) * 2);

long double c, total = 0, start, end;
int i, j;

scan(argc, argv[1], location);

location->lat = location->lat * RAD;
location->lon = location->lon * RAD;

puts("{");

for (i = 2; i < argc || argc == 1; ++i) {
if (scan(argc, argv[i], (location + 1)) == -1)
break;

(location + 1)->lat = (location + 1)->lat * RAD;
(location + 1)->lon = (location + 1)->lon * RAD;

c = distance(location);
start = NORMALISE(bearing(location, location + 1));
end = NORMALISE(bearing(location + 1, location) - 180);

total += c;
memcpy(location, location + 1, sizeof(struct Coordinates));

print(i - 2, c, start, end);
}

free(location);

printf(" \"total_distance\": %Lf\n}\n", total);

return 0;
}
133 changes: 133 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <strings.h>

#include "constants.h"
#include "functions.h"
#include "haversine.h"
#include "vincenty.h"

void help(char *name)
{
puts("Usage:");
printf("\n\t%s [options] [coordinate 1] [coordinate 2] ...\n", name);
printf("\t-h Show usage.\n\t-f [haversine|vincenty] Set formula to haversine or vincenty.\n\t-s Compute distance.\n\t-o Compute azimuths.\n\n");
puts("More info in README.md");
return;
}


int main(int argc, char **argv)
{
int i, j;
int distance = 0, azimuth = 0;

while ((i = getopt(argc, argv, "f:soh")) != -1) {
switch (i)
{
case 'f':
if (strcmp(optarg, "haversine") == 0)
j = 1;
else if (strcmp(optarg, "vincenty") == 0)
j = 2;
else {
puts("Invalid formula. Abort.");
exit(1);
}
break;
case 's':
distance = 1;
break;
case 'o':
azimuth = 1;
break;
case 'h':
help(argv[0]);
exit(1);
break;
default:
exit(1);
break;
}
}

if (distance == 0 && azimuth == 0) {
puts("Nothing to be shown. Abort.");
exit(1);
}

struct Coordinates *location = malloc(sizeof(struct Coordinates) * 2);

long double c, total = 0, start, end;
struct vincenty_result *res;

scan(optind == argc, argv[optind], location);

location->lat = location->lat * RAD;
location->lon = location->lon * RAD;

putchar('{');

for (i = 1; optind == argc || (optind + i) < argc; ++i) {
if (optind != argc)
scan(argc, argv[optind + i], location + 1);
else if (scan(1, NULL, location + 1) == -1)
break;

(location + 1)->lat = (location + 1)->lat * RAD;
(location + 1)->lon = (location + 1)->lon * RAD;

if (azimuth == 1 && i != 1)
printf(",");

printf("\n \"%d\": {\n", i - 1);

switch (j)
{
case 1:
if (distance == 1)
c = haversine_distance(location);
if (azimuth == 1) {
start = NORMALISE(haversine_bearing(location, location + 1));
end = NORMALISE(haversine_bearing(location + 1, location) - 180);
}
break;
case 2:
res = malloc(sizeof(struct vincenty_result));
vincenty(res, location);
c = res->distance;
start = res->start;
end = res->end;
free(res);
break;
}

if (distance == 1) {
total += c;
printf(" \"distance\": %Lf", c);
if (azimuth == 1)
printf(",\n");
else
printf("\n },");
}

if (azimuth == 1)
printf(" \"start_azimuth\": %Lf,\n \"end_azimuth\": %Lf\n }", start, end);

memcpy(location, location + 1, sizeof(struct Coordinates));
}

free(location);

if (distance == 1) {
if (azimuth == 1 && total != 0)
printf(",");
printf("\n \"total_distance\": %Lf\n}\n", total);
}
else
printf("\n}\n");

exit(0);
}
Loading

0 comments on commit 0ed3296

Please sign in to comment.