This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMath.hpp
66 lines (53 loc) · 1.36 KB
/
Math.hpp
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
#pragma once
#include <array>
#include <numeric>
#include <concepts>
#include <random>
#include <SFML/System/Vector2.hpp>
namespace Math
{
template <std::integral T>
T random(T min, T max)
{
static std::default_random_engine engine;
std::uniform_int_distribution<T> distribution{min, max};
return distribution(engine);
}
template <std::floating_point T>
T random(T min, T max)
{
static std::default_random_engine engine;
std::uniform_real_distribution<T> distribution{min, max};
return distribution(engine);
}
inline float length(const sf::Vector2f &vec)
{
return std::sqrt(std::pow(vec.x, 2.0f) + std::pow(vec.y, 2.0f));
}
inline sf::Vector2f normalize(const sf::Vector2f &vec)
{
auto len = length(vec);
return {vec.x / len, vec.y / len};
}
inline float distance(const sf::Vector2f &lhs, const sf::Vector2f &rhs)
{
return length(lhs - rhs);
}
template <typename T, size_t N>
class MiddleAverageFilter
{
public:
void push(const T &value)
{
data[id] = value;
id = (id + 1) % N;
}
T getAverage() const
{
return std::reduce(data.begin(), data.end()) / N;
}
private:
std::array<T, N> data;
size_t id = 0u;
};
}