2
0

Vector2.pkg 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. $#include "Vector2.h"
  2. /// Two-dimensional vector.
  3. class Vector2
  4. {
  5. public:
  6. /// Construct undefined.
  7. Vector2()
  8. {
  9. }
  10. /// Copy-construct from another vector.
  11. Vector2(const Vector2& vector) :
  12. x_(vector.x_),
  13. y_(vector.y_)
  14. {
  15. }
  16. /// Construct from coordinates.
  17. Vector2(float x, float y) :
  18. x_(x),
  19. y_(y)
  20. {
  21. }
  22. /// Construct from a float array.
  23. Vector2(const float* data) :
  24. x_(data[0]),
  25. y_(data[1])
  26. {
  27. }
  28. /// Test for equality with another vector without epsilon.
  29. bool operator == (const Vector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
  30. /// Add a vector.
  31. Vector2 operator + (const Vector2& rhs) const { return Vector2(x_ + rhs.x_, y_ + rhs.y_); }
  32. /// Return negation.
  33. Vector2 operator - () const { return Vector2(-x_, -y_); }
  34. /// Subtract a vector.
  35. Vector2 operator - (const Vector2& rhs) const { return Vector2(x_ - rhs.x_, y_ - rhs.y_); }
  36. /// Multiply with a scalar.
  37. Vector2 operator * (float rhs) const { return Vector2(x_ * rhs, y_ * rhs); }
  38. /// Multiply with a vector.
  39. Vector2 operator * (const Vector2& rhs) const { return Vector2(x_ * rhs.x_, y_ * rhs.y_); }
  40. /// Divide by a scalar.
  41. Vector2 operator / (float rhs) const { return Vector2(x_ / rhs, y_ / rhs); }
  42. /// Divide by a vector.
  43. Vector2 operator / (const Vector2& rhs) const { return Vector2(x_ / rhs.x_, y_ / rhs.y_); }
  44. Vector2 operator / (const Vector2& rhs) const;
  45. /// Normalize to unit length and return the previous length.
  46. float Normalize()
  47. {
  48. float len = Length();
  49. if (len >= M_EPSILON)
  50. {
  51. float invLen = 1.0f / len;
  52. x_ *= invLen;
  53. y_ *= invLen;
  54. }
  55. return len;
  56. }
  57. /// Return length.
  58. float Length() const { return sqrtf(x_ * x_ + y_ * y_); }
  59. /// Return squared length.
  60. float LengthSquared() const { return x_ * x_ + y_ * y_; }
  61. /// Calculate dot product.
  62. float DotProduct(const Vector2& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_; }
  63. /// Calculate absolute dot product.
  64. float AbsDotProduct(const Vector2& rhs) const { return Urho3D::Abs(x_ * rhs.x_) + Urho3D::Abs(y_ * rhs.y_); }
  65. /// Return absolute vector.
  66. Vector2 Abs() const { return Vector2(Urho3D::Abs(x_), Urho3D::Abs(y_)); }
  67. /// Linear interpolation with another vector.
  68. Vector2 Lerp(const Vector2& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  69. /// Test for equality with another vector with epsilon.
  70. bool Equals(const Vector2& rhs) const { return Urho3D::Equals(x_, rhs.x_) && Urho3D::Equals(y_, rhs.y_); }
  71. /// Return normalized to unit length.
  72. Vector2 Normalized() const
  73. {
  74. float len = Length();
  75. if (len >= M_EPSILON)
  76. return *this * (1.0f / len);
  77. else
  78. return *this;
  79. }
  80. /// Return as string.
  81. String ToString() const;
  82. /// X coordinate.
  83. float x_ @ x;
  84. /// Y coordinate.
  85. float y_ @ y;
  86. /// Zero vector.
  87. static const Vector2 ZERO;
  88. /// (-1,0) vector.
  89. static const Vector2 LEFT;
  90. /// (1,0) vector.
  91. static const Vector2 RIGHT;
  92. /// (0,1) vector.
  93. static const Vector2 UP;
  94. /// (0,-1) vector.
  95. static const Vector2 DOWN;
  96. /// (1,1) vector.
  97. static const Vector2 ONE;
  98. };
  99. /// Two-dimensional vector with integer values.
  100. class IntVector2
  101. {
  102. public:
  103. /// Construct undefined.
  104. IntVector2()
  105. {
  106. }
  107. /// Construct from coordinates.
  108. IntVector2(int x, int y) :
  109. x_(x),
  110. y_(y)
  111. {
  112. }
  113. /// Construct from an int array.
  114. IntVector2(const int* data) :
  115. x_(data[0]),
  116. y_(data[1])
  117. {
  118. }
  119. /// Copy-construct from another vector.
  120. IntVector2(const IntVector2& rhs) :
  121. x_(rhs.x_),
  122. y_(rhs.y_)
  123. {
  124. }
  125. /// Test for equality with another vector.
  126. bool operator == (const IntVector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
  127. /// Add a vector.
  128. IntVector2 operator + (const IntVector2& rhs) const { return IntVector2(x_ + rhs.x_, y_ + rhs.y_); }
  129. /// Return negation.
  130. IntVector2 operator - () const { return IntVector2(-x_, -y_); }
  131. /// Subtract a vector.
  132. IntVector2 operator - (const IntVector2& rhs) const { return IntVector2(x_ - rhs.x_, y_ - rhs.y_); }
  133. /// Multiply with a scalar.
  134. IntVector2 operator * (int rhs) const { return IntVector2(x_ * rhs, y_ * rhs); }
  135. /// Divide by a scalar.
  136. IntVector2 operator / (int rhs) const { return IntVector2(x_ / rhs, y_ / rhs); }
  137. IntVector2 operator / (int rhs) const;
  138. /// Return as string.
  139. String ToString() const;
  140. /// X coordinate.
  141. int x_ @ x;
  142. /// Y coordinate.
  143. int y_ @ y;
  144. /// Zero vector.
  145. static const IntVector2 ZERO;
  146. };