Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

vector2d.hxx

Go to the documentation of this file.
00001 //  $Id: vector2d.hxx,v 1.5 2003/01/04 20:12:38 grumbel Exp $
00002 // 
00003 //  Construo - A wire-frame construction gamee
00004 //  Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 // 
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #ifndef HEADER_VECTOR2D_HXX
00021 #define HEADER_VECTOR2D_HXX
00022 
00023 #include <math.h>
00024 #include <iostream>
00025 
00027 class Vector2d
00028 {
00029 public:
00030   float x;
00031   float y;
00032   
00033   Vector2d ()
00034     : x(0), y(0)
00035   {}
00036 
00037   Vector2d (float x_, float y_) 
00038     : x (x_), y (y_)
00039   {}
00040 
00041   inline 
00042   void operator+= (const Vector2d& vec) {
00043     x += vec.x;
00044     y += vec.y;
00045   }
00046 
00047   inline 
00048   void operator-= (const Vector2d& vec) {
00049     x -= vec.x;
00050     y -= vec.y;
00051   }
00052 
00053   inline 
00054   void operator*= (float f) {
00055     x *= f;
00056     y *= f;
00057   }
00058 
00059   inline 
00060   Vector2d operator+ (const Vector2d& vec) const  {
00061     return Vector2d(x + vec.x, y + vec.y);
00062   }
00063 
00064   inline
00065   float dot(const Vector2d& vec) const {
00066     return (x * vec.x) + (y * vec.y);
00067   }
00068 
00069   inline 
00070   Vector2d operator- () const  {
00071     return Vector2d(-x, -y);
00072   }
00073 
00074   inline 
00075   Vector2d operator- (const Vector2d& vec) const  {
00076     return Vector2d(x - vec.x, y - vec.y);
00077   }
00078 
00079   inline 
00080   Vector2d operator* (float f) const  {
00081     return Vector2d(x * f, y * f);
00082   }
00083 
00084   inline 
00085   float norm() const {
00086     return sqrt (x*x + y*y);
00087   }
00088 
00089 
00090   inline 
00091   void normalize() {
00092     float f = norm();
00093     if (f!=0)
00094       {
00095         x /= f;
00096         y /= f;
00097       }
00098   }
00099 
00100 };
00101 
00102 inline 
00103 std::ostream& operator << (std::ostream& os, const Vector2d& v)
00104 {
00105   return os << "[" << v.x << ", " << v.y << "]";
00106 }
00107 
00108 #endif
00109 
00110 /* EOF */

Generated on Thu Jul 24 10:24:31 2003 for Construo by doxygen1.3-rc3