Wildvine Engine
Referencia Doxygen del codigo propio de Wildvine Engine.
Cargando...
Buscando...
Nada coincide
EngineMath.h
Ir a la documentación de este archivo.
1
6/*
7 * MIT License
8 *
9 * Copyright (c) 2024 Roberto Charreton
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in all
19 * copies or substantial portions of the Software.
20 *
21 * In addition, any project or software that uses this library or class must include
22 * the following acknowledgment in the credits:
23 *
24 * "This project uses software developed by Roberto Charreton and Attribute Overload."
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33*/
34#pragma once
35namespace EU {
36
37 // Constantes matemáticas
38 constexpr float PI = 3.14159265358979323846f;
39 constexpr float E = 2.71828182845904523536f;
40
47 inline float sqrt(float value) {
48 if (value < 0) {
49 return 0; // Handle negative input gracefully.
50 }
51 float x = value;
52 float y = 1.0f;
53 float epsilon = 0.00001f; // Desired precision.
54 while (x - y > epsilon) {
55 x = (x + y) / 2.0f;
56 y = value / x;
57 }
58 return x;
59 }
60
67 inline float square(float value) {
68 return value * value;
69 }
70
77 inline float cube(float value) {
78 return value * value * value;
79 }
80
88 inline float power(float base, int exponent) {
89 if (exponent == 0) return 1;
90 if (exponent < 0) return 1.0f / power(base, -exponent);
91 float result = 1;
92 while (exponent) {
93 if (exponent % 2 == 1) result *= base;
94 base *= base;
95 exponent /= 2;
96 }
97 return result;
98 }
99
106 inline float abs(float value) {
107 return (value < 0) ? -value : value;
108 }
109
117 inline float EMax(float a, float b) {
118 return (a > b) ? a : b;
119 }
120
128 inline float EMin(float a, float b) {
129 return (a < b) ? a : b;
130 }
131
138 inline float round(float value) {
139 return (value > 0) ? static_cast<int>(value + 0.5f) : static_cast<int>(value - 0.5f);
140 }
141
148 inline float floor(float value) {
149 int intValue = static_cast<int>(value);
150 return (value < intValue) ? intValue - 1 : intValue;
151 }
152
159 inline float ceil(float value) {
160 int intValue = static_cast<int>(value);
161 return (value > intValue) ? intValue + 1 : intValue;
162 }
163
169 inline float fabs(float value) {
170 return value < 0.0f ? -value : value;
171 }
172
173 // Funciones Trigonométricas
179 inline float sin(float angle) {
180 float result = 0.0f;
181 float term = angle;
182 float angle_squared = angle * angle;
183 int n = 1;
184 while (term > 1e-6f || term < -1e-6f) {
185 result += term;
186 term *= -angle_squared / ((2 * n) * (2 * n + 1));
187 ++n;
188 }
189 return result;
190 }
191
197 inline float cos(float angle) {
198 return sin(angle + PI / 2);
199 }
200
206 inline float tan(float angle) {
207 float s = sin(angle);
208 float c = cos(angle);
209 return c != 0.0f ? s / c : 0.0f; // Evita la división por cero
210 }
211
217 inline float asin(float value) {
218 // Aproximación con la serie de Taylor
219 float x = value;
220 float result = x;
221 float term = x;
222 for (int n = 1; n < 10; ++n) {
223 term *= x * x * (2 * n - 1) / ((2 * n + 1) * (2 * n));
224 result += term;
225 }
226 return result;
227 }
228
234 inline float acos(float value) {
235 return PI / 2 - asin(value);
236 }
237
243 inline float atan(float value) {
244 // Aproximación de la función arcotangente
245 float result = 0.0f;
246 float term = value;
247 for (int n = 0; n < 10; ++n) {
248 result += term / (2 * n + 1);
249 term *= -value * value;
250 }
251 return result;
252 }
253
259 inline float sinh(float value) {
260 return (exp(value) - exp(-value)) / 2;
261 }
262
268 inline float cosh(float value) {
269 return (exp(value) + exp(-value)) / 2;
270 }
271
277 inline float tanh(float value) {
278 return sinh(value) / cosh(value);
279 }
280
281 // Conversión entre Radianes y Grados
287 inline float radians(float degrees) {
288 return degrees * PI / 180.0f;
289 }
290
296 inline float degrees(float radians) {
297 return radians * 180.0f / PI;
298 }
299
300 // Funciones Exponenciales y Logarítmicas
306 inline float exp(float value) {
307 float result = 1.0f;
308 float term = 1.0f;
309 for (int n = 1; n < 20; ++n) {
310 term *= value / n;
311 result += term;
312 }
313 return result;
314 }
315
321 inline float log(float value) {
322 // Aproximación usando la serie de Taylor
323 if (value <= 0) return 0;
324 float x = (value - 1) / (value + 1);
325 float x2 = x * x;
326 float result = 0.0f;
327 float term = x;
328 for (int n = 1; n < 20; ++n) {
329 result += term / (2 * n - 1);
330 term *= x2;
331 }
332 return 2 * result;
333 }
334
340 inline float log10(float value) {
341 return log(value) / log(10);
342 }
343
344 // Operaciones de Redondeo Avanzadas
351 inline float mod(float a, float b) {
352 return a - b * static_cast<int>(a / b);
353 }
354
355 // Funciones de Geometría
361 inline float circleArea(float radius) {
362 return PI * radius * radius;
363 }
364
370 inline float circleCircumference(float radius) {
371 return 2 * PI * radius;
372 }
373
380 inline float rectangleArea(float width, float height) {
381 return width * height;
382 }
383
390 inline float rectanglePerimeter(float width, float height) {
391 return 2 * (width + height);
392 }
393
400 inline float triangleArea(float base, float height) {
401 return 0.5f * base * height;
402 }
403
412 inline float distance(float x1, float y1, float x2, float y2) {
413 float dx = x2 - x1;
414 float dy = y2 - y1;
415 return sqrt(dx * dx + dy * dy);
416 }
417
418 // Funciones de Cálculo Numérico
426 inline float lerp(float a, float b, float t) {
427 return a + t * (b - a);
428 }
429
435 inline int factorial(int n) {
436 int result = 1;
437 for (int i = 2; i <= n; ++i) {
438 result *= i;
439 }
440 return result;
441 }
442
450 inline bool approxEqual(float a, float b, float epsilon) {
451 return fabs(a - b) < epsilon;
452 }
453
454}
455
456
Definition Matrix2x2.h:35
float lerp(float a, float b, float t)
Interpola linealmente entre dos valores.
Definition EngineMath.h:426
float log(float value)
Calcula el logaritmo natural de un valor.
Definition EngineMath.h:321
float square(float value)
Calcula el cuadrado de un número.
Definition EngineMath.h:67
float rectangleArea(float width, float height)
Calcula el área de un rectángulo dado su ancho y alto.
Definition EngineMath.h:380
float fabs(float value)
Calcula el valor absoluto de un número flotante.
Definition EngineMath.h:169
float tan(float angle)
Calcula la tangente de un ángulo en radianes.
Definition EngineMath.h:206
float abs(float value)
Calcula el valor absoluto de un número.
Definition EngineMath.h:106
float sqrt(float value)
Computes the square root using the Newton-Raphson method.
Definition EngineMath.h:47
float EMin(float a, float b)
Devuelve el valor mínimo de dos números.
Definition EngineMath.h:128
float circleCircumference(float radius)
Calcula la circunferencia de un círculo dado su radio.
Definition EngineMath.h:370
float radians(float degrees)
Convierte grados a radianes.
Definition EngineMath.h:287
float mod(float a, float b)
Calcula el módulo de dos números.
Definition EngineMath.h:351
float floor(float value)
Trunca un número a su parte entera, redondeando hacia abajo.
Definition EngineMath.h:148
float power(float base, int exponent)
Calcula la potencia de un número base elevado a un exponente usando el método de exponenciación rápida.
Definition EngineMath.h:88
float round(float value)
Redondea un número al entero más cercano.
Definition EngineMath.h:138
float cube(float value)
Calcula el cubo de un número.
Definition EngineMath.h:77
constexpr float E
Definition EngineMath.h:39
float exp(float value)
Calcula la función exponencial e^x.
Definition EngineMath.h:306
float log10(float value)
Calcula el logaritmo en base 10 de un valor.
Definition EngineMath.h:340
float circleArea(float radius)
Calcula el área de un círculo dado su radio.
Definition EngineMath.h:361
int factorial(int n)
Calcula el factorial de un número entero.
Definition EngineMath.h:435
float degrees(float radians)
Convierte radianes a grados.
Definition EngineMath.h:296
float tanh(float value)
Calcula la tangente hiperbólica de un valor.
Definition EngineMath.h:277
float ceil(float value)
Redondea un número hacia arriba al entero más cercano.
Definition EngineMath.h:159
float sinh(float value)
Calcula el seno hiperbólico de un valor.
Definition EngineMath.h:259
float cosh(float value)
Calcula el coseno hiperbólico de un valor.
Definition EngineMath.h:268
float sin(float angle)
Calcula el seno de un ángulo en radianes.
Definition EngineMath.h:179
float triangleArea(float base, float height)
Calcula el área de un triángulo dado su base y altura.
Definition EngineMath.h:400
float cos(float angle)
Calcula el coseno de un ángulo en radianes.
Definition EngineMath.h:197
bool approxEqual(float a, float b, float epsilon)
Verifica si dos valores flotantes son aproximadamente iguales.
Definition EngineMath.h:450
float rectanglePerimeter(float width, float height)
Calcula el perímetro de un rectángulo dado su ancho y alto.
Definition EngineMath.h:390
float distance(float x1, float y1, float x2, float y2)
Calcula la distancia entre dos puntos en un plano bidimensional.
Definition EngineMath.h:412
float atan(float value)
Calcula el arco tangente de un valor.
Definition EngineMath.h:243
float acos(float value)
Calcula el arco coseno de un valor.
Definition EngineMath.h:234
constexpr float PI
Definition EngineMath.h:38
float EMax(float a, float b)
Devuelve el valor máximo de dos números.
Definition EngineMath.h:117
float asin(float value)
Calcula el arco seno de un valor.
Definition EngineMath.h:217