ToyMathUtils.h
4.01 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// ToyMathUtils.h
// SteveMaggieCpp
//
// Created by Katarzyna Kalinowska-Górska on 10.05.2017.
//
//
#ifndef ToyMathUtils_h
#define ToyMathUtils_h
#include <stdio.h>
#include <numeric>
#include <deque>
#include <random>
#include "cocos2d.h"
class ToyMathUtils {
public:
// Returns a random number between min (inclusive) and max (exclusive)
static double getRandom(double min, double max);
// Returns a random integer between min (included) and max (excluded)
static int getRandomInt(int min, int max);
static cocos2d::Color4F getRandomOpaqueColor();
static int signum(float expression){
if(expression < 0){
return -1;
} else if(expression > 0){
return 1;
}
return 0;
};
template<class T>
static T getRandomElement(std::vector<T>& arr){
auto size = arr.size();
if(size > 0){
int randomIndex = ToyMathUtils::getRandomInt(0, (int)size-1);
return arr[randomIndex];
}
throw "Empty array!";
}
template<class T>
static void shuffleArray(std::vector<T>& arr)
{
int currentIndex = (int)arr.size(), randomIndex;
T temporaryValue;
// While there remain elements to shuffle...
while (0 != currentIndex) {
// Pick a remaining element...
randomIndex = ToyMathUtils::getRandomInt(0, currentIndex-1);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = arr[currentIndex];
arr[currentIndex] = arr[randomIndex];
arr[randomIndex] = temporaryValue;
}
}
template<class T>
static void multiplyArray(std::vector<T>& arr, T byValue)
{
for(int i = 0; i < arr.size(); ++i){
arr[i] *= byValue;
}
}
template<class T>
static void multiplyPointArray(std::vector<cocos2d::Point>& arr, T byValue)
{
for(int i = 0; i < arr.size(); ++i){
arr[i].x *= byValue;
arr[i].y *= byValue;
}
}
template<class T>
static void multiplyRectPositionsArray(std::vector<cocos2d::Rect>& arr, T byValue)
{
for(int i = 0; i < arr.size(); ++i){
arr[i].origin.x *= byValue;
arr[i].origin.y *= byValue;
arr[i].size.width *= byValue;
arr[i].size.height *= byValue;
}
}
template<class T>
static void multiplyRectPosition(cocos2d::Rect& rect, T byValue)
{
rect.origin.x *= byValue;
rect.origin.y *= byValue;
rect.size.width *= byValue;
rect.size.height *= byValue;
}
template<class T>
static void multiplyPoint(cocos2d::Point& point, T byValue)
{
point.x *= byValue;
point.y *= byValue;
}
//NOT WORKING, WHY? Container size is always 0.
// template<class T, class Container>
// static void averageValue(const Container& valueArray, T& averageValue){
// if(valueArray.size() == 0){
// averageValue = 0;
// } else {
// averageValue = std::accumulate(valueArray.begin(), valueArray.end(), 0) / valueArray.size();
// }
// }
//temp
static void averageValue(const std::deque<float>& valueArray, float& averageValue){
if(valueArray.size() == 0){
averageValue = 0;
} else {
float size = valueArray.size();
averageValue = std::accumulate(valueArray.begin(), valueArray.end(), 0) / size;
}
}
// static void drawRoundedCorneredRect(cocos2d::DrawNode*, cocos2d::Rect rect, float cornerRadius, cocos2d::Color4F&);
private:
static std::mt19937 _getRNG();
};
#endif /* ToyMathUtils_h */