home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / XenonSource.exe / gamesystem / includes / gs_vector.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-21  |  5.3 KB  |  241 lines

  1. //-------------------------------------------------------------
  2. //
  3. // Class:    gsCVector
  4. //
  5. // Author:    John M Phillips
  6. //
  7. // Started:    12/03/00
  8. //
  9. // Base:    gsCObject
  10. //
  11. // Derived:    None
  12. //
  13. //-------------------------------------------------------------
  14.  
  15. #ifndef _INCLUDE_GS_VECTOR_H
  16. #define _INCLUDE_GS_VECTOR_H
  17.  
  18. #include "gs_object.h"
  19. #include "gs_maths.h"
  20. #include "gs_error.h"
  21.  
  22. class gsCPoint;
  23.  
  24. //-------------------------------------------------------------
  25. // 2d Vector Class
  26.  
  27. class gsCVector : public gsCObject
  28. {
  29.     friend class gsCPoint;
  30.     
  31.     private:
  32.         float m_x;
  33.         float m_y;
  34.  
  35.     public:
  36.         gsCVector();
  37.         gsCVector(float x,float y);
  38.         gsCVector(const gsCVector& vector);
  39.         ~gsCVector();
  40.  
  41.         float getX() const;
  42.         float getY() const;
  43.  
  44.         void setX(float x);
  45.         void setY(float y);
  46.  
  47.         static gsCVector polar(float distance,float angle);
  48.  
  49.         gsCVector operator - ();
  50.  
  51.         const gsCVector& operator += (const gsCVector& a);
  52.         const gsCVector& operator -= (const gsCVector& a);
  53.  
  54.         friend gsCVector operator + (const gsCVector& a,const gsCVector& b);
  55.         friend gsCVector operator - (const gsCVector& a,const gsCVector& b);
  56.         friend gsCVector operator * (const gsCVector& a,const gsCVector& b);
  57.         friend gsCVector operator * (const gsCVector& a,float b);
  58.         friend gsCVector operator * (float a,const gsCVector& b);
  59.         friend gsCVector interpolate(const gsCVector& a,const gsCVector& b,float t);
  60.  
  61.         friend bool operator == (const gsCVector& a,const gsCVector& b);
  62.         friend bool operator != (const gsCVector& a,const gsCVector& b);
  63.  
  64.         float length();
  65.         float direction();
  66.         void normalize();
  67.  
  68.         void clamp(float minx,float maxx,float miny,float maxy);
  69. };
  70.  
  71. //-------------------------------------------------------------
  72.  
  73. inline gsCVector::gsCVector()
  74. {
  75.     m_x = m_y = 0.f;
  76. }
  77.  
  78. //-------------------------------------------------------------
  79.  
  80. inline gsCVector::gsCVector(float x,float y)
  81. {
  82.     m_x = x;
  83.     m_y = y;
  84. }
  85.  
  86. //-------------------------------------------------------------
  87.  
  88. inline gsCVector::gsCVector(const gsCVector& vector)
  89. {
  90.     m_x = vector.m_x;
  91.     m_y = vector.m_y;
  92. }
  93.  
  94. //-------------------------------------------------------------
  95.  
  96. inline float gsCVector::getX() const
  97. {
  98.     return m_x;
  99. }
  100.  
  101. //-------------------------------------------------------------
  102.  
  103. inline float gsCVector::getY() const
  104. {
  105.     return m_y;
  106. }
  107.  
  108. //-------------------------------------------------------------
  109.  
  110. inline void gsCVector::setX(float x)
  111. {
  112.     m_x = x;
  113. }
  114.  
  115. //-------------------------------------------------------------
  116.  
  117. inline void gsCVector::setY(float y)
  118. {
  119.     m_y = y;
  120. }
  121.  
  122. //-------------------------------------------------------------
  123.  
  124. inline gsCVector gsCVector::operator - ()
  125. {
  126.     return gsCVector(-m_x,-m_y);
  127. }
  128.  
  129. //-------------------------------------------------------------
  130.  
  131. inline const gsCVector& gsCVector::operator += (const gsCVector& a)
  132. {
  133.     m_x += a.m_x;
  134.     m_y += a.m_y;
  135.  
  136.     return *this;
  137. }
  138.  
  139. //-------------------------------------------------------------
  140.  
  141. inline const gsCVector& gsCVector::operator -= (const gsCVector& a)
  142. {
  143.     m_x -= a.m_x;
  144.     m_y -= a.m_y;
  145.  
  146.     return *this;
  147. }
  148.  
  149. //-------------------------------------------------------------
  150.  
  151. inline gsCVector operator + (const gsCVector& a,const gsCVector& b)
  152. {
  153.     return gsCVector(a.m_x + b.m_x,a.m_y + b.m_y);
  154. }
  155.  
  156. //-------------------------------------------------------------
  157.  
  158. inline gsCVector operator - (const gsCVector& a,const gsCVector& b)
  159. {
  160.     return gsCVector(a.m_x - b.m_x,a.m_y - b.m_y);
  161. }
  162.  
  163. //-------------------------------------------------------------
  164.  
  165. inline gsCVector operator * (const gsCVector& a,const gsCVector& b)
  166. {
  167.     return gsCVector(a.m_x * b.m_x,a.m_y * b.m_y);
  168. }
  169.  
  170. //-------------------------------------------------------------
  171.  
  172. inline gsCVector operator * (const gsCVector& a,float b)
  173. {
  174.     return gsCVector(a.m_x * b,a.m_y * b);
  175. }
  176.  
  177. //-------------------------------------------------------------
  178.  
  179. inline gsCVector operator * (float a,const gsCVector& b)
  180. {
  181.     return gsCVector(a * b.m_x,a * b.m_y);
  182. }
  183.  
  184. //-------------------------------------------------------------
  185.  
  186. inline gsCVector interpolate(const gsCVector& a,const gsCVector& b,float t)
  187. {
  188.     return gsCVector(a.m_x + (b.m_x - a.m_x) * t,
  189.                      a.m_y + (b.m_y - a.m_y) * t);
  190. }
  191.  
  192. //-------------------------------------------------------------
  193.  
  194. inline bool operator == (const gsCVector& a,const gsCVector& b)
  195. {
  196.     return (a.m_x == b.m_x) && (a.m_y == b.m_y);
  197. }
  198.  
  199. //-------------------------------------------------------------
  200.  
  201. inline bool operator != (const gsCVector& a,const gsCVector& b)
  202. {
  203.     return (a.m_x != b.m_x) || (a.m_y != b.m_y);
  204. }
  205.  
  206. //-------------------------------------------------------------
  207.  
  208. inline float gsCVector::length()
  209. {
  210.     return gsSquareRoot(m_x * m_x + m_y * m_y);
  211. }
  212.  
  213. //-------------------------------------------------------------
  214.  
  215. inline float gsCVector::direction()
  216. {
  217.     return gsArcTan(m_x,-m_y);
  218. }
  219.  
  220. //-------------------------------------------------------------
  221.  
  222. inline void gsCVector::normalize()
  223. {
  224.     float len = length();
  225.  
  226.     if (len > 0.f) {
  227.         m_x /= len;
  228.         m_y /= len;
  229.         }
  230. }
  231.  
  232. //-------------------------------------------------------------
  233.  
  234. inline gsCVector::~gsCVector()
  235. {
  236. }
  237.  
  238. //-------------------------------------------------------------
  239.  
  240. #endif
  241.