SGL
gmath.h
1 /*
2  * File: gmath.h
3  * -------------
4  * This file exports several functions for working with graphical
5  * geometry along with the mathematical constants <code>PI</code>
6  * and <code>E</code>.
7  *
8  * @version 2021/04/09
9  * - added sgl::math namespace
10  * @version 2018/11/22
11  * - added headless mode support
12  * - alphabetized methods
13  * @version 2018/09/25
14  * - added doc comments for new documentation generation
15  * @version 2017/12/12
16  * - added floatingPointEqual(a, b, tolerance)
17  * @version 2016/10/14
18  * - added floatingPointEqual method for comparing floats and doubles
19  */
20 
21 
22 #ifndef _gmath_h
23 #define _gmath_h
24 
25 #include <cmath>
26 #include <limits>
27 
28 namespace sgl {
29 
30 struct GPoint;
31 
32 namespace math {
33 
38 extern const double PI;
39 
45 extern const double E;
46 
51 double cosDegrees(double angle);
52 
53 
60 int countDigits(int n, int base = 10);
61 
71 template<typename T>
72 bool floatingPointEqual(T f1, T f2, T tolerance) {
73  return (std::fabs(f1 - f2) <= tolerance);
74 }
75 
84 template<typename T>
85 bool floatingPointEqual(T f1, T f2) {
86  return floatingPointEqual(f1, f2, /* tolerance */ (T) std::numeric_limits<T>::epsilon() * std::fmax(fabs(f1), fabs(f2)));
87 }
88 
97 template<typename T>
98 bool floatingPointEqual(T f1, int f2) {
99  return floatingPointEqual(f1, (T) f2);
100 }
101 
110 template<typename T>
111 bool floatingPointEqual(int f1, T f2) {
112  return floatingPointEqual((T) f1, f2);
113 }
114 
123 template<typename T>
124 bool floatingPointEqual(T f1, long int f2) {
125  return floatingPointEqual(f1, (T) f2);
126 }
127 
136 template<typename T>
137 bool floatingPointEqual(long int f1, T f2) {
138  return floatingPointEqual((T) f1, f2);
139 }
140 
145 double sinDegrees(double angle);
146 
151 double tanDegrees(double angle);
152 
156 double toDegrees(double radians);
157 
161 double toRadians(double degrees);
162 
169 double vectorAngle(double x, double y);
170 
177 double vectorAngle(const ::sgl::GPoint& pt);
178 
182 double vectorDistance(double x, double y);
183 
187 double vectorDistance(const ::sgl::GPoint& pt);
188 
189 } // namespace math
190 } // namespace sgl
191 
192 #endif // _gmath_h
double vectorAngle(double x, double y)
Returns the angle in degrees from the origin to the specified point.
Definition: gmath.cpp:66
bool floatingPointEqual(T f1, T f2, T tolerance)
Returns true if the two given floating-point numbers are "equal" to each other, within a given tolera...
Definition: gmath.h:72
Definition: console.h:45
double tanDegrees(double angle)
Returns the trigonometric tangent of angle, which is expressed in degrees.
Definition: gmath.cpp:54
double toRadians(double degrees)
Converts an angle from degrees to radians.
Definition: gmath.cpp:62
int countDigits(int n, int base)
Definition: gmath.cpp:33
double vectorDistance(double x, double y)
Computes the distance between the origin and the specified point.
Definition: gmath.cpp:75
const double PI
The mathematical constant pi, which is the ratio of the circumference of a circle to its diameter...
Definition: gmath.h:38
double cosDegrees(double angle)
Returns the trigonometric cosine of angle, which is expressed in degrees.
Definition: gmath.cpp:29
double sinDegrees(double angle)
Returns the trigonometric sine of angle, which is expressed in degrees.
Definition: gmath.cpp:50
double toDegrees(double radians)
Converts an angle from radians to degrees.
Definition: gmath.cpp:58
const double E
Constant: E The mathematical constant e, which is the base of natural logarithms. ...
Definition: gmath.h:45