817bb4d96876717a3fefcfed891270f71373a5b7
[supertux.git] / src / math / vector.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #ifndef SUPERTUX_VECTOR_H
21 #define SUPERTUX_VECTOR_H
22
23 /** Simple two dimensional vector. */
24 class Vector
25 {
26 public:
27   Vector(float nx, float ny)
28       : x(nx), y(ny)
29   { }
30   Vector(const Vector& other)
31       : x(other.x), y(other.y)
32   { }
33   Vector()
34       : x(0), y(0)
35   { }
36
37   bool operator ==(const Vector& other) const
38     {
39       return x == other.x && y == other.y;
40     }
41
42   bool operator !=(const Vector& other) const
43     {
44       return !(x == other.x && y == other.y);
45     }
46
47   const Vector& operator=(const Vector& other)
48   {
49     x = other.x;
50     y = other.y;
51     return *this;
52   }
53
54   Vector operator+(const Vector& other) const
55     {
56       return Vector(x + other.x, y + other.y);
57     }
58
59   Vector operator-(const Vector& other) const
60     {
61       return Vector(x - other.x, y - other.y);
62     }
63
64   Vector operator*(float s) const
65     {
66       return Vector(x * s, y * s);
67     }
68
69   Vector operator/(float s) const
70     {
71       return Vector(x / s, y / s);
72     }
73
74   Vector operator-() const
75     {
76       return Vector(-x, -y);
77     }
78
79   const Vector& operator +=(const Vector& other)
80   {
81     x += other.x;
82     y += other.y;
83     return *this;
84   }
85
86   const Vector& operator -=(const Vector& other)
87   {
88     x -= other.x;
89     y -= other.y;
90     return *this;
91   }
92
93   const Vector& operator *=(float val)
94   {
95     x *= val;
96     y *= val;
97     return *this;
98   }
99
100   const Vector& operator /=(float val)
101   {
102     x /= val;
103     y /= val;
104     return *this;
105   }
106
107   /// Scalar product of 2 vectors
108   float operator*(const Vector& other) const
109     {
110       return x*other.x + y*other.y;
111     }
112
113   float norm() const;
114   Vector unit() const;
115
116   // ... add the other operators as needed, I'm too lazy now ...
117
118   float x, y; // leave this public, get/set methods just give me headaches
119   // for such simple stuff :)
120 };
121
122 #endif