PolyVector2.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include <math.h>
  22. #include <assert.h>
  23. namespace Polycode {
  24. /**
  25. * 2D Vector (convenience wrapper around Vector3).
  26. */
  27. class _PolyExport Vector2 : public PolyBase {
  28. public:
  29. /**
  30. * Create from x,y,z coordinates.
  31. * @param x X coordinate.
  32. * @param y Y coordinate.
  33. */
  34. Vector2(Number x,Number y);
  35. /**
  36. * Default constructor.
  37. */
  38. Vector2();
  39. ~Vector2();
  40. /**
  41. * Sets the vector from x,y,z coordinates.
  42. * @param x X coordinate.
  43. * @param y Y coordinate.
  44. */
  45. void set(Number x, Number y);
  46. inline Vector2 operator - ( const Vector2& v2 ) const {
  47. return Vector2(x - v2.x, y - v2.y);
  48. }
  49. /**
  50. * Returns the distance from this vector to another one.
  51. * @param vec2 Second vector.
  52. * @return Distance to the other vector.
  53. */
  54. inline Number distance(const Vector2& vec2) const {
  55. return (*this - vec2).length();
  56. }
  57. // ----------------------------------------------------------------------------------------------------------------
  58. /** @name Operators
  59. * Available vector operators.
  60. */
  61. //@{
  62. inline Vector2 operator * (const Number val) const {
  63. return Vector2(x * val, y * val);
  64. }
  65. inline Vector2 operator / (const Number val) const {
  66. assert( val != 0.0 );
  67. return operator*(1/val);
  68. }
  69. inline Vector2& operator = ( const Vector2& v2) {
  70. x = v2.x;
  71. y = v2.y;
  72. return *this;
  73. }
  74. inline Vector2& operator += ( const Vector2& v2) {
  75. x += v2.x;
  76. y += v2.y;
  77. return *this;
  78. }
  79. inline Vector2& operator -= ( const Vector2& v2) {
  80. x -= v2.x;
  81. y -= v2.y;
  82. return *this;
  83. }
  84. inline Vector2 operator + ( const Vector2& v2 ) const {
  85. return Vector2(x + v2.x, y + v2.y);
  86. }
  87. inline Vector2 operator - () {
  88. return Vector2(-x, -y);
  89. }
  90. inline bool operator == ( const Vector2& v2) {
  91. return (v2.x == x && v2.y == y);
  92. }
  93. inline bool operator != ( const Vector2& v2) {
  94. return (v2.x != x || v2.y != y);
  95. }
  96. //@}
  97. // ----------------------------------------------------------------------------------------------------------------
  98. /**
  99. * Returns the vector length.
  100. * @return Length of the vector.
  101. */
  102. inline Number length () const {
  103. return sqrtf( x * x + y * y);
  104. }
  105. /**
  106. * Returns the dot product with another vector.
  107. * @return Dot product with the vector.
  108. */
  109. inline Number dot(const Vector2 &u) const {
  110. return x * u.x + y * u.y;
  111. }
  112. /**
  113. * Returns the cross product with another vector.
  114. * @param vec2 Second vector.
  115. * @return Cross product with the vector.
  116. */
  117. inline Number crossProduct( const Vector2& vec2 ) const {
  118. return x * vec2.y - y * vec2.x;
  119. }
  120. inline Number angle(const Vector2& vec2 ) const {
  121. Number dtheta,theta1,theta2;
  122. theta1 = atan2(y,x);
  123. theta2 = atan2(vec2.y,vec2.x);
  124. dtheta = theta2 - theta1;
  125. while (dtheta > PI)
  126. dtheta -= PI*2.0;
  127. while (dtheta < -PI)
  128. dtheta += PI*2.0;
  129. return(dtheta);
  130. }
  131. /**
  132. * Normalizes the vector.
  133. */
  134. void Normalize();
  135. /**
  136. * X coordinate.
  137. */
  138. Number x;
  139. /**
  140. * Y coordinate.
  141. */
  142. Number y;
  143. protected:
  144. };
  145. }