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