| 622 | */ |
| 623 | template<class T> |
| 624 | struct v_2d |
| 625 | { |
| 626 | static_assert(std::is_arithmetic<T>::value, "olc::v_2d<type> must be numeric"); |
| 627 | |
| 628 | // x-axis component |
| 629 | T x = 0; |
| 630 | // y-axis component |
| 631 | T y = 0; |
| 632 | |
| 633 | // Default constructor |
| 634 | inline constexpr v_2d() = default; |
| 635 | |
| 636 | // Specific constructor |
| 637 | inline constexpr v_2d(T _x, T _y) : x(_x), y(_y) |
| 638 | {} |
| 639 | |
| 640 | // Copy constructor |
| 641 | inline constexpr v_2d(const v_2d& v) = default; |
| 642 | |
| 643 | // Assignment operator |
| 644 | inline constexpr v_2d& operator=(const v_2d& v) = default; |
| 645 | |
| 646 | |
| 647 | inline constexpr std::array<T, 2> a() const |
| 648 | { |
| 649 | return std::array<T, 2>{x, y}; |
| 650 | } |
| 651 | |
| 652 | // Returns rectangular area of vector |
| 653 | inline constexpr auto area() const |
| 654 | { |
| 655 | return x * y; |
| 656 | } |
| 657 | |
| 658 | // Returns magnitude of vector |
| 659 | inline auto mag() const |
| 660 | { |
| 661 | return std::sqrt(x * x + y * y); |
| 662 | } |
| 663 | |
| 664 | // Returns magnitude squared of vector (useful for fast comparisons) |
| 665 | inline constexpr T mag2() const |
| 666 | { |
| 667 | return x * x + y * y; |
| 668 | } |
| 669 | |
| 670 | // Returns normalised version of vector |
| 671 | inline v_2d norm() const |
| 672 | { |
| 673 | auto r = 1 / mag(); |
| 674 | return v_2d(x * r, y * r); |
| 675 | } |
| 676 | |
| 677 | // Returns vector at 90 degrees to this one |
| 678 | inline constexpr v_2d perp() const |
| 679 | { |
| 680 | return v_2d(-y, x); |
| 681 | } |
no outgoing calls
no test coverage detected