Skip to content

Commit 0ed3296

Browse files
committed
Integrate
1 parent dbb206a commit 0ed3296

File tree

8 files changed

+253
-163
lines changed

8 files changed

+253
-163
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
haversine
2-
vincenty
1+
bin/

Makefile

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ CFLAGS=-O3 -Iinclude
33

44
LDFLAGS=-lm
55

6-
all: haversine vincenty
6+
HEADERS = constants.h functions.h haversine.h vincenty.h
77

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

12-
vincenty:
11+
all: src/haversine.o src/vincenty.o src/main.o src/io.o src/math.o
1312
@mkdir -p bin
14-
$(CC) $(CFLAGS) -o bin/vincenty src/vincenty.c src/math.c src/io.c $(LDFLAGS)
13+
$(CC) $(CFLAGS) -o bin/geodesic src/haversine.o src/vincenty.o src/main.o src/io.o src/math.o -lm
14+
@strip bin/geodesic
1515

1616
clean:
17-
rm -rf bin
17+
rm -rf src/*.o

include/functions.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#include "constants.h"
22

3+
#ifndef __HAVE_FUNCTIONS_H__
4+
5+
#define __HAVE_FUNCTIONS_H__
6+
37
int scan(int argc, char *argv, struct Coordinates *loc);
4-
void print(int order, long double s, long double start, long double end);
58

69
long double sqr(long double operand);
710
long double atan2_modified(long double y, long double x);
11+
12+
#endif

include/haversine.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "constants.h"
2+
3+
#ifndef __HAVE_HAVERSINE_H__
4+
5+
#define __HAVE_HAVERSINE_H__
6+
7+
long double haversine_distance(struct Coordinates *location);
8+
long double haversine_bearing(struct Coordinates *start, struct Coordinates *end);
9+
10+
#endif

include/vincenty.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "constants.h"
2+
3+
#ifndef __HAVE_VINCENTY_H__
4+
5+
#define __HAVE_VINCENTY_H__
6+
7+
struct vincenty_result {
8+
long double distance;
9+
long double start;
10+
long double end;
11+
};
12+
13+
void vincenty(struct vincenty_result *result, struct Coordinates *location);
14+
15+
#endif

src/haversine.c

+3-48
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#include <math.h>
2-
#include <stdio.h>
3-
#include <stdlib.h>
42
#include "constants.h"
5-
63
#include "functions.h"
4+
#include "haversine.h"
75

8-
long double distance(struct Coordinates *location)
6+
long double haversine_distance(struct Coordinates *location)
97
{
108
long double londiff, latdiff;
119
long double a, b;
@@ -20,7 +18,7 @@ long double distance(struct Coordinates *location)
2018
return 2 * atan2_modified(sqrtl(a), sqrtl(1-a)) * RADIUS;
2119
}
2220

23-
long double bearing(struct Coordinates *start, struct Coordinates *end)
21+
long double haversine_bearing(struct Coordinates *start, struct Coordinates *end)
2422
{
2523
long double londiff, y, x;
2624

@@ -29,46 +27,3 @@ long double bearing(struct Coordinates *start, struct Coordinates *end)
2927
x = cosl(start->lat) * sinl(end->lat) - sinl(start->lat) * cosl(end->lat) * cosl(londiff);
3028
return atan2_modified(y, x) / (RAD);
3129
}
32-
33-
int main(int argc, char **argv)
34-
{
35-
if (argc == 2) {
36-
printf("Usage:\n\t%s [coordinate 1] [coordinate 2] ...\n", argv[0]);
37-
return 1;
38-
}
39-
40-
struct Coordinates *location = malloc(sizeof(struct Coordinates) * 2);
41-
42-
long double c, total = 0, start, end;
43-
int i, j;
44-
45-
scan(argc, argv[1], location);
46-
47-
location->lat = location->lat * RAD;
48-
location->lon = location->lon * RAD;
49-
50-
puts("{");
51-
52-
for (i = 2; i < argc || argc == 1; ++i) {
53-
if (scan(argc, argv[i], (location + 1)) == -1)
54-
break;
55-
56-
(location + 1)->lat = (location + 1)->lat * RAD;
57-
(location + 1)->lon = (location + 1)->lon * RAD;
58-
59-
c = distance(location);
60-
start = NORMALISE(bearing(location, location + 1));
61-
end = NORMALISE(bearing(location + 1, location) - 180);
62-
63-
total += c;
64-
memcpy(location, location + 1, sizeof(struct Coordinates));
65-
66-
print(i - 2, c, start, end);
67-
}
68-
69-
free(location);
70-
71-
printf(" \"total_distance\": %Lf\n}\n", total);
72-
73-
return 0;
74-
}

src/main.c

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <math.h>
4+
#include <unistd.h>
5+
#include <strings.h>
6+
7+
#include "constants.h"
8+
#include "functions.h"
9+
#include "haversine.h"
10+
#include "vincenty.h"
11+
12+
void help(char *name)
13+
{
14+
puts("Usage:");
15+
printf("\n\t%s [options] [coordinate 1] [coordinate 2] ...\n", name);
16+
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");
17+
puts("More info in README.md");
18+
return;
19+
}
20+
21+
22+
int main(int argc, char **argv)
23+
{
24+
int i, j;
25+
int distance = 0, azimuth = 0;
26+
27+
while ((i = getopt(argc, argv, "f:soh")) != -1) {
28+
switch (i)
29+
{
30+
case 'f':
31+
if (strcmp(optarg, "haversine") == 0)
32+
j = 1;
33+
else if (strcmp(optarg, "vincenty") == 0)
34+
j = 2;
35+
else {
36+
puts("Invalid formula. Abort.");
37+
exit(1);
38+
}
39+
break;
40+
case 's':
41+
distance = 1;
42+
break;
43+
case 'o':
44+
azimuth = 1;
45+
break;
46+
case 'h':
47+
help(argv[0]);
48+
exit(1);
49+
break;
50+
default:
51+
exit(1);
52+
break;
53+
}
54+
}
55+
56+
if (distance == 0 && azimuth == 0) {
57+
puts("Nothing to be shown. Abort.");
58+
exit(1);
59+
}
60+
61+
struct Coordinates *location = malloc(sizeof(struct Coordinates) * 2);
62+
63+
long double c, total = 0, start, end;
64+
struct vincenty_result *res;
65+
66+
scan(optind == argc, argv[optind], location);
67+
68+
location->lat = location->lat * RAD;
69+
location->lon = location->lon * RAD;
70+
71+
putchar('{');
72+
73+
for (i = 1; optind == argc || (optind + i) < argc; ++i) {
74+
if (optind != argc)
75+
scan(argc, argv[optind + i], location + 1);
76+
else if (scan(1, NULL, location + 1) == -1)
77+
break;
78+
79+
(location + 1)->lat = (location + 1)->lat * RAD;
80+
(location + 1)->lon = (location + 1)->lon * RAD;
81+
82+
if (azimuth == 1 && i != 1)
83+
printf(",");
84+
85+
printf("\n \"%d\": {\n", i - 1);
86+
87+
switch (j)
88+
{
89+
case 1:
90+
if (distance == 1)
91+
c = haversine_distance(location);
92+
if (azimuth == 1) {
93+
start = NORMALISE(haversine_bearing(location, location + 1));
94+
end = NORMALISE(haversine_bearing(location + 1, location) - 180);
95+
}
96+
break;
97+
case 2:
98+
res = malloc(sizeof(struct vincenty_result));
99+
vincenty(res, location);
100+
c = res->distance;
101+
start = res->start;
102+
end = res->end;
103+
free(res);
104+
break;
105+
}
106+
107+
if (distance == 1) {
108+
total += c;
109+
printf(" \"distance\": %Lf", c);
110+
if (azimuth == 1)
111+
printf(",\n");
112+
else
113+
printf("\n },");
114+
}
115+
116+
if (azimuth == 1)
117+
printf(" \"start_azimuth\": %Lf,\n \"end_azimuth\": %Lf\n }", start, end);
118+
119+
memcpy(location, location + 1, sizeof(struct Coordinates));
120+
}
121+
122+
free(location);
123+
124+
if (distance == 1) {
125+
if (azimuth == 1 && total != 0)
126+
printf(",");
127+
printf("\n \"total_distance\": %Lf\n}\n", total);
128+
}
129+
else
130+
printf("\n}\n");
131+
132+
exit(0);
133+
}

0 commit comments

Comments
 (0)