-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2d.h
83 lines (72 loc) · 3.21 KB
/
v2d.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
#pragma once
class v2d {
private:
float inv_sqrt(float x)
{ union { float f; uint32_t u; } y = {x};
y.u = 0x5F1FFFF9ul - (y.u >> 1);
return 0.703952253f * y.f * (2.38924456f - x * y.f * y.f);
}
float randMapped() {
return(static_cast <float> (rand()) / static_cast <float> (RAND_MAX));
}
public:
//Create an empty vector
v2d() { x = 0.0f; y = 0.0f; }
//Create a vector from _x and _y
v2d(const float _x, const float _y) : x(_x), y(_y) {}
//Set vector to _x and _y
v2d set(float _x, float _y) { x = _x; y = _y; return(*this); }
//Randomize this vector
v2d randomize(float scale = 1) { float r = randMapped() * 2 * 3.141592f; set(cos(r) * scale, sin(r) * scale); return(*this); }
//Set vector to zero
void zero() { x = 0.0f; y = 0.0f; }
v2d operator + (const v2d& r) { return v2d(this->x + r.x, this->y + r.y); }
v2d operator + (const float r) { return v2d(this->x + r, this->y + r); }
v2d operator += (const v2d& r) { this->x += r.x; this->y += r.y; return(*this); }
v2d operator += (const float r) { this->x += r; this->y += r; return(*this); }
v2d operator - (const v2d& r) { return v2d(this->x - r.x, this->y - r.y); }
v2d operator - (const float r) { return v2d(this->x - r, this->y - r); }
v2d operator -= (const v2d& r) { this->x -= r.x; this->y -= r.y; return(*this); }
v2d operator -= (const float r) { this->x -= r; this->y -= r; return(*this); }
v2d operator * (const v2d& r) { return v2d(this->x * r.x, this->y * r.y); }
v2d operator * (const float r) { return v2d(this->x * r, this->y * r); }
v2d operator *= (const v2d& r) { this->x *= r.x; this->y *= r.y; return(*this); }
v2d operator *= (const float r) { this->x *= r; this->y *= r; return(*this); }
v2d operator / (const v2d& r) { return v2d(this->x / r.x, this->y / r.y); }
v2d operator / (const float r) { return v2d(this->x / r, this->y / r); }
v2d operator /= (const v2d& r) { this->x /= r.x; this->y /= r.y; return(*this); }
v2d operator /= (const float r) { this->x /= r; this->y /= r; return(*this); }
bool operator > (v2d& r) { return(sqrLen() > r.sqrLen());}
bool operator < (v2d& r) { return(sqrLen() < r.sqrLen());}
bool operator == (v2d& r) { return(sqrLen() == r.sqrLen());}
bool operator != (v2d& r) { return(sqrLen() != r.sqrLen());}
v2d norm() { *this = *this * invLen(); return(*this); }
v2d setLen(float mag) {
this->operator*=(invLen() * mag);
return(*this);
}
float len() {
return sqrtf((x*x) + (y*y));
}
float invLen() {
return inv_sqrt(sqrLen());
}
float sqrLen() { return(x * x + y * y); }
float sqrDist(const v2d& b) { v2d a = *this - b; return(a.x * a.x + a.y * a.y); }
v2d limit(float b)
{
if (sqrLen() <= b * b) {
return(*this);
}
setLen(b);
return(*this);
}
v2d rotate(float rad){
float cs = cos(rad), sn = sin(rad);
float px = x * cs - y * sn;
float py = x * sn + y * cs;
return v2d(px, py);
}
float x;
float y;
};