-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.h
86 lines (73 loc) · 1.7 KB
/
utilities.h
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
#ifndef UTILITIES_H
#define UTILITIES_H
#include <iostream>
#include <sstream>
#include <string>
#include <stdexcept>
#include "vectors.h"
#include <iomanip>
class BadConversion : public std::runtime_error {
public:
BadConversion(std::string const& s)
: std::runtime_error(s)
{ }
};
inline std::string stringify(double x)
{
std::ostringstream o;
if(!(o <<x))
throw BadConversion("stringify(double)");
return o.str();
}
inline std::string stringify(int x)
{
std::ostringstream o;
if(!(o <<x))
throw BadConversion("stringify(int)");
return o.str();
}
inline std::string stringify(float x)
{
std::ostringstream o;
// o << std::fixed;
if(!(o <<x))
throw BadConversion("stringify(float)");
return o.str();
}
inline std::string stringify(Vector3 x)
{
std::ostringstream o;
if(!(o << "(" << x.x << ", " << x.y << ", " << x.z << ")"))
throw BadConversion("stringify(Vector3)");
return o.str();
}
inline std::string stringify(Vector2 x)
{
std::ostringstream o;
if(!(o << "(" << x.x << ", " << x.y << ")"))
throw BadConversion("stringify(Vector2)");
return o.str();
}
inline float Clamp(float min, float max, float val) {
if(max < min) {
float tmp = min;
min = max;
max = tmp;
}
if(val < min)
val = min;
if(val > max)
val = max;
return val;
}
inline float Lerp(float v1, float v2, float amt) {
float val = v1 + ((v2 - v1) * amt);
val = Clamp(v1, v2, val);
return val;
}
inline int Lerp(int v1, int v2, float amt) {
float val = v1 + ((v2 - v1) * amt);
val = Clamp(v1, v2, val);
return (int)val;
}
#endif