-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMathUtils.cpp
95 lines (72 loc) · 1.82 KB
/
MathUtils.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
94
95
#include "MathUtils.h"
json_t* json_vector2(const Vector2& v)
{
json_t* root = json_array();
// X
json_t* jsonX = json_real(v.X());
json_array_append(root, jsonX);
json_decref(jsonX);
// Y
json_t* jsonY = json_real(v.Y());
json_array_append(root, jsonY);
json_decref(jsonY);
return root;
}
bool json_is_vector2(const json_t *root)
{
if (!json_is_array(root))
return false;
if (json_array_size(root) != 2)
return false;
for (int i=0; i<2; ++i)
{
if (!json_is_real(json_array_get(root, i)))
return false;
}
return true;
}
Vector2 json_vector2_value(const json_t *root)
{
Vector2 vec;
// Read X
json_t* jsonX = json_array_get(root, 0);
vec.X((float)json_real_value(jsonX));
// Read Y
json_t* jsonY = json_array_get(root, 1);
vec.Y((float)json_real_value(jsonY));
return vec;
}
float min(float x1, float x2)
{
return (x1 < x2) ? x1 : x2;
}
float max(float x1, float x2)
{
return (x1 > x2) ? x1 : x2;
}
float lerp(float minVal, float maxVal, float term)
{
return (maxVal - minVal) * term + minVal;
}
float clamp(float val, float minVal, float maxVal)
{
return max(min(val, maxVal), minVal);
}
Vector2 lerp(const Vector2& minVal, const Vector2& maxVal, float term)
{
Vector2 result;
result.m_x = lerp(minVal.m_x, maxVal.m_x, term);
result.m_y = lerp(minVal.m_y, maxVal.m_y, term);
return result;
}
Vector2 lerp4(const Vector2& topLeft, const Vector2& topRight, const Vector2& bottomRight, const Vector2& bottomLeft, float termX, float termY)
{
Vector2 result;
float topX = lerp(topLeft.m_x, topRight.m_x, termX);
float bottomX = lerp(bottomLeft.m_x, bottomRight.m_x, termX);
result.m_x = lerp(topX, bottomX, termY);
float leftY = lerp(topLeft.m_y, bottomLeft.m_y, termY);
float rightY = lerp(topRight.m_y, bottomRight.m_y, termY);
result.m_y = lerp(leftY, rightY, termX);
return result;
}