argh, clean out copy
[supertux.git] / src / unison / include / unison / video / Coord.hpp
1 //          Copyright Timothy Goya 2007.
2 // Distributed under the Boost Software License, Version 1.0.
3 //    (See accompanying file LICENSE_1_0.txt or copy at
4 //          http://www.boost.org/LICENSE_1_0.txt)
5
6 #ifndef UNISON_VIDEO_COORD_HPP
7 #define UNISON_VIDEO_COORD_HPP
8
9 namespace Unison
10 {
11    namespace Video
12    {
13       /// A 2 dimensional quantity in rectangular space
14       template<typename T> class Coord
15       {
16          public:
17             /// The horizontal value
18             T x;
19
20             /// The vertical value
21             T y;
22
23             /// Default constructor (0, 0)
24             Coord()
25              : x(),
26                y()
27             {
28             }
29
30             /// Create a coordinate from the given values
31             /// \param[in] x The horizontal value
32             /// \param[in] y The vertical value
33             Coord(T x, T y)
34              : x(x),
35                y(y)
36             {
37             }
38
39             /// Copy constructor
40             /// \param[in] rhs The source coordinate
41             template<typename V> Coord(const Coord<V> &rhs)
42              : x(rhs.x),
43                y(rhs.y)
44             {
45             }
46
47             /// Assignment operator
48             /// \param[in] rhs The source color
49             template<typename V> Coord<T> &operator =(const Coord<V> &rhs)
50             {
51                x = rhs.x;
52                y = rhs.y;
53                return *this;
54             }
55
56             /// Equality operator
57             /// \param[in] rhs The coordinate to test
58             /// \return Whether the coordinates are equal
59             template<typename V> bool operator ==(const Coord<V> &rhs) const
60             {
61                return x == rhs.x && y == rhs.y;
62             }
63
64             /// Equality operator
65             /// \param[in] rhs The coordinate to test
66             /// \return Whether the coordinates are not equal
67             template<typename V> bool operator !=(const Coord<V> &rhs) const
68             {
69                return !(*this == rhs);
70             }
71
72             /// Add two coordinates and assign to the left operand
73             /// \param[in] rhs The right operand
74             /// \return The resultant coordinate
75             template<typename V> Coord<T> &operator +=(const Coord<V> &rhs)
76             {
77                x += rhs.x;
78                y += rhs.y;
79                return *this;
80             }
81
82             /// Subtract two coordinates and assign to the left operand
83             /// \param[in] rhs The right operand
84             /// \return The resultant coordinate
85             template<typename V> Coord<T> &operator -=(const Coord<V> &rhs)
86             {
87                x -= rhs.x;
88                y -= rhs.y;
89                return *this;
90             }
91
92             /// Multiply two coordinates and assign to the left operand
93             /// \param[in] rhs The right operand
94             /// \return The resultant coordinate
95             template<typename V> Coord<T> &operator *=(const Coord<V> &rhs)
96             {
97                x *= rhs.x;
98                y *= rhs.y;
99                return *this;
100             }
101
102             /// Multiply two coordinates and assign to the left operand
103             /// \param[in] rhs The right operand
104             /// \return The resultant coordinate
105             template<typename V> Coord<T> &operator /=(const Coord<V> &rhs)
106             {
107                x /= rhs.x;
108                y /= rhs.y;
109                return *this;
110             }
111
112             /// Add a coordinate with a scalar and assign to the left operand
113             /// \param[in] rhs The right operand
114             /// \return The resultant coordinate
115             template<typename V> Coord<T> &operator +=(const V &rhs)
116             {
117                x += rhs;
118                y += rhs;
119                return *this;
120             }
121
122             /// Subtract a coordinate with a scalar and assign to the left operand
123             /// \param[in] rhs The right operand
124             /// \return The resultant coordinate
125             template<typename V> Coord<T> &operator -=(const V &rhs)
126             {
127                x -= rhs;
128                y -= rhs;
129                return *this;
130             }
131
132             /// Multiply a coordinate with a scalar and assign to the left operand
133             /// \param[in] rhs The right operand
134             /// \return The resultant coordinate
135             template<typename V> Coord<T> &operator *=(const V &rhs)
136             {
137                x *= rhs;
138                y *= rhs;
139                return *this;
140             }
141
142             /// Divide a coordinate with a scalar and assign to the left operand
143             /// \param[in] rhs The right operand
144             /// \return The resultant coordinate
145             template<typename V> Coord<T> &operator /=(const V &rhs)
146             {
147                x /= rhs;
148                y /= rhs;
149                return *this;
150             }
151
152             /*T length()
153             {
154                double sq = x*x + y*y;
155                return T(sqrt(sq) + 0.5);
156             }*/
157       };
158
159       /// Add two coordinates
160       /// \param[in] lhs The left operand
161       /// \param[in] rhs The right operand
162       /// \return The resultant coordinate
163       template<typename T, typename V> const Coord<T> operator +(const Coord<T> &lhs, const Coord<V> &rhs)
164       {
165          return Coord<T>(lhs) += rhs;
166       }
167
168       /// Subtract two coordinates
169       /// \param[in] lhs The left operand
170       /// \param[in] rhs The right operand
171       /// \return The resultant coordinate
172       template<typename T, typename V> const Coord<T> operator -(const Coord<T> &lhs, const Coord<V> &rhs)
173       {
174          return Coord<T>(lhs) -= rhs;
175       }
176
177       /// Multiply two coordinates
178       /// \param[in] lhs The left operand
179       /// \param[in] rhs The right operand
180       /// \return The resultant coordinate
181       template<typename T, typename V> const Coord<T> operator *(const Coord<T> &lhs, const Coord<V> &rhs)
182       {
183          return Coord<T>(lhs) *= rhs;
184       }
185
186       /// Divide two coordinates
187       /// \param[in] lhs The left operand
188       /// \param[in] rhs The right operand
189       /// \return The resultant coordinate
190       template<typename T, typename V> const Coord<T> operator /(const Coord<T> &lhs, const Coord<V> &rhs)
191       {
192          return Coord<T>(lhs) /= rhs;
193       }
194
195       /// Add a coordinate with a scalar
196       /// \param[in] lhs The left operand
197       /// \param[in] rhs The right operand
198       /// \return The resultant coordinate
199       template<typename T, typename V> const Coord<T> operator +(const Coord<T> &lhs, const V &rhs)
200       {
201          return Coord<T>(lhs) += rhs;
202       }
203
204       /// Subtract a coordinate with a scalar
205       /// \param[in] lhs The left operand
206       /// \param[in] rhs The right operand
207       /// \return The resultant coordinate
208       template<typename T, typename V> const Coord<T> operator -(const Coord<T> &lhs, const V &rhs)
209       {
210          return Coord<T>(lhs) -= rhs;
211       }
212
213       /// Multiply a coordinate with a scalar
214       /// \param[in] lhs The left operand
215       /// \param[in] rhs The right operand
216       /// \return The resultant coordinate
217       template<typename T, typename V> const Coord<T> operator *(const Coord<T> &lhs, const V &rhs)
218       {
219          return Coord<T>(lhs) *= rhs;
220       }
221
222       /// Divide a coordinate with a scalar
223       /// \param[in] lhs The left operand
224       /// \param[in] rhs The right operand
225       /// \return The resultant coordinate
226       template<typename T, typename V> const Coord<T> operator /(const Coord<T> &lhs, const V &rhs)
227       {
228          return Coord<T>(lhs) /= rhs;
229       }
230
231       /// Add a coordinate with a scalar
232       /// \param[in] lhs The left operand
233       /// \param[in] rhs The right operand
234       /// \return The resultant coordinate
235       template<typename V, typename T> const Coord<T> operator +(const V &lhs, const Coord<T> &rhs)
236       {
237          return Coord<T>(rhs) += lhs;
238       }
239
240       /// Subtract a coordinate with a scalar
241       /// \param[in] lhs The left operand
242       /// \param[in] rhs The right operand
243       /// \return The resultant coordinate
244       template<typename V, typename T> const Coord<T> operator -(const V &lhs, const Coord<T> &rhs)
245       {
246          return Coord<T>(rhs) -= lhs;
247       }
248
249       /// Multiply a coordinate with a scalar
250       /// \param[in] lhs The left operand
251       /// \param[in] rhs The right operand
252       /// \return The resultant coordinate
253       template<typename V, typename T> const Coord<T> operator *(const V &lhs, const Coord<T> &rhs)
254       {
255          return Coord<T>(rhs) *= lhs;
256       }
257
258       /// Divide a coordinate with a scalar
259       /// \param[in] lhs The left operand
260       /// \param[in] rhs The right operand
261       /// \return The resultant coordinate
262       template<typename V, typename T> const Coord<T> operator /(const V &lhs, const Coord<T> &rhs)
263       {
264          return Coord<T>(rhs) /= lhs;
265       }
266
267       /// A point in 2-dimensional space
268       typedef Coord<int> Point;
269
270       /// A 2-dimensional area
271       typedef Coord<unsigned int> Area;
272    }
273 }
274
275 #endif