-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.c
85 lines (73 loc) · 2.11 KB
/
helper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* File: helper.c
* By: Joshua Pfosi, Date: Sat May 10
* Last Updated: Mon Sep 15 11:13:27
*
* Implements helper functions
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "helper.h"
/* Args: two Positions
* Purpose: Calculate the angle defined by pos1 and pos2, using pos1 as origin
* Returns the angle in standard position between 0 and 360
*/
inline Angle ang_btwn_positions(Position pos1, Position pos2) {
Angle ang_in_rads = atan2(pos2.lat - pos1.lat, pos2.lon - pos1.lon);
Angle ang_in_degs = radians_to_degrees(ang_in_rads);
if (ang_in_degs < 0) {
ang_in_degs += 360;
}
return ang_in_degs;
}
/* Args: two angles
* Purpose: Calculate the angle between to angles
* Returns the angle
*/
inline Angle ang_btwn_angles(Angle a1, Angle a2) {
Angle a = a2 - a1;
a = (a > 0) ? a : -a;
return (a < 180) ? a : 360 - a;
}
/* Args: two directions
* Purpose: determine angle to wind for sail trim
* Notes: starboard is positive angle, port is negative angle
* angle returned (ar): -180 < ar <= 180
*/
inline Angle ang_to_wind(Angle wind, Angle heading) {
Angle a = heading - wind;
return (a < 180) ? a : a - 360;
}
inline double distance_btwn_positions(Position pos1, Position pos2) {
return sqrt((pos1.lat - pos2.lat) * (pos1.lat - pos2.lat)
+ (pos1.lon - pos2.lon) * (pos1.lon - pos2.lon));
}
/* Args: ang_to_wind, ang_to_waypt
* Purpose:
* Returns 0 iff waypt is upwind, defined by CLOSE_HAULED_ANGLE
*/
int is_upwind(Angle wind_ang, Angle ang_to_waypt) {
return ang_btwn_angles(wind_ang, ang_to_waypt) < CLOSE_HAULED_ANGLE;
}
inline double degrees_to_radians(double deg) {
return (deg * M_PI) / 180;
}
inline double radians_to_degrees(double rads) {
return (rads / M_PI) * 180;
}
/* converts angle to standard position */
inline Angle standardize(Angle a) {
if (a < 0)
a += 360;
if (a > 360)
a -= 360;
return a;
}
FILE *open_or_abort(char *filename, char *mode) {
FILE *fp = fopen(filename, mode);
if (fp == NULL) {
perror(filename);
exit(1);
}
return fp;
}