| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #pragma once |
| 4 | |
| 5 | #include "Prerequisites/BsPrerequisitesUtil.h" |
| 6 | #include "BsVector2.h" |
| 7 | |
| 8 | namespace bs |
| 9 | { |
| 10 | /** @addtogroup Math |
| 11 | * @{ |
| 12 | */ |
| 13 | |
| 14 | /** A two dimensional vector with integer coordinates.*/ |
| 15 | struct BS_UTILITY_EXPORT Vector2I |
| 16 | { |
| 17 | INT32 x = 0; |
| 18 | INT32 y = 0; |
| 19 | |
| 20 | constexpr Vector2I() = default; |
| 21 | |
| 22 | constexpr Vector2I(INT32 _x, INT32 _y ) |
| 23 | :x(_x), y(_y) |
| 24 | { } |
| 25 | |
| 26 | constexpr explicit Vector2I(int val) |
| 27 | :x(val), y(val) |
| 28 | { } |
| 29 | |
| 30 | /** Exchange the contents of this vector with another. */ |
| 31 | void swap(Vector2I& other) |
| 32 | { |
| 33 | std::swap(x, other.x); |
| 34 | std::swap(y, other.y); |
| 35 | } |
| 36 | |
| 37 | /** Returns the manhattan distance between this and another point. */ |
| 38 | UINT32 manhattanDist(const Vector2I& other) const |
| 39 | { |
| 40 | return (UINT32)std::abs(other.x - x) + (UINT32)std::abs(other.y - y); |
| 41 | } |
| 42 | |
| 43 | INT32 operator[] (size_t i) const |
| 44 | { |
| 45 | assert(i < 2); |
| 46 | |
| 47 | return *(&x+i); |
| 48 | } |
| 49 | |
| 50 | INT32& operator[] (size_t i) |
| 51 | { |
| 52 | assert(i < 2); |
| 53 | |
| 54 | return *(&x+i); |
| 55 | } |
| 56 | |
| 57 | Vector2I& operator= (int val) |
| 58 | { |
| 59 | x = val; |
| 60 | y = val; |
| 61 | |
| 62 | return *this; |
| 63 | } |
| 64 | |
| 65 | bool operator== (const Vector2I& rhs) const |
| 66 | { |
| 67 | return (x == rhs.x && y == rhs.y); |
| 68 | } |
| 69 | |
| 70 | bool operator!= (const Vector2I& rhs) const |
| 71 | { |
| 72 | return (x != rhs.x || y != rhs.y); |
| 73 | } |
| 74 | |
| 75 | Vector2I operator+ (const Vector2I& rhs) const |
| 76 | { |
| 77 | return Vector2I(x + rhs.x, y + rhs.y); |
| 78 | } |
| 79 | |
| 80 | Vector2I operator- (const Vector2I& rhs) const |
| 81 | { |
| 82 | return Vector2I(x - rhs.x, y - rhs.y); |
| 83 | } |
| 84 | |
| 85 | Vector2I operator* (int val) const |
| 86 | { |
| 87 | return Vector2I(x * val, y * val); |
| 88 | } |
| 89 | |
| 90 | Vector2 operator* (float val) const |
| 91 | { |
| 92 | return Vector2(x * val, y * val); |
| 93 | } |
| 94 | |
| 95 | Vector2I operator* (const Vector2I& rhs) const |
| 96 | { |
| 97 | return Vector2I(x * rhs.x, y * rhs.y); |
| 98 | } |
| 99 | |
| 100 | Vector2I operator/ (int val) const |
| 101 | { |
| 102 | assert(val != 0); |
| 103 | |
| 104 | return Vector2I(x / val, y / val); |
| 105 | } |
| 106 | |
| 107 | Vector2 operator/ (float val) const |
| 108 | { |
| 109 | assert(val != 0); |
| 110 | |
| 111 | return Vector2(x / val, y / val); |
| 112 | } |
| 113 | |
| 114 | Vector2I operator/ (const Vector2I& rhs) const |
| 115 | { |
| 116 | return Vector2I(x / rhs.x, y / rhs.y); |
| 117 | } |
| 118 | |
| 119 | const Vector2I& operator+ () const |
| 120 | { |
| 121 | return *this; |
| 122 | } |
| 123 | |
| 124 | Vector2I operator- () const |
| 125 | { |
| 126 | return Vector2I(-x, -y); |
| 127 | } |
| 128 | |
| 129 | friend Vector2I operator* (int lhs, const Vector2I& rhs) |
| 130 | { |
| 131 | return Vector2I(lhs * rhs.x, lhs * rhs.y); |
| 132 | } |
| 133 | |
| 134 | friend Vector2I operator/ (int lhs, const Vector2I& rhs) |
| 135 | { |
| 136 | return Vector2I(lhs / rhs.x, lhs / rhs.y); |
| 137 | } |
| 138 | |
| 139 | Vector2I& operator+= (const Vector2I& rhs) |
| 140 | { |
| 141 | x += rhs.x; |
| 142 | y += rhs.y; |
| 143 | |
| 144 | return *this; |
| 145 | } |
| 146 | |
| 147 | Vector2I& operator-= (const Vector2I& rhs) |
| 148 | { |
| 149 | x -= rhs.x; |
| 150 | y -= rhs.y; |
| 151 | |
| 152 | return *this; |
| 153 | } |
| 154 | |
| 155 | Vector2I& operator*= (INT32 val) |
| 156 | { |
| 157 | x *= val; |
| 158 | y *= val; |
| 159 | |
| 160 | return *this; |
| 161 | } |
| 162 | |
| 163 | Vector2I& operator*= (const Vector2I& rhs) |
| 164 | { |
| 165 | x *= rhs.x; |
| 166 | y *= rhs.y; |
| 167 | |
| 168 | return *this; |
| 169 | } |
| 170 | |
| 171 | Vector2I& operator/= (INT32 val) |
| 172 | { |
| 173 | assert(val != 0); |
| 174 | |
| 175 | x /= val; |
| 176 | y /= val; |
| 177 | |
| 178 | return *this; |
| 179 | } |
| 180 | |
| 181 | Vector2I& operator/= (const Vector2I& rhs) |
| 182 | { |
| 183 | x /= rhs.x; |
| 184 | y /= rhs.y; |
| 185 | |
| 186 | return *this; |
| 187 | } |
| 188 | |
| 189 | /** Returns the square of the length(magnitude) of the vector. */ |
| 190 | INT32 squaredLength() const |
| 191 | { |
| 192 | return x * x + y * y; |
| 193 | } |
| 194 | |
| 195 | /** Calculates the dot (scalar) product of this vector with another. */ |
| 196 | INT32 dot(const Vector2I& vec) const |
| 197 | { |
| 198 | return x * vec.x + y * vec.y; |
| 199 | } |
| 200 | |
| 201 | static const Vector2I ZERO; |
| 202 | }; |
| 203 | |
| 204 | /** @} */ |
| 205 | BS_ALLOW_MEMCPY_SERIALIZATION(Vector2I) |
| 206 | } |
| 207 | |