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)
6 #ifndef UNISON_VIDEO_COORD_HPP
7 #define UNISON_VIDEO_COORD_HPP
13 /// A 2 dimensional quantity in rectangular space
14 template<typename T> class Coord
17 /// The horizontal value
20 /// The vertical value
23 /// Default constructor (0, 0)
30 /// Create a coordinate from the given values
31 /// \param[in] x The horizontal value
32 /// \param[in] y The vertical value
40 /// \param[in] rhs The source coordinate
41 template<typename V> Coord(const Coord<V> &rhs)
47 /// Assignment operator
48 /// \param[in] rhs The source color
49 template<typename V> Coord<T> &operator =(const Coord<V> &rhs)
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
61 return x == rhs.x && y == rhs.y;
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
69 return !(*this == rhs);
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)
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)
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)
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)
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)
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)
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)
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)
154 double sq = x*x + y*y;
155 return T(sqrt(sq) + 0.5);
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)
165 return Coord<T>(lhs) += rhs;
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)
174 return Coord<T>(lhs) -= rhs;
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)
183 return Coord<T>(lhs) *= rhs;
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)
192 return Coord<T>(lhs) /= rhs;
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)
201 return Coord<T>(lhs) += rhs;
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)
210 return Coord<T>(lhs) -= rhs;
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)
219 return Coord<T>(lhs) *= rhs;
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)
228 return Coord<T>(lhs) /= rhs;
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)
237 return Coord<T>(rhs) += lhs;
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)
246 return Coord<T>(rhs) -= lhs;
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)
255 return Coord<T>(rhs) *= lhs;
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)
264 return Coord<T>(rhs) /= lhs;
267 /// A point in 2-dimensional space
268 typedef Coord<int> Point;
270 /// A 2-dimensional area
271 typedef Coord<unsigned int> Area;