-
Notifications
You must be signed in to change notification settings - Fork 0
/
Math.cpp
93 lines (79 loc) · 2.12 KB
/
Math.cpp
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
#include <math.h>
#include <stdlib.h>
#include "Math.hpp"
namespace math
{
Math::Math()
{
}
Math::~Math()
{
}
/**
* Implementation of Linear Interpolation between 2 values.
*
* @param a The starting value
* @param b The ending value
* @param dist A value between 0.0 and 1.0; will return a value between a and b
*
* @return A value between a and b, based on the dist value given
*/
float Math::linearInterp(float a, float b, float dist)
{
return a * (1 - dist) + b * dist;
}
/**
* Implementation of Cosine Interpolation between 2 values. Basically, gives a
* smoother result compared to the Linear Interpolation function, but requires a
* a little more computation.
*
* @param a The starting value
* @param b The ending value
* @param dist A value between 0.0 and 1.0; will return a value between a and b
*
* @return A value between a and b, based on the dist value given
*/
float Math::cosineInterp(float a, float b, float dist)
{
float ft = dist * 3.1415927f;
float f = (1.0f - cos(ft)) * 0.5f;
return a * (1.0f - f) + b * f;
}
/**
* Implementation of Cubic Interpolation between 2 values. Basically, gives a
* smoother result compared to the Cosine Interpolation function, but requires a
* a bunch more computation.
*
* @param a0 The value before the starting value
* @param a1 The starting value
* @param b1 The ending value
* @param b2 The value after the starting value
* @param dist A value between 0.0 and 1.0; will return a value between a1 and b1
*
* @return A value between a and b, based on the dist value given
*/
float Math::cubicInterp(float a0, float a1, float b1, float b2, float dist)
{
float P = (b2 - b1) - (a0 - a1);
float Q = (a0 - a1) - P;
float R = b1 - a0;
float S = a1;
// Px^3 + Qx^2 + Rx + S;
return P * (dist * dist * dist) + Q * (dist * dist) + R * (dist) + S;
}
/**
*
*/
int Math::random()
{
static unsigned int retVal = 5323;
retVal = (8253729 * retVal + 2396402);
// returns value between 0 and 32767
return retVal % RANDOM_MAX;
/*
int m_z = 36969 * (m_z & 65535) + (m_z >> 16);
int m_w = 18000 * (m_w & 65535) + (m_w >> 16);
return (m_z << 16) + m_w;
*/
}
}