home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 3 / RISC_DISC_3.iso / resources / etexts / gems / gemsv / ch7_7 / libgm / gmvec2.h < prev    next >
C/C++ Source or Header  |  1995-03-07  |  5KB  |  253 lines

  1. // gmVector2.h - 2 element vector class
  2. //
  3. // libgm++: Graphics Math Library
  4. // Ferdi Scheepers and Stephen F May
  5. // 15 June 1994
  6.  
  7. #ifndef GMVECTOR2_H
  8. #define GMVECTOR2_H
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <assert.h>
  13. #include <gmUtils.h>
  14.  
  15. class gmVector2 {
  16.  
  17. protected:
  18.   double v_[2];
  19.  
  20. public:
  21.   gmVector2();
  22.   gmVector2(const gmVector2&);
  23.   gmVector2(double, double);
  24.  
  25.   // array access
  26.  
  27.   double& operator [](int);
  28.   const double& operator [](int) const;
  29.  
  30.   // assignment
  31.  
  32.   gmVector2& assign(double, double);
  33.   gmVector2& operator =(const gmVector2&);
  34.  
  35.   // math operators
  36.  
  37.   gmVector2& operator +=(const gmVector2&);
  38.   gmVector2& operator -=(const gmVector2&);
  39.   gmVector2& operator *=(double);
  40.   gmVector2& operator /=(double);
  41.  
  42.   gmVector2 operator +(const gmVector2&) const;
  43.   gmVector2 operator -(const gmVector2&) const;
  44.   gmVector2 operator -() const;
  45.   gmVector2 operator *(double) const;
  46.   gmVector2 operator /(double) const;
  47.  
  48. friend gmVector2 operator *(double, const gmVector2&);
  49.  
  50.   bool operator ==(const gmVector2&) const;
  51.   bool operator !=(const gmVector2&) const;
  52.  
  53.   // operations
  54.  
  55.   gmVector2& clamp(double, double);
  56.   double length() const;
  57.   double lengthSquared() const;
  58.   gmVector2& normalize();
  59.  
  60.   void copyTo(float [2]) const;
  61.   void copyTo(double [2]) const;
  62.  
  63. friend double distance(const gmVector2&, const gmVector2&);
  64. friend double distanceSquared(const gmVector2&, const gmVector2&);
  65. friend double dot(const gmVector2&, const gmVector2&);
  66. friend gmVector2 lerp(double, const gmVector2&, const gmVector2&);
  67.  
  68.   // output
  69.  
  70. friend ostream & operator << ( ostream &, const gmVector2 & );
  71. };
  72.  
  73. // CONSTRUCTORS
  74.  
  75. inline gmVector2::gmVector2()
  76. {
  77.   v_[0] = v_[1] = 0;
  78. }
  79.  
  80. inline gmVector2::gmVector2(const gmVector2& v)
  81. {
  82.   v_[0] = v.v_[0]; v_[1] = v.v_[1];
  83. }
  84.  
  85. inline gmVector2::gmVector2(double x, double y)
  86. {
  87.   v_[0] = x; v_[1] = y;
  88. }
  89.  
  90. // ARRAY ACCESS
  91.  
  92. inline double& gmVector2::operator [](int i) 
  93. {
  94.   assert(i == 0 || i == 1);
  95.   return v_[i];
  96. }
  97.  
  98. inline const double& gmVector2::operator [](int i) const
  99. {
  100.   assert(i == 0 || i == 1);
  101.   return v_[i];
  102. }
  103.  
  104. // ASSIGNMENT
  105.  
  106. inline gmVector2& gmVector2::assign(double x, double y)
  107. {
  108.   v_[0] = x; v_[1] = y;
  109.   return *this;
  110. }
  111.  
  112. inline gmVector2& gmVector2::operator =(const gmVector2& v)
  113. {
  114.   v_[0] = v[0]; v_[1] = v[1];
  115.   return *this;
  116. }
  117.  
  118. // MATH OPERATORS
  119.  
  120. inline gmVector2& gmVector2::operator +=(const gmVector2& v)
  121. {
  122.   v_[0] += v[0]; v_[1] += v[1];
  123.   return *this;
  124. }
  125.  
  126. inline gmVector2& gmVector2::operator -=(const gmVector2& v)
  127. {
  128.   v_[0] -= v[0]; v_[1] -= v[1];
  129.   return *this;
  130. }
  131.  
  132. inline gmVector2& gmVector2::operator *=(double c)
  133. {
  134.   v_[0] *= c; v_[1] *= c;
  135.   return *this;
  136. }
  137.  
  138. inline gmVector2& gmVector2::operator /=(double c)
  139. {
  140.   assert(!gmIsZero(c));
  141.   v_[0] /= c; v_[1] /= c;
  142.   return *this;
  143. }
  144.  
  145. inline gmVector2 gmVector2::operator +(const gmVector2& v) const
  146. {
  147.   return gmVector2(v_[0] + v[0], v_[1] + v[1]);
  148. }
  149.  
  150. inline gmVector2 gmVector2::operator -(const gmVector2& v) const
  151. {
  152.   return gmVector2(v_[0] - v[0], v_[1] - v[1]);
  153. }
  154.  
  155. inline gmVector2 gmVector2::operator -() const
  156. {
  157.   return gmVector2(-v_[0], -v_[1]);
  158. }
  159.  
  160. inline gmVector2 gmVector2::operator *(double c) const
  161. {
  162.   return gmVector2(v_[0] * c, v_[1] * c);
  163. }
  164.  
  165. inline gmVector2 gmVector2::operator /(double c) const
  166. {
  167.   assert(!gmIsZero(c));
  168.   return gmVector2(v_[0] / c, v_[1] / c);
  169. }
  170.  
  171. inline gmVector2 operator *(double c, const gmVector2& v)
  172. {
  173.   return gmVector2(c * v[0], c * v[1]);
  174. }
  175.  
  176. inline bool gmVector2::operator ==(const gmVector2& v) const
  177. {
  178.   return (gmFuzEQ(v_[0], v[0]) && gmFuzEQ(v_[1], v[1]));
  179. }
  180.  
  181. inline bool gmVector2::operator !=(const gmVector2& v) const
  182. {
  183.   return (!(*this == v));
  184. }
  185.  
  186. // OPERATIONS
  187.  
  188. inline gmVector2& gmVector2::clamp(double lo, double hi)
  189. {
  190.   gmClamp(v_[0], lo, hi); gmClamp(v_[1], lo, hi);
  191.   return *this;
  192. }
  193.  
  194. inline double gmVector2::length() const
  195. {
  196.   return sqrt(gmSqr(v_[0]) + gmSqr(v_[1]));
  197. }
  198.  
  199. inline double gmVector2::lengthSquared() const
  200. {
  201.   return gmSqr(v_[0]) + gmSqr(v_[1]);
  202. }
  203.  
  204. inline gmVector2& gmVector2::normalize()
  205. {
  206.   double len = length();
  207.   assert(!gmIsZero(len));
  208.   *this /= len;
  209.   return *this;
  210. }
  211.  
  212. inline void gmVector2::copyTo(float f[2]) const
  213. {
  214.   f[0] = v_[0]; f[1] = v_[1];
  215. }
  216.  
  217. inline void gmVector2::copyTo(double f[2]) const
  218. {
  219.   f[0] = v_[0]; f[1] = v_[1];
  220. }
  221.  
  222. inline double distance(const gmVector2& v1, const gmVector2& v2)
  223. {
  224.   return sqrt(gmSqr(v1[0] - v2[0]) + gmSqr(v1[1] - v2[1]));
  225. }
  226.  
  227. inline double distanceSquared(const gmVector2& v1, const gmVector2& v2)
  228. {
  229.   return gmSqr(v1[0] - v2[0]) + gmSqr(v1[1] - v2[1]);
  230. }
  231.  
  232. inline double dot(const gmVector2& v1, const gmVector2& v2)
  233. {
  234.   return v1[0] * v2[0] + v1[1] * v2[1];
  235. }
  236.  
  237. inline gmVector2 lerp(double f, const gmVector2& v1, const gmVector2& v2)
  238. {
  239.   return v1 + ((v2 - v1) * f);
  240. }
  241.  
  242. // OUTPUT
  243.  
  244. inline ostream & operator << ( ostream& os, const gmVector2& v)
  245. {
  246.   os << "< " << v[0] << " " << v[1] << " >";
  247.   return os;
  248. }
  249.  
  250. #endif
  251.  
  252.  
  253.