-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdogma.h
143 lines (118 loc) · 2.96 KB
/
dogma.h
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// This is free and unencumbered software released into the public domain.
#pragma once
/**
* @file
*
* Dogma for C.
*
* @see https://github.com/dogmatists/dogma.c
*/
#ifndef __cplusplus
#if __STDC_VERSION__ < 201103L
#error "<dogma.h> requires a C11 or newer compiler (CFLAGS='-std=c11')"
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
#include <errno.h> // for errno, EDOM
#include <stdbool.h> // for bool
#include <stddef.h> // for size_t
#include <stdint.h> // for {u,}int*_t
/**
* @see https://dogma.dev/history
*/
enum {
DOGMA_VERSION_MAJOR = 0,
DOGMA_VERSION_MINOR = 0,
DOGMA_VERSION_PATCH = 0,
};
/** π */
#ifndef M_PI
#define M_PI (3.14159265358979323846)
#endif
/**
* @see https://dogma.dev/Angle
*/
typedef struct Angle {
double radians;
} Angle;
/// Constructs an angle from radians.
static inline Angle AngleFromRadians(const double radians) {
return (Angle){radians};
}
/// Constructs an angle from degrees.
static inline Angle AngleFromDegrees(const double degrees) {
return (Angle){degrees / 180.0 * M_PI};
}
/// Constructs an angle from turns.
static inline Angle AngleFromTurns(const double turns) {
return (Angle){turns * 2 * M_PI};
}
/// The angle in radians.
static inline double AngleToRadians(const Angle angle) {
return angle.radians;
}
/// The angle in degrees.
static inline double AngleToDegrees(const Angle angle) {
return angle.radians / M_PI * 180.0;
}
/// The angle in turns.
static inline double AngleToTurns(const Angle angle) {
return angle.radians / (2 * M_PI);
}
/**
* @see https://dogma.dev/Latitude
*/
typedef struct Latitude {
Angle angle;
} Latitude;
enum {
LATITUDE_MIN_DEGREES = -90,
LATITUDE_MAX_DEGREES = 90,
};
/// Constructs a latitude.
#define Latitude(degrees) LatitudeFromDegrees(degrees)
/// Constructs a latitude.
static inline Latitude LatitudeFromDegrees(const double degrees) {
if (degrees < LATITUDE_MIN_DEGREES) {
return errno = EDOM, (Latitude){0};
}
if (degrees > LATITUDE_MAX_DEGREES) {
return errno = EDOM, (Latitude){0};
}
return (Latitude){AngleFromDegrees(degrees)};
}
/// The latitude in degrees.
static inline double LatitudeToDegrees(const Latitude latitude) {
return AngleToDegrees(latitude.angle);
}
/**
* @see https://dogma.dev/Longitude
*/
typedef struct Longitude {
Angle angle;
} Longitude;
enum {
LONGITUDE_MIN_DEGREES = -180,
LONGITUDE_MAX_DEGREES = 180,
};
/// Constructs a longitude.
#define Longitude(degrees) LongitudeFromDegrees(degrees)
/// Constructs a longitude.
static inline Longitude LongitudeFromDegrees(const double degrees) {
if (degrees < LONGITUDE_MIN_DEGREES) {
return errno = EDOM, (Longitude){0};
}
if (degrees > LONGITUDE_MAX_DEGREES) {
return errno = EDOM, (Longitude){0};
}
return (Longitude){AngleFromDegrees(degrees)};
}
/// The longitude in degrees.
static inline double LongitudeToDegrees(const Longitude longitude) {
return AngleToDegrees(longitude.angle);
}
#ifdef __cplusplus
} // extern "C"
#endif