Vector2.hpp

Go to the documentation of this file.
00001 // Two dimensional vector template
00002 
00003 #ifndef __Vector2T_HPP
00004 #define __Vector2T_HPP
00005 
00006 #include "Math.hpp"
00007 #include <iostream>
00008 
00009 template <class T>
00010 class Vector2T
00011 {
00012 public:
00013       T   x;                    // x-component of the vector
00014       T   y;                    // y-component of the vector
00015 
00016             Vector2T  ()                  {}
00017             Vector2T  (float cx, float cy)        { x = (T)cx;  y = (T)cy;                }
00018             Vector2T  (double cx, double cy)        { x = (T)(cx);  y = (T)(cy);              }
00019 template <class S>    Vector2T  (const Vector2T<S>& v)        { x = (T)v.x; y = (T)v.y; }
00020 template <class S>    Vector2T& operator=	(const Vector2T<S>& v)  { x = (T)v.x; y = (T)v.y; return *this;     }
00021       
00022       Vector2T& clear   (void)              { x = (T)(0);  y = (T)(0);  return *this; }
00023       Vector2T& make    (double cx, double cy)      { x = (T)(cx); y = (T)(cy); return *this; }
00024       Vector2T& make    (double xy)           { x = (T)(xy); y = (T)(xy); return *this; }
00025       bool    operator==  (const Vector2T& src) const   { return (x == src.x && y == src.y);      }
00026       bool    operator!=  (const Vector2T& src) const   { return !(x == src.x && y == src.y);     }
00027           Vector2T& operator+=	(const Vector2T& v)        { x += v.x, y += v.y; return *this;       }   
00028           Vector2T& operator-=	(const Vector2T& v)        { x -= v.x, y -= v.y; return *this;       }
00029           Vector2T& operator*=	(double s)           { x = (T)(x*s), y = (T)(y*s); return *this; }
00030       Vector2T& operator/=	(double s)           { s = 1.0/s; x = (T)(x*s), y = (T)(y*s); return *this; }
00031       bool    isOne   (void) const          { return (x == 1.0f && y == 1.0f); }
00032       bool    isZero    (void) const          { return (x == 0.0f && y == 0.0f); }
00033       bool    isValid   (void) const          { return _finite(x) && _finite(y);  }
00034       double    length    (void) const          { return sqrt(x*x+y*y); }
00035       double    lengthSqr (void) const          { return x*x+y*y; }
00036           Vector2T& negate    (void)              { x=-x; y=-y; return *this; }
00037           Vector2T& normalize (double len = 1.0)        { double l = length(); if (l!=0.0) *this *= (len/l); else { x = 1; y = 0; } return *this; }
00038           Vector2T& scale   (const Vector2T& v)       { x *= v.x, y *= v.y; return *this; }
00039           Vector2T& descale   (const Vector2T& v)       { x /= v.x, y /= v.y; return *this; }
00040           Vector2T& rotate    (double s, double c)      { T t = x; x = (T)(x*c+y*-s); y = (T)(t*s+y*c); return *this; }
00041           Vector2T& rotate    (double angle)          { return rotate(sin(angle), cos(angle)); }
00042       double    angle   (void) const          { return atan2(y, x); }
00043       Vector2T& clampUnit (void)              { if(x <= (T)0.0) x = (T)0.0; else if(x >= (T)1.0) x = (T)1.0; if(y <= (T)0.0) y = (T)0.0; else if(y >= (T)1.0) y = (T)1.0; return *this; }
00044   const T&      operator[]	(int i) const          { return ((T*)this)[i]; }
00045       T&      operator[]	(int i)              { return ((T*)this)[i]; }
00046 };
00047 
00048 template <class T> inline Vector2T<T> operator+	(const Vector2T<T>& v1, const Vector2T<T>& v2)  { return Vector2T<T>(v1.x+v2.x, v1.y+v2.y); }
00049 template <class T> inline Vector2T<T> operator-	(const Vector2T<T>& v1, const Vector2T<T>& v2)  { return Vector2T<T>(v1.x-v2.x, v1.y-v2.y); }
00050 template <class T> inline Vector2T<T> operator*	(const Vector2T<T>& v, const double s)    { return Vector2T<T>(v.x*s, v.y*s); }
00051 template <class T> inline Vector2T<T> operator*	(const double s, const Vector2T<T>& v)    { return v*s; }
00052 template <class T> inline Vector2T<T> operator/	(const Vector2T<T>& v, const double s)    { double r = 1.0/s; return v*r; }
00053 template <class T> inline Vector2T<T> operator-	(const Vector2T<T>& v)            { return Vector2T<T>(-v.x, -v.y); }
00054 
00055 template <class T>
00056 float abs(Vector2T<T> t)
00057 {
00058   return t.length();
00059 }
00060 
00061 template <class T>
00062 std::ostream &operator<<(std::ostream &os, const Vector2T<T> &t)
00063 {
00064   os << t.x << ' ' << t.y;
00065   return os;
00066 }
00067 
00068 template <class T>
00069 std::istream &operator>>(std::istream &is, Vector2T<T> &t)
00070 {
00071   is >> t.x;
00072   is >> t.y;
00073   return is;
00074 }
00075 
00076 typedef Vector2T<float> Vector2; 
00077 inline Vector2 randOnCircle() { float a = 2*pi*urand(); return Vector2(cos(a),sin(a)); }
00078 
00079 template <class T>
00080 inline T dot(const Vector2T<T>& a, const Vector2T<T>& b)
00081 {
00082   return a.x*b.x+a.y*b.y;
00083 }
00084 
00085 template <class T>
00086 inline float polygonArea(Vector2T<T> *arr, int n, const Vector2 &ctr)
00087 {
00088   float s = 0.0f;
00089   
00090 
00091   for(int i = 0; i < n; i++)
00092   {
00093     s += (arr[i].x-ctr.x) * (arr[(i+1) % n].y-ctr.y) - (arr[(i+1) % n].x-ctr.x) * (arr[i].y-ctr.y);
00094   }
00095   s /= 2.0f;
00096   return s;
00097 }
00098 
00099 
00100 #endif

Generated on Mon Mar 12 21:09:00 2007 for VEE - The Visual Effects Engine by  doxygen 1.4.6