Vector2D.h

Go to the documentation of this file.
00001 /*
00002 RealTimeBattle, a robot programming game for Unix
00003 Copyright (C) 1998-2000  Erik Ouchterlony and Ragnar Ouchterlony
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software Foundation,
00017 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #ifndef __VECTOR2D__
00021 #define __VECTOR2D__
00022 
00023 #include <iostream>
00024 
00025 using namespace std;
00026 
00027 class Vector2D
00028 {
00029 public:
00030   Vector2D() { vector[0] = 0.0; vector[1] = 0.0; }
00031   Vector2D(const Vector2D& v)  { vector[0] = v.vector[0]; vector[1] = v.vector[1]; }
00032   Vector2D(const double x, const double y) { vector[0] = x; vector[1] = y; }
00033   ~Vector2D() {}
00034   
00035   friend Vector2D operator+(const  Vector2D&, const Vector2D&);
00036   inline friend Vector2D operator-(const Vector2D&, const Vector2D&);
00037   friend Vector2D operator-(const Vector2D&);
00038   Vector2D& operator=(const Vector2D&);
00039   friend int operator==(const Vector2D&, const Vector2D&);
00040   //friend double operator*(const Vector2D&, const Vector2D&);  
00041   friend Vector2D operator*(const Vector2D&, const double);  
00042   friend Vector2D operator*(const double, const Vector2D&);  
00043   friend Vector2D operator/(const Vector2D&, const double);  
00044   friend ostream& operator<<(ostream&, const Vector2D&);  
00045   friend istream& operator>>(istream&, Vector2D&);  
00046 
00047   Vector2D& operator+=(const Vector2D&);
00048   Vector2D& operator-=(const Vector2D&);
00049   Vector2D& operator*=(const double);
00050   Vector2D& operator/=(const double);
00051 
00052   double operator[](int) const;
00053 
00054   friend double length(const Vector2D&);  
00055   inline friend double lengthsqr(const Vector2D&);  
00056   friend double vec2angle(const Vector2D&);
00057   friend Vector2D angle2vec(const double);
00058   Vector2D& normalize();  
00059   friend Vector2D unit(const Vector2D&);  
00060   inline friend double vedge(const Vector2D&, const Vector2D&);  
00061   inline friend double dot(const Vector2D&, const Vector2D&);  
00062   friend Vector2D rotate(const Vector2D&, const double angle);  
00063   friend Vector2D rotate90(const Vector2D&);  
00064 
00065 
00066 private:
00067   double vector[2];
00068 
00069   Vector2D& copy_vector(const Vector2D&);
00070 };
00071 
00072 
00073 // The following functions are critical for the speed of RTB and
00074 // are therefore inlined
00075 
00076 inline Vector2D
00077 operator-(const Vector2D& vec1, const Vector2D& vec2)
00078 {
00079   return( Vector2D( vec1.vector[0] - vec2.vector[0],
00080                     vec1.vector[1] - vec2.vector[1] ) );
00081 }
00082 
00083 
00084 inline double
00085 dot(const Vector2D& vec1, const Vector2D& vec2)
00086 {
00087   return( vec1.vector[0] * vec2.vector[0] + 
00088           vec1.vector[1] * vec2.vector[1] );
00089 }
00090 
00091 inline double
00092 vedge(const Vector2D& vec1, const Vector2D& vec2)
00093 {
00094   return( vec1.vector[0]*vec2.vector[1] - 
00095           vec1.vector[1]*vec2.vector[0] ); 
00096 }
00097 
00098 
00099 inline double
00100 lengthsqr(const Vector2D& vec)
00101 {
00102   return( vec.vector[0]*vec.vector[0] + 
00103           vec.vector[1]*vec.vector[1] );
00104 }
00105 
00106 #endif

Generated on Fri Oct 15 15:47:40 2004 for Real Time Battle by  doxygen 1.3.9.1