home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 February / Chip_2001-02_cd1.bin / bonus / demos / CS / exp / SOURCES / GLENGINE / vector2.h < prev    next >
C/C++ Source or Header  |  2000-08-16  |  4KB  |  143 lines

  1. #ifndef __OGL2_VECTOR2_H__
  2. #define __OGL2_VECTOR2_H__
  3.  
  4. #if !defined (__GNUG__) && !defined (__attribute__)
  5. #define __attribute__(foo) /* Ignore.  */
  6. #endif
  7.  
  8. #include <iostream>
  9.  
  10. extern "C++" {
  11.  
  12. template <class __type>
  13. struct Vector2 {
  14.   __type x;
  15.   __type y;
  16.  
  17.   Vector2() :
  18.     x(0), y(0) {}
  19.   Vector2(__type _x, __type _y) :
  20.     x(_x), y(_y) {}
  21.  
  22.   Vector2<__type>& operator () (__type _x, __type _y) {
  23.     x = _x; y = _y;
  24.     return *this;
  25.   }
  26.   Vector2<__type>& operator () (const Vector2<__type>& b) {
  27.     x = b.x;
  28.     y = b.y;
  29.     return *this;
  30.   }
  31.   Vector2<__type>& operator += (const Vector2<__type>& b) {
  32.     x += b.x;
  33.     y += b.y;
  34.     return *this;
  35.   }
  36.   Vector2<__type>& operator -= (const Vector2<__type>& b) {
  37.     x -= b.x;
  38.     y -= b.y;
  39.     return *this;
  40.   }
  41.   Vector2<__type>& operator *= (const Vector2<__type>& b) {
  42.     x *= b.x;
  43.     y *= b.y;
  44.     return *this;
  45.   }
  46.   Vector2<__type>& operator *= (__type s) {
  47.     x *= s;
  48.     y *= s;
  49.     return *this;
  50.   }
  51.   __type Length () {
  52.     return sqrt(x*x + y*y);
  53.   }
  54.   __type Len () {
  55.     return Length();
  56.   }
  57.  
  58.   void Unpack(__type* u) {
  59.     u[0] = x;
  60.     u[1] = y;
  61.   }
  62. };
  63.  
  64. template struct Vector2<GLfloat>;
  65. template struct Vector2<GLdouble>;
  66.  
  67. typedef Vector2<GLfloat> Vector2f;
  68. typedef Vector2<GLdouble> Vector2d;
  69.  
  70. template <class __type>
  71. ostream& operator << (ostream& s, const Vector2<__type>& a) {
  72.   return s << "(" << a.x << "," << a.y << ")";
  73. }
  74.  
  75. template <class __type> inline Vector2<__type>
  76. operator + (const Vector2<__type>& a) __attribute__ ((const));
  77.  
  78. template <class __type> inline Vector2<__type>
  79. operator + (const Vector2<__type>& a) {
  80.   return a;
  81. }
  82.  
  83. template <class __type> inline Vector2<__type>
  84. operator - (const Vector2<__type>& a) __attribute__ ((const));
  85.  
  86. template <class __type> inline Vector2<__type>
  87. operator - (const Vector2<__type>& a) {
  88.   return Vector2<__type> (-a.x, -a.y);
  89. }
  90.  
  91. template <class __type> inline Vector2<__type>
  92. operator + (const Vector2<__type>& a, const Vector2<__type>& b) __attribute__ ((const));
  93.  
  94. template <class __type> inline Vector2<__type>
  95. operator + (const Vector2<__type>& a, const Vector2<__type>& b) {
  96.   return Vector2<__type> (a.x+b.x, a.y+b.y);
  97. }
  98.  
  99. template <class __type> inline Vector2<__type>
  100. operator - (const Vector2<__type>& a, const Vector2<__type>& b) __attribute__ ((const));
  101.  
  102. template <class __type> inline Vector2<__type>
  103. operator - (const Vector2<__type>& a, const Vector2<__type>& b) {
  104.   return Vector2<__type> (a.x-b.x, a.y-b.y);
  105. }
  106.  
  107. template <class __type> inline Vector2<__type>
  108. operator * (const Vector2<__type>& a, __type s) __attribute__ ((const));
  109.  
  110. template <class __type> inline Vector2<__type>
  111. operator * (const Vector2<__type>& a, __type s) {
  112.   return Vector2<__type> (a.x*s, a.y*s);
  113. }
  114.  
  115. template <class __type> inline Vector2<__type>
  116. operator * (__type s, const Vector2<__type>& a) __attribute__ ((const));
  117.  
  118. template <class __type> inline Vector2<__type>
  119. operator * (__type s, const Vector2<__type>& a)
  120. {
  121.   return Vector2<__type> (s*a.x, s*a.y);
  122. }
  123.  
  124. template <class __type> inline Vector2<__type>
  125. operator * (const Vector2<__type>& a, const Vector2<__type>& b) __attribute__ ((const));
  126.  
  127. template <class __type> inline Vector2<__type>
  128. operator * (const Vector2<__type>& a, const Vector2<__type>& b) {
  129.   return Vector2<__type> (a.x*b.x, a.y*b.y);
  130. }
  131.  
  132. template <class __type> inline __type
  133. Dot (const Vector2<__type>& a, const Vector2<__type>& b) __attribute__ ((const));
  134.  
  135. template <class __type> inline __type
  136. Dot (const Vector2<__type>& a, const Vector2<__type>& b) {
  137.   return (a.x*b.x + a.y*b.y);
  138. }
  139.  
  140. } // extern "C++"
  141.  
  142. #endif 
  143.