Wizard Engine
2D cross-platform game engine built around SDL2
 
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
1/*
2 Wizard Engine
3 Copyright (C) 2023-2024 Zana Domán
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
28#ifndef WIZARD_ENGINE_MATH_HPP
29#define WIZARD_ENGINE_MATH_HPP
30
32
33namespace wze {
37class math final {
38 public:
42 math() = delete;
43
47 [[nodiscard]] static constexpr float epsilon() {
48 // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
49 return .01;
50 }
51
55 [[nodiscard]] static constexpr float pi() {
56 // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
57 return 3.1415927;
58 }
59
65 [[nodiscard]] static constexpr float to_radians(float degrees) {
66 // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
67 return degrees * pi() / 180;
68 }
69
75 [[nodiscard]] static constexpr float to_degrees(float radians) {
76 // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
77 return radians * 180 / pi();
78 }
79
86 [[nodiscard]] static constexpr std::pair<float, float> normal(float x,
87 float y) {
88 return {y, -x};
89 }
90
97 [[nodiscard]] static float length(float x, float y);
98
105 [[nodiscard]] static float angle(float x, float y);
106
113 [[nodiscard]] static std::pair<float, float> normalize(float x, float y);
114
121 [[nodiscard]] static float move_x(float length, float angle);
122
129 [[nodiscard]] static float move_y(float length, float angle);
130
137 [[nodiscard]] static std::array<float, 4>
138 transformation_matrix(float angle, float scale);
139
147 [[nodiscard]] static constexpr float
148 transform_x(float x, float y,
149 std::array<float, 4> const& transformation_matrix) {
150 return (x * transformation_matrix.at(0)) +
151 (y * transformation_matrix.at(1));
152 }
153
161 [[nodiscard]] static constexpr float
162 transform_y(float x, float y,
163 std::array<float, 4> const& transformation_matrix) {
164 return (x * transformation_matrix.at(2)) +
165 (y * transformation_matrix.at(3));
166 }
167
175 template <typename T>
176 [[nodiscard]] static T random(T minimum = std::numeric_limits<T>::lowest(),
177 T maximum = std::numeric_limits<T>::max()) {
178 static_assert(!std::is_same_v<T, bool>);
179 if constexpr (std::is_integral_v<T>) {
180 return std::uniform_int_distribution<T>{minimum,
181 maximum}(_mt19937_64);
182 } else if constexpr (std::is_floating_point_v<T>) {
183 return std::uniform_real_distribution<T>{minimum,
184 maximum}(_mt19937_64);
185 }
186 }
187
194 template <typename T>
195 [[nodiscard]] static bool random(float probability = (float)true / 2) {
196 static_assert(std::is_same_v<T, bool>);
197 return std::bernoulli_distribution{(double)probability}(_mt19937_64);
198 }
199
200 private:
201 static std::mt19937_64 _mt19937_64;
202};
203} /* namespace wze */
204
205#endif /* WIZARD_ENGINE_MATH_HPP */
Math modul.
Definition math.hpp:37
static constexpr float to_radians(float degrees)
Converts degrees to radians.
Definition math.hpp:65
static float angle(float x, float y)
Returns the angle of a vector.
Definition math.cpp:37
static constexpr float transform_y(float x, float y, std::array< float, 4 > const &transformation_matrix)
Transforms the y component of a vector.
Definition math.hpp:162
static bool random(float probability=(float) true/2)
Returns a random boolean value.
Definition math.hpp:195
math()=delete
Deleted explicit constructor.
static constexpr std::pair< float, float > normal(float x, float y)
Returns the normal vector of a vector.
Definition math.hpp:86
static std::array< float, 4 > transformation_matrix(float angle, float scale)
Creates a transformation matrix.
Definition math.cpp:57
static float move_y(float length, float angle)
Moves the y component of a vector.
Definition math.cpp:53
static constexpr float to_degrees(float radians)
Converts radians to degrees.
Definition math.hpp:75
static std::pair< float, float > normalize(float x, float y)
Normalizes a vector.
Definition math.cpp:41
static float move_x(float length, float angle)
Moves the x component of a vector.
Definition math.cpp:49
static constexpr float pi()
Single precision PI.
Definition math.hpp:55
static float length(float x, float y)
Returns the length of a vector.
Definition math.cpp:33
static constexpr float epsilon()
Single precision epsilon.
Definition math.hpp:47
static constexpr float transform_x(float x, float y, std::array< float, 4 > const &transformation_matrix)
Transforms the x component of a vector.
Definition math.hpp:148
static T random(T minimum=std::numeric_limits< T >::lowest(), T maximum=std::numeric_limits< T >::max())
Returns a random numeric value from an interval.
Definition math.hpp:176
Export header of the Wizard Engine.
Wizard Engine.