Vector2.hpp

Go to the documentation of this file.
00001 
00002 //                                                                           //
00003 // Copyright 1998-2000 by Janne Kontkanen. All Rights reserved. Usage or     //
00004 // redistribution of this material in source or binary form is prohibited    //
00005 // unless otherwise agreed.                                                  //
00006 //                                                                           //
00008 
00009 // Two dimensional vector template
00010 
00011 #ifndef __Vector2T_HPP
00012 #define __Vector2T_HPP
00013 
00014 #include <iostream>
00015 
00016 template <class T>
00017 class Vector2T
00018 {
00019 public:
00020       T   x;                    // x-component of the vector
00021       T   y;                    // y-component of the vector
00022 
00023             Vector2T  ()                  {}
00024             Vector2T  (float cx, float cy)        { x = (T)cx;  y = (T)cy;                }
00025             Vector2T  (double cx, double cy)        { x = (T)(cx);  y = (T)(cy);              }
00026 template <class S>    Vector2T  (const Vector2T<S>& v)        { x = (T)v.x; y = (T)v.y; }
00027 template <class S>    Vector2T& operator=	(const Vector2T<S>& v)  { x = (T)v.x; y = (T)v.y; return *this;     }
00028       
00029       Vector2T& clear   (void)              { x = (T)(0);  y = (T)(0);  return *this; }
00030       Vector2T& make    (double cx, double cy)      { x = (T)(cx); y = (T)(cy); return *this; }
00031       Vector2T& make    (double xy)           { x = (T)(xy); y = (T)(xy); return *this; }
00032       bool    operator==  (const Vector2T& src) const   { return (x == src.x && y == src.y);      }
00033       bool    operator!=  (const Vector2T& src) const   { return !(x == src.x && y == src.y);     }
00034           Vector2T& operator+=	(const Vector2T& v)        { x += v.x, y += v.y; return *this;       }   
00035           Vector2T& operator-=	(const Vector2T& v)        { x -= v.x, y -= v.y; return *this;       }
00036           Vector2T& operator*=	(double s)           { x = (T)(x*s), y = (T)(y*s); return *this; }
00037       Vector2T& operator/=	(double s)           { s = 1.0/s; x = (T)(x*s), y = (T)(y*s); return *this; }
00038       bool    isOne   (void) const          { return (x == 1.0f && y == 1.0f); }
00039       bool    isZero    (void) const          { return (x == 0.0f && y == 0.0f); }
00040       bool    isValid   (void) const          { return _finite(x) && _finite(y);  }
00041       double    length    (void) const          { return sqrt(x*x+y*y); }
00042       double    lengthSqr (void) const          { return x*x+y*y; }
00043           Vector2T& negate    (void)              { x=-x; y=-y; return *this; }
00044           Vector2T& normalize (double len = 1.0)        { double l = length(); if (l!=0.0) *this *= (len/l); return *this; }
00045           Vector2T& scale   (const Vector2T& v)       { x *= v.x, y *= v.y; return *this; }
00046           Vector2T& descale   (const Vector2T& v)       { x /= v.x, y /= v.y; return *this; }
00047           Vector2T& rotate    (double s, double c)      { T t = x; x = (T)(x*c+y*-s); y = (T)(t*s+y*c); return *this; }
00048           Vector2T& rotate    (double angle)          { return rotate(sin(angle), cos(angle)); }
00049       double    angle   (void) const          { return atan2(y, x); }
00050       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; }
00051   const T&      operator[]	(int i) const          { return ((T*)this)[i]; }
00052       T&      operator[]	(int i)              { return ((T*)this)[i]; }
00053 };
00054 
00055 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); }
00056 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); }
00057 template <class T> inline Vector2T<T> operator*	(const Vector2T<T>& v, const double s)    { return Vector2T<T>(v.x*s, v.y*s); }
00058 template <class T> inline Vector2T<T> operator*	(const double s, const Vector2T<T>& v)    { return v*s; }
00059 template <class T> inline Vector2T<T> operator/	(const Vector2T<T>& v, const double s)    { double r = 1.0/s; return v*r; }
00060 template <class T> inline Vector2T<T> operator-	(const Vector2T<T>& v)            { return Vector2T<T>(-v.x, -v.y); }
00061 
00062 template <class T>
00063 float abs(Vector2T<T> t)
00064 {
00065   return t.length();
00066 }
00067 
00068 template <class T>
00069 std::ostream &operator<<(std::ostream &os, const Vector2T<T> &t)
00070 {
00071   os << t.x << ' ' << t.y;
00072   return os;
00073 }
00074 
00075 template <class T>
00076 std::istream &operator>>(std::istream &is, Vector2T<T> &t)
00077 {
00078   is >> t.x;
00079   is >> t.y;
00080   return is;
00081 }
00082 
00083 typedef Vector2T<float> Vector2; 
00084 
00085 #endif

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