Vec2.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #ifndef VEC2_H
  2. #define VEC2_H
  3. #include "Common.h"
  4. namespace M {
  5. /// 2D vector
  6. class Vec2
  7. {
  8. public:
  9. /// @name Constructors & distructors
  10. /// @{
  11. explicit Vec2();
  12. explicit Vec2(float x, float y);
  13. explicit Vec2(float f);
  14. explicit Vec2(float arr[]);
  15. Vec2(const Vec2& b);
  16. explicit Vec2(const Vec3& v3);
  17. explicit Vec2(const Vec4& v4);
  18. /// @}
  19. /// @name Accessors
  20. /// @{
  21. float& x();
  22. float x() const;
  23. float& y();
  24. float y() const;
  25. float& operator[](uint i);
  26. float operator[](uint i) const;
  27. /// @}
  28. /// @name Operators with same type
  29. /// @{
  30. Vec2& operator=(const Vec2& b);
  31. Vec2 operator+(const Vec2& b) const;
  32. Vec2& operator+=(const Vec2& b);
  33. Vec2 operator-(const Vec2& b) const;
  34. Vec2& operator-=(const Vec2& b);
  35. Vec2 operator*(const Vec2& b) const;
  36. Vec2& operator*=(const Vec2& b);
  37. Vec2 operator/(const Vec2& b) const;
  38. Vec2& operator/=(const Vec2& b);
  39. Vec2 operator-() const;
  40. bool operator==(const Vec2& b) const;
  41. bool operator!=(const Vec2& b) const;
  42. /// @}
  43. /// @name Operators with float
  44. /// @{
  45. Vec2 operator+(float f) const;
  46. Vec2& operator+=(float f);
  47. Vec2 operator-(float f) const;
  48. Vec2& operator-=(float f);
  49. Vec2 operator*(float f) const;
  50. Vec2& operator*=(float f);
  51. Vec2 operator/(float f) const;
  52. Vec2& operator/=(float f);
  53. /// @}
  54. /// @name Other
  55. /// @{
  56. float getLength() const;
  57. Vec2 getNormalized() const;
  58. void normalize();
  59. float dot(const Vec2& b) const;
  60. /// @}
  61. private:
  62. /// @name Data members
  63. /// @{
  64. union
  65. {
  66. struct
  67. {
  68. float x, y;
  69. } vec;
  70. boost::array<float, 2> arr;
  71. };
  72. /// @}
  73. };
  74. /// @name Other operators
  75. /// @{
  76. extern Vec2 operator+(float f, const Vec2& v2);
  77. extern Vec2 operator-(float f, const Vec2& v2);
  78. extern Vec2 operator*(float f, const Vec2& v2);
  79. extern Vec2 operator/(float f, const Vec2& v2);
  80. extern std::ostream& operator<<(std::ostream& s, const Vec2& v);
  81. /// @}
  82. } // end namespace
  83. #include "Vec2.inl.h"
  84. #endif