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