src/math/vector.hpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #ifndef HEADER_SUPERTUX_MATH_VECTOR_HPP
00018 #define HEADER_SUPERTUX_MATH_VECTOR_HPP
00019 
00021 class Vector
00022 {
00023 public:
00024   Vector(float nx, float ny)
00025     : x(nx), y(ny)
00026   { }
00027   Vector(const Vector& other)
00028     : x(other.x), y(other.y)
00029   { }
00030   Vector()
00031     : x(0), y(0)
00032   { }
00033 
00034   bool operator ==(const Vector& other) const
00035   {
00036     return x == other.x && y == other.y;
00037   }
00038 
00039   bool operator !=(const Vector& other) const
00040   {
00041     return !(x == other.x && y == other.y);
00042   }
00043 
00044   const Vector& operator=(const Vector& other)
00045   {
00046     x = other.x;
00047     y = other.y;
00048     return *this;
00049   }
00050 
00051   Vector operator+(const Vector& other) const
00052   {
00053     return Vector(x + other.x, y + other.y);
00054   }
00055 
00056   Vector operator-(const Vector& other) const
00057   {
00058     return Vector(x - other.x, y - other.y);
00059   }
00060 
00061   Vector operator*(float s) const
00062   {
00063     return Vector(x * s, y * s);
00064   }
00065 
00066   Vector operator/(float s) const
00067   {
00068     return Vector(x / s, y / s);
00069   }
00070 
00071   Vector operator-() const
00072   {
00073     return Vector(-x, -y);
00074   }
00075 
00076   const Vector& operator +=(const Vector& other)
00077   {
00078     x += other.x;
00079     y += other.y;
00080     return *this;
00081   }
00082 
00083   const Vector& operator -=(const Vector& other)
00084   {
00085     x -= other.x;
00086     y -= other.y;
00087     return *this;
00088   }
00089 
00090   const Vector& operator *=(float val)
00091   {
00092     x *= val;
00093     y *= val;
00094     return *this;
00095   }
00096 
00097   const Vector& operator /=(float val)
00098   {
00099     x /= val;
00100     y /= val;
00101     return *this;
00102   }
00103 
00105   float operator*(const Vector& other) const
00106   {
00107     return x*other.x + y*other.y;
00108   }
00109 
00110   float norm() const;
00111   Vector unit() const;
00112 
00113   // ... add the other operators as needed, I'm too lazy now ...
00114 
00115   float x, y; // leave this public, get/set methods just give me headaches
00116   // for such simple stuff :)
00117 };
00118 
00119 #endif
00120 
00121 /* EOF */

Generated on Mon Jun 9 03:38:18 2014 for SuperTux by  doxygen 1.5.1