-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdogma.hpp
More file actions
139 lines (113 loc) · 2.72 KB
/
dogma.hpp
File metadata and controls
139 lines (113 loc) · 2.72 KB
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
// This is free and unencumbered software released into the public domain.
#pragma once
/**
* @file
*
* Dogma for C++.
*
* @see https://github.com/dogmatists/dogma.cpp
*/
#ifndef __cplusplus
#error "<dogma.hpp> requires a C++ compiler"
#endif
#if __cplusplus < 201703L
#error "<dogma.hpp> requires a C++17 or newer compiler (CXXFLAGS='-std=c++17')"
#endif
#include <cerrno> // for errno, EDOM
/** */
namespace dogma {
namespace version {}
} // namespace dogma
/**
* @see @see https://dogma.dev/history
*/
namespace dogma::version {
static inline constexpr int major = 0;
static inline constexpr int minor = 0;
static inline constexpr int patch = 0;
} // namespace dogma::version
/** π */
#ifndef M_PI
#define M_PI (3.14159265358979323846)
#endif
namespace dogma {
struct Angle;
struct Latitude;
struct Longitude;
} // namespace dogma
/**
* @see https://dogma.dev/Angle
*/
struct dogma::Angle {
double _radians;
/// Constructs an angle from radians.
static inline Angle from_radians(const double radians) {
return Angle{radians};
}
/// Constructs an angle from degrees.
static inline Angle from_degrees(const double degrees) {
return Angle{degrees / 180.0 * M_PI};
}
/// Constructs an angle from turns.
static inline Angle from_turns(const double turns) {
return Angle{turns * 2 * M_PI};
}
/// The angle in radians.
inline double radians() const {
return _radians;
}
/// The angle in degrees.
inline double degrees() const {
return _radians / M_PI * 180.0;
}
/// The angle in turns.
inline double turns() const {
return _radians / (2 * M_PI);
}
};
/**
* @see https://dogma.dev/Latitude
*/
struct dogma::Latitude {
Angle _angle;
Latitude() : _angle{0} {}
Latitude(const double degrees) : _angle{Angle::from_degrees(degrees)} {
if (degrees < min_degrees) {
errno = EDOM;
_angle = Angle{0};
}
if (degrees > max_degrees) {
errno = EDOM;
_angle = Angle{0};
}
}
static inline constexpr int min_degrees = -90;
static inline constexpr int max_degrees = 90;
/// The latitude in degrees.
inline double degrees() const {
return _angle.degrees();
}
};
/**
* @see https://dogma.dev/Longitude
*/
struct dogma::Longitude {
Angle _angle;
Longitude() : _angle{0} {}
Longitude(const double degrees) : _angle{Angle::from_degrees(degrees)} {
if (degrees < min_degrees) {
errno = EDOM;
_angle = Angle{0};
}
if (degrees > max_degrees) {
errno = EDOM;
_angle = Angle{0};
}
}
static inline constexpr int min_degrees = -180;
static inline constexpr int max_degrees = 180;
/// The longitude in degrees.
inline double degrees() const {
return _angle.degrees();
}
};