Repository navigation
Expand file tree
/
Copy pathVec2.h
More file actions
58 lines (46 loc) · 1.35 KB
/
Copy pathVec2.h
File metadata and controls
58 lines (46 loc) · 1.35 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
#ifndef VEC2_H
#define VEC2_H
#include <cmath>
#include <iostream>
struct Vec2
{
float components[2] = { 0.0, 0.0 };
float operator[](int i) const { return components[i]; }
};
inline float dot(const Vec2& v0, const Vec2& v1)
{
return v0.components[0] * v1.components[0]
+ v0.components[1] * v1.components[1];
}
inline float squaredLength(const Vec2& v)
{
return v.components[0] * v.components[0]
+ v.components[1] * v.components[1];
}
inline float length(const Vec2& p)
{
return std::sqrt(squaredLength(p));
}
inline Vec2 operator-(const Vec2 &v) { return {-v.components[0], -v.components[1] }; }
inline Vec2 operator+(const Vec2& v0, const Vec2& v1) {
return {{ v0.components[0] + v1.components[0], v0.components[1] + v1.components[1] }};
}
inline Vec2 operator-(const Vec2& v0, const Vec2& v1) {
return {{ v0.components[0] - v1.components[0], v0.components[1] - v1.components[1]}};
}
inline Vec2 operator*(float t, const Vec2& v) {
return {{ t*v.components[0], t*v.components[1] }};
}
inline Vec2 normalize(const Vec2& p)
{
return 1/length(p) * p;
}
inline Vec2 perpendicular(const Vec2& p)
{
return normalize({{ -p.components[1], p.components[0] }});
}
inline std::ostream& operator<<(std::ostream& out, const Vec2& v)
{
return out << v.components[0] << " " << v.components[1];
}
#endif // VEC2_H