diff options
Diffstat (limited to 'src/vector.h')
-rw-r--r-- | src/vector.h | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/vector.h b/src/vector.h new file mode 100644 index 0000000..9355dd5 --- /dev/null +++ b/src/vector.h | |||
@@ -0,0 +1,113 @@ | |||
1 | #ifndef COORDINATES_H_A45D34FB | ||
2 | #define COORDINATES_H_A45D34FB | ||
3 | |||
4 | template <typename T> | ||
5 | class vec2 { | ||
6 | public: | ||
7 | |||
8 | T coords[2]; | ||
9 | |||
10 | vec2() : coords{0, 0} | ||
11 | { | ||
12 | } | ||
13 | |||
14 | vec2(T x, T y) : coords{x, y} | ||
15 | { | ||
16 | } | ||
17 | |||
18 | inline T& x() | ||
19 | { | ||
20 | return coords[0]; | ||
21 | } | ||
22 | |||
23 | inline const T& x() const | ||
24 | { | ||
25 | return coords[0]; | ||
26 | } | ||
27 | |||
28 | inline T& w() | ||
29 | { | ||
30 | return coords[0]; | ||
31 | } | ||
32 | |||
33 | inline const T& w() const | ||
34 | { | ||
35 | return coords[0]; | ||
36 | } | ||
37 | |||
38 | inline T& y() | ||
39 | { | ||
40 | return coords[1]; | ||
41 | } | ||
42 | |||
43 | inline const T& y() const | ||
44 | { | ||
45 | return coords[1]; | ||
46 | } | ||
47 | |||
48 | inline T& h() | ||
49 | { | ||
50 | return coords[1]; | ||
51 | } | ||
52 | |||
53 | inline const T& h() const | ||
54 | { | ||
55 | return coords[1]; | ||
56 | } | ||
57 | |||
58 | template <typename R> | ||
59 | operator vec2<R>() const | ||
60 | { | ||
61 | return vec2<R>(x(), y()); | ||
62 | } | ||
63 | |||
64 | vec2 operator+(const vec2& other) const | ||
65 | { | ||
66 | return vec2(x() + other.x(), y() + other.y()); | ||
67 | } | ||
68 | |||
69 | vec2& operator+=(const vec2& other) | ||
70 | { | ||
71 | x() += other.x(); | ||
72 | y() += other.y(); | ||
73 | |||
74 | return *this; | ||
75 | } | ||
76 | |||
77 | vec2 operator-(const vec2& other) const | ||
78 | { | ||
79 | return vec2(x() - other.x(), y() - other.y()); | ||
80 | } | ||
81 | |||
82 | vec2 operator-=(const vec2& other) | ||
83 | { | ||
84 | x() -= other.x(); | ||
85 | y() -= other.y(); | ||
86 | |||
87 | return *this; | ||
88 | } | ||
89 | |||
90 | vec2 operator-() const | ||
91 | { | ||
92 | return vec2(-x(), -y()); | ||
93 | } | ||
94 | |||
95 | vec2 operator*(T s) const | ||
96 | { | ||
97 | return vec2(x() * s, y() * s); | ||
98 | } | ||
99 | |||
100 | vec2& operator*=(T s) | ||
101 | { | ||
102 | x() *= s; | ||
103 | y() *= s; | ||
104 | |||
105 | return *this; | ||
106 | } | ||
107 | |||
108 | }; | ||
109 | |||
110 | using vec2d = vec2<double>; | ||
111 | using vec2i = vec2<int>; | ||
112 | |||
113 | #endif /* end of include guard: COORDINATES_H_A45D34FB */ | ||