home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / flying-6.11 / vec2.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-30  |  5.1 KB  |  210 lines

  1. #ifndef _vec2_h
  2. #define _vec2_h
  3.  
  4. #ifndef _real_h
  5. #    include "real.h"
  6. #endif
  7.  
  8. #if (Vec2IsVector)
  9.  
  10. #ifndef _vector_h
  11. #    include "vector.h"
  12. #endif
  13.  
  14. class Vec2 : public Vector {
  15.     public:
  16.         const Real &X() const                             { return data[0]; }
  17.         const Real &Y() const                             { return data[1]; }
  18.  
  19.         Vec2( double x, double y ) : Vector( 2, x, y )        { }
  20.         Vec2( const Vector &v ) : Vector( v )                    { Resize(2); }
  21.         Vec2()                                                            { }
  22.  
  23.         const Vec2& operator=(const Vector &v)
  24.         { Vector::operator=(v); return *this; }
  25.  
  26.         Vec2 TurnLeft() const                    { return Vec2( -Y(), X() ); }
  27.         Vec2 TurnRight() const                    { return Vec2( Y(), -X() ); }
  28.         Vec2 TurnAngleRad( const Real &angle ) const;
  29.         Vec2 TurnAngleDeg( const Real &angle ) const
  30.             { return TurnAngleRad(angle/Real(180/M_PI)); }
  31.  
  32.         void Split( const Vec2 &d, Vec2 *vx, Vec2 *vy ) const;
  33.         void Split( const Vec2 &d, Vec2 *vx ) const;
  34.  
  35.         Real AngleRadial( const Vec2 &d ) const;
  36.         Real AngleDeg( const Vec2 &d ) const        { return AngleRadial(d)*Real(180/M_PI); }
  37.  
  38.         static int Solve(    const Vec2 &p1, const Vec2 &d1,
  39.                         const Vec2 &p2, const Vec2 &d2, Real *t1 );
  40. };
  41. #else
  42. //
  43. // -------------------------------------------------------------------------
  44. // class Vec2 : Vektorklasse, die einfach die komplexen Zahlen erweitert
  45. // -------------------------------------------------------------------------
  46. //
  47.  
  48. class Vec2 {
  49.     private:
  50.         Real    x_val;
  51.         Real    y_val;
  52.  
  53.     public:
  54.         const Real &X() const                             { return x_val; }
  55.         const Real &Y() const                             { return y_val; }
  56.  
  57.         int    IsZero() const                                { return X()==0.0&&Y()==0.0; }
  58.  
  59.         Vec2( const Real &x, const Real &y )        { x_val=x; y_val=y; }
  60.         Vec2( const Vec2 &v )                        { x_val=v.X(); y_val=v.Y(); }
  61.         Vec2()                                                { }
  62.  
  63.         Real SqrNorm() const;
  64.         Real Norm() const;
  65.         Vec2 Norm1() const;
  66.  
  67.         Vec2 TurnLeft() const                { return Vec2( -Y(), X() ); }
  68.         Vec2 TurnRight() const            { return Vec2( Y(), -X() );    }
  69.         Vec2 TurnAngleRad( const Real &angle ) const;
  70.         Vec2 TurnAngleDeg( const Real &angle ) const
  71.             { return TurnAngleRad(angle/Real(180/M_PI)); }
  72.  
  73.         void Split( const Vec2 &d, Vec2 *vx, Vec2 *vy ) const;
  74.         void Split( const Vec2 &d, Vec2 *vx ) const;
  75.  
  76.         Real AngleRadial( const Vec2 &d ) const;
  77.         Real AngleDeg( const Vec2 &d ) const        { return AngleRadial(d)*Real(180/M_PI); }
  78.  
  79.         static int Solve(    const Vec2 &p1, const Vec2 &d1,
  80.                         const Vec2 &p2, const Vec2 &d2, Real *t1 );
  81.  
  82.     inline const Vec2& operator=(const Vec2 &v);
  83.  
  84.     // Binary Operator Functions
  85.  
  86.     inline Vec2 operator+(const Vec2&) const;
  87.     inline Vec2 operator-(const Vec2&) const;
  88.  
  89. #ifndef _TURBOC_
  90.     friend inline Real operator*(const Vec2&, const Vec2&);
  91.     friend inline Vec2 operator*(const Real&, const Vec2&);
  92.     friend inline int operator==(const Vec2&, const Vec2&);
  93.     friend inline int operator!=(const Vec2&, const Vec2&);
  94. #else
  95.     friend Real operator*(const Vec2&, const Vec2&);
  96.     friend Vec2 operator*(const Real&, const Vec2&);
  97.     friend int operator==(const Vec2&, const Vec2&);
  98.     friend int operator!=(const Vec2&, const Vec2&);
  99. #endif
  100.  
  101.     inline Vec2 operator*(const Real&) const;
  102.     inline Vec2 operator/(const Real&) const;
  103.  
  104.     inline const Vec2& operator+=(const Vec2&);
  105.     inline const Vec2& operator-=(const Vec2&);
  106.     inline const Vec2& operator*=(const Real&);
  107.     inline const Vec2& operator/=(const Real&);
  108.     inline Vec2 operator+() const;
  109.     inline Vec2 operator-() const;
  110.  
  111. };
  112.  
  113. inline const Vec2& Vec2::operator=(const Vec2 &v) {
  114.     x_val = v.x_val;
  115.     y_val = v.y_val;
  116.     return *this;
  117. }
  118.  
  119. inline Vec2 Vec2::operator+() const
  120. {
  121.     return *this;
  122. }
  123.  
  124. inline Vec2 Vec2::operator-() const
  125. {
  126.     return Vec2(-x_val, -y_val);
  127. }
  128.  
  129.  
  130. // Definitions of compound-assignment operator member functions
  131.  
  132. inline const Vec2& Vec2::operator+=(const Vec2& z2)
  133. {
  134.     x_val += z2.x_val;
  135.     y_val += z2.y_val;
  136.     return *this;
  137. }
  138.  
  139. inline const Vec2& Vec2::operator-=(const Vec2& z2)
  140. {
  141.     x_val -= z2.x_val;
  142.     y_val -= z2.y_val;
  143.     return *this;
  144. }
  145.  
  146. inline const Vec2& Vec2::operator*=(const Real& val)
  147. {
  148.     x_val *= val;
  149.     y_val *= val;
  150.     return *this;
  151. }
  152.  
  153. inline const Vec2& Vec2::operator/=(const Real& val)
  154. {
  155.     x_val /= val;
  156.     y_val /= val;
  157.     return *this;
  158. }
  159.  
  160.  
  161. // Definitions of non-member binary operator functions
  162.  
  163. inline Vec2 Vec2::operator+(const Vec2& z2) const
  164. {
  165.           return Vec2(x_val + z2.x_val, y_val + z2.y_val);
  166. }
  167. inline Vec2 Vec2::operator-(const Vec2& z2) const
  168. {
  169.           return Vec2(x_val - z2.x_val, y_val - z2.y_val);
  170. }
  171.  
  172.  
  173. inline Real operator*(const Vec2& z1, const Vec2& z2)
  174. {
  175.           return z1.x_val*z2.x_val + z1.y_val*z2.y_val;
  176. }
  177. inline Vec2 Vec2::operator*(const Real& val2) const
  178. {
  179.           return Vec2(x_val*val2, y_val*val2);
  180. }
  181. inline Vec2 operator*(const Real& val, const Vec2& z2)
  182. {
  183.           return Vec2(z2.x_val*val, z2.y_val*val);
  184. }
  185.  
  186. inline Vec2 Vec2::operator/(const Real& val) const
  187. {
  188.           return Vec2(x_val/val, y_val/val);
  189. }
  190.  
  191. inline int operator==(const Vec2& z1, const Vec2& z2)
  192. {
  193.           return z1.x_val == z2.x_val && z1.y_val == z2.y_val;
  194. }
  195.  
  196. inline int operator!=(const Vec2& z1, const Vec2& z2)
  197. {
  198.           return z1.x_val != z2.x_val || z1.y_val != z2.y_val;
  199. }
  200.  
  201. inline Real Vec2::SqrNorm() const                { return X()*X()+Y()*Y(); }
  202. inline Real Vec2::Norm() const                    { return sqrt(SqrNorm()); }
  203. inline Vec2 Vec2::Norm1() const                { return *this / Norm(); }
  204.  
  205. #endif
  206.  
  207. extern Vec2 Vec2Zero;
  208.  
  209. #endif
  210.