| 207 | */ |
| 208 | template<class T> |
| 209 | struct v_2d |
| 210 | { |
| 211 | static_assert(std::is_arithmetic<T>::value, "olc::v_2d<type> must be numeric"); |
| 212 | |
| 213 | // x-axis component |
| 214 | T x = 0; |
| 215 | // y-axis component |
| 216 | T y = 0; |
| 217 | |
| 218 | // Default constructor |
| 219 | inline constexpr v_2d() = default; |
| 220 | |
| 221 | // Specific constructor |
| 222 | inline constexpr v_2d(T _x, T _y) : x(_x), y(_y) |
| 223 | {} |
| 224 | |
| 225 | // Copy constructor |
| 226 | inline constexpr v_2d(const v_2d& v) = default; |
| 227 | |
| 228 | // Assignment operator |
| 229 | inline constexpr v_2d& operator=(const v_2d& v) = default; |
| 230 | |
| 231 | inline constexpr std::array<T, 2> a() const |
| 232 | { |
| 233 | return std::array<T, 2>{x, y}; |
| 234 | } |
| 235 | |
| 236 | // Returns rectangular area of vector |
| 237 | inline constexpr auto area() const |
| 238 | { |
| 239 | return x * y; |
| 240 | } |
| 241 | |
| 242 | // Returns magnitude of vector |
| 243 | inline auto mag() const |
| 244 | { |
| 245 | return std::sqrt(x * x + y * y); |
| 246 | } |
| 247 | |
| 248 | // Returns magnitude squared of vector (useful for fast comparisons) |
| 249 | inline constexpr T mag2() const |
| 250 | { |
| 251 | return x * x + y * y; |
| 252 | } |
| 253 | |
| 254 | // Returns normalised version of vector |
| 255 | inline v_2d norm() const |
| 256 | { |
| 257 | auto r = 1 / mag(); |
| 258 | return v_2d(x * r, y * r); |
| 259 | } |
| 260 | |
| 261 | // Returns vector at 90 degrees to this one |
| 262 | inline constexpr v_2d perp() const |
| 263 | { |
| 264 | return v_2d(-y, x); |
| 265 | } |
| 266 |
no outgoing calls
no test coverage detected