lvector2_src.I 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Filename: lvector2_src.I
  2. // Created by: drose (08Mar00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. ////////////////////////////////////////////////////////////////////
  19. // Function: LVector2::Default Constructor
  20. // Access: Public
  21. // Description:
  22. ////////////////////////////////////////////////////////////////////
  23. INLINE_LINMATH FLOATNAME(LVector2)::
  24. FLOATNAME(LVector2)() {
  25. }
  26. ////////////////////////////////////////////////////////////////////
  27. // Function: LVector2::Copy Constructor
  28. // Access: Public
  29. // Description:
  30. ////////////////////////////////////////////////////////////////////
  31. INLINE_LINMATH FLOATNAME(LVector2)::
  32. FLOATNAME(LVector2)(const FLOATNAME(LVecBase2) &copy) : FLOATNAME(LVecBase2)(copy) {
  33. }
  34. ////////////////////////////////////////////////////////////////////
  35. // Function: LVector2::Copy Assignment Operator
  36. // Access: Public
  37. // Description:
  38. ////////////////////////////////////////////////////////////////////
  39. INLINE_LINMATH FLOATNAME(LVector2) &FLOATNAME(LVector2)::
  40. operator = (const FLOATNAME(LVecBase2) &copy) {
  41. FLOATNAME(LVecBase2)::operator = (copy);
  42. return *this;
  43. }
  44. ////////////////////////////////////////////////////////////////////
  45. // Function: LVector2::Copy Fill Operator
  46. // Access: Public
  47. // Description:
  48. ////////////////////////////////////////////////////////////////////
  49. INLINE_LINMATH FLOATNAME(LVector2) &FLOATNAME(LVector2)::
  50. operator = (FLOATTYPE fill_value) {
  51. FLOATNAME(LVecBase2)::operator = (fill_value);
  52. return *this;
  53. }
  54. ////////////////////////////////////////////////////////////////////
  55. // Function: LVector2::Constructor
  56. // Access: Public
  57. // Description:
  58. ////////////////////////////////////////////////////////////////////
  59. INLINE_LINMATH FLOATNAME(LVector2)::
  60. FLOATNAME(LVector2)(FLOATTYPE fill_value) :
  61. FLOATNAME(LVecBase2)(fill_value)
  62. {
  63. }
  64. ////////////////////////////////////////////////////////////////////
  65. // Function: LVector2::Constructor
  66. // Access: Public
  67. // Description:
  68. ////////////////////////////////////////////////////////////////////
  69. INLINE_LINMATH FLOATNAME(LVector2)::
  70. FLOATNAME(LVector2)(FLOATTYPE x, FLOATTYPE y) :
  71. FLOATNAME(LVecBase2)(x, y)
  72. {
  73. }
  74. ////////////////////////////////////////////////////////////////////
  75. // Function: LVector2::zero Named Constructor
  76. // Access: Public
  77. // Description: Returns a zero-length vector.
  78. ////////////////////////////////////////////////////////////////////
  79. INLINE_LINMATH const FLOATNAME(LVector2) &FLOATNAME(LVector2)::
  80. zero() {
  81. return (const FLOATNAME(LVector2) &)FLOATNAME(LVecBase2)::zero();
  82. }
  83. ////////////////////////////////////////////////////////////////////
  84. // Function: LVector2::unit_x Named Constructor
  85. // Access: Public
  86. // Description: Returns a unit X vector.
  87. ////////////////////////////////////////////////////////////////////
  88. INLINE_LINMATH const FLOATNAME(LVector2) &FLOATNAME(LVector2)::
  89. unit_x() {
  90. return (const FLOATNAME(LVector2) &)FLOATNAME(LVecBase2)::unit_x();
  91. }
  92. ////////////////////////////////////////////////////////////////////
  93. // Function: LVector2::unit_y Named Constructor
  94. // Access: Public
  95. // Description: Returns a unit Y vector.
  96. ////////////////////////////////////////////////////////////////////
  97. INLINE_LINMATH const FLOATNAME(LVector2) &FLOATNAME(LVector2)::
  98. unit_y() {
  99. return (const FLOATNAME(LVector2) &)FLOATNAME(LVecBase2)::unit_y();
  100. }
  101. ////////////////////////////////////////////////////////////////////
  102. // Function: LVector2::unary -
  103. // Access: Public
  104. // Description:
  105. ////////////////////////////////////////////////////////////////////
  106. INLINE_LINMATH FLOATNAME(LVector2) FLOATNAME(LVector2)::
  107. operator - () const {
  108. return FLOATNAME(LVecBase2)::operator - ();
  109. }
  110. ////////////////////////////////////////////////////////////////////
  111. // Function: LVector2::vector + vecbase
  112. // Access: Public
  113. // Description:
  114. ////////////////////////////////////////////////////////////////////
  115. INLINE_LINMATH FLOATNAME(LVecBase2) FLOATNAME(LVector2)::
  116. operator + (const FLOATNAME(LVecBase2) &other) const {
  117. return FLOATNAME(LVecBase2)::operator + (other);
  118. }
  119. ////////////////////////////////////////////////////////////////////
  120. // Function: LVector2::vector + vector
  121. // Access: Public
  122. // Description:
  123. ////////////////////////////////////////////////////////////////////
  124. INLINE_LINMATH FLOATNAME(LVector2) FLOATNAME(LVector2)::
  125. operator + (const FLOATNAME(LVector2) &other) const {
  126. return FLOATNAME(LVecBase2)::operator + (other);
  127. }
  128. ////////////////////////////////////////////////////////////////////
  129. // Function: LVector2::vector - vecbase
  130. // Access: Public
  131. // Description:
  132. ////////////////////////////////////////////////////////////////////
  133. INLINE_LINMATH FLOATNAME(LVecBase2) FLOATNAME(LVector2)::
  134. operator - (const FLOATNAME(LVecBase2) &other) const {
  135. return FLOATNAME(LVecBase2)::operator - (other);
  136. }
  137. ////////////////////////////////////////////////////////////////////
  138. // Function: LVector2::vector - vector
  139. // Access: Public
  140. // Description:
  141. ////////////////////////////////////////////////////////////////////
  142. INLINE_LINMATH FLOATNAME(LVector2) FLOATNAME(LVector2)::
  143. operator - (const FLOATNAME(LVector2) &other) const {
  144. return FLOATNAME(LVecBase2)::operator - (other);
  145. }
  146. ////////////////////////////////////////////////////////////////////
  147. // Function: LVector2::length
  148. // Access: Public
  149. // Description: Returns the length of the vector, by the Pythagorean
  150. // theorem.
  151. ////////////////////////////////////////////////////////////////////
  152. INLINE_LINMATH FLOATTYPE FLOATNAME(LVector2)::
  153. length() const {
  154. return csqrt((*this).dot(*this));
  155. }
  156. ////////////////////////////////////////////////////////////////////
  157. // Function: LVector2::length_squared
  158. // Access: Public
  159. // Description: Returns the square of the vector's length, cheap and
  160. // easy.
  161. ////////////////////////////////////////////////////////////////////
  162. INLINE_LINMATH FLOATTYPE FLOATNAME(LVector2)::
  163. length_squared() const {
  164. return (*this).dot(*this);
  165. }
  166. ////////////////////////////////////////////////////////////////////
  167. // Function: LVector2::normalize
  168. // Access: Public
  169. // Description: Normalizes the vector in place. Returns true if the
  170. // vector was normalized, false if it was a zero-length
  171. // vector.
  172. ////////////////////////////////////////////////////////////////////
  173. INLINE_LINMATH bool FLOATNAME(LVector2)::
  174. normalize() {
  175. FLOATTYPE l2 = length_squared();
  176. if (l2 == (FLOATTYPE)0.0f) {
  177. set(0.0f, 0.0f);
  178. return false;
  179. } else if (!IS_THRESHOLD_EQUAL(l2, 1.0f, NEARLY_ZERO(FLOATTYPE) * NEARLY_ZERO(FLOATTYPE))) {
  180. (*this) /= csqrt(l2);
  181. }
  182. return true;
  183. }
  184. ////////////////////////////////////////////////////////////////////
  185. // Function: LVector2::operator * scalar
  186. // Access: Public
  187. // Description:
  188. ////////////////////////////////////////////////////////////////////
  189. INLINE_LINMATH FLOATNAME(LVector2) FLOATNAME(LVector2)::
  190. operator * (FLOATTYPE scalar) const {
  191. return FLOATNAME(LVector2)(FLOATNAME(LVecBase2)::operator * (scalar));
  192. }
  193. ////////////////////////////////////////////////////////////////////
  194. // Function: LVector2::operator / scalar
  195. // Access: Public
  196. // Description:
  197. ////////////////////////////////////////////////////////////////////
  198. INLINE_LINMATH FLOATNAME(LVector2) FLOATNAME(LVector2)::
  199. operator / (FLOATTYPE scalar) const {
  200. FLOATTYPE recip_scalar = 1.0f/scalar;
  201. return FLOATNAME(LVector2)(FLOATNAME(LVecBase2)::operator * (recip_scalar));
  202. }
  203. #ifdef HAVE_PYTHON
  204. ////////////////////////////////////////////////////////////////////
  205. // Function: LVector2::python_repr
  206. // Access: Published
  207. // Description:
  208. ////////////////////////////////////////////////////////////////////
  209. INLINE_LINMATH void FLOATNAME(LVector2)::
  210. python_repr(ostream &out, const string &class_name) const {
  211. FLOATNAME(LVecBase2)::python_repr(out, class_name);
  212. }
  213. #endif // HAVE_PYTHON