Claw 1.7.3
coordinate_2d.tpp
1/*
2 CLAW - a C++ Library Absolutely Wonderful
3
4 CLAW is a free library without any particular aim but being useful to
5 anyone.
6
7 Copyright (C) 2005-2011 Julien Jorge
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
23 contact: julien.jorge@gamned.org
24*/
25/**
26 * \file coordinate_2d.tpp
27 * \brief Implementation of claw::math::coordinate_2d class.
28 * \author Julien Jorge
29 */
30#include <cmath>
31
32/*----------------------------------------------------------------------------*/
33/**
34 * \brief Constructor.
35 */
36template<typename T>
37claw::math::coordinate_2d<T>::coordinate_2d()
38{
39
40} // coordinate_2d::coordinate_2d() [constructor]
41
42/*----------------------------------------------------------------------------*/
43/**
44 * \brief Copy constructor.
45 */
46template<typename T>
47template<typename U>
48claw::math::coordinate_2d<T>::coordinate_2d(const coordinate_2d<U>& that)
49 : x(that.x), y(that.y)
50{
51
52} // coordinate_2d::coordinate_2d() [copy constructor]
53
54/*----------------------------------------------------------------------------*/
55/**
56 * \brief Constructor with initialization.
57 * \param _x x value.
58 * \param _y y Value.
59 */
60template<typename T>
61claw::math::coordinate_2d<T>::coordinate_2d
62(const value_type& _x, const value_type& _y)
63 : x(_x), y(_y)
64{
65
66} // coordinate_2d::coordinate_2d() [constructor whit values]
67
68/*----------------------------------------------------------------------------*/
69/**
70 * \brief Get a copy of the rectangle by converting its members to a given type.
71 *
72 * Consider the following code:
73 *
74 * <tt> coordinate_2d<float> a;
75 *
76 * ...
77 *
78 * coordinate_2d<int> b(a); </tt>
79 *
80 * The copy constructor will be called, and your compiler should print some
81 * warnings in your console. These warnings have a meaning, so we don't wan't to
82 * make them disapear by adding explicit type conversion inside the
83 * coordinate_2d class nor adding a cast operator that will be used silently by
84 * the compiler.
85 *
86 * If you really want to convert the type, this method will explicitly cast the
87 * member variables.
88 */
89template<class T>
90template<typename U>
91claw::math::coordinate_2d<U>
92claw::math::coordinate_2d<T>::cast_value_type_to() const
93{
94 return claw::math::coordinate_2d<U>( (U)x, (U)y );
95} // coordinate_2d::cast_value_type_to()
96
97/*----------------------------------------------------------------------------*/
98/**
99 * \brief Sets new values to the coordinate.
100 * \param _x New x value.
101 * \param _y New y Value.
102 */
103template<typename T>
104void
105claw::math::coordinate_2d<T>::set(const value_type& _x, const value_type& _y)
106{
107 x = _x;
108 y = _y;
109} // coordinate_2d::set()
110
111/*----------------------------------------------------------------------------*/
112/**
113 * \brief Get the distance separing two coordinates.
114 * \param p The second coordinate
115 */
116template<typename T>
117typename claw::math::coordinate_2d<T>::value_type
118claw::math::coordinate_2d<T>::distance(const self_type& p) const
119{
120 return (value_type)sqrt( (p.x - x)*(p.x - x) + (p.y - y)*(p.y - y) );
121} // coordinate_2d::distance()
122
123/*----------------------------------------------------------------------------*/
124/**
125 * \brief Rotate this point around an other point.
126 * \param center The other point.
127 * \param angle The angle of the rotation.
128 */
129template<typename T>
130void
131claw::math::coordinate_2d<T>::rotate( const self_type& center, double angle )
132{
133 self_type result(center);
134
135 result.x +=
136 (x - center.x) * std::cos(angle) - (y - center.y) * std::sin(angle);
137 result.y +=
138 (x - center.x) * std::sin(angle) + (y - center.y) * std::cos(angle);
139
140 *this = result;
141} // coordinate_2d::rotate()
142
143/*----------------------------------------------------------------------------*/
144/**
145 * \brief Get the angle of the slope starting from this and ending with an other
146 * coordinate.
147 * \param to The other point.
148 */
149template<typename T>
150double claw::math::coordinate_2d<T>::slope_angle( const self_type& to ) const
151{
152 return std::atan2( to.y - y, to.x - x );
153} // coordinate_2d::slope_angle()
154
155/*----------------------------------------------------------------------------*/
156/**
157 * \brief Equality operator.
158 * \param that Coordinate to compare to.
159 */
160template<typename T>
161bool claw::math::coordinate_2d<T>::operator==(const self_type& that) const
162{
163 return (x == that.x) && (y == that.y);
164} // coordinate_2d::operator==()
165
166/*----------------------------------------------------------------------------*/
167/**
168 * \brief Difference operator.
169 * \param that Coordinate to compare to.
170 */
171template<typename T>
172bool claw::math::coordinate_2d<T>::operator!=(const self_type& that) const
173{
174 return !(*this == that);
175} // coordinate_2d::operator!=()
176
177/*----------------------------------------------------------------------------*/
178/**
179 * \brief Addition.
180 * \param that Coordinate to add.
181 */
182template<typename T>
183claw::math::coordinate_2d<T>
184claw::math::coordinate_2d<T>::operator+(const self_type& that) const
185{
186 return self_type( x + that.x, y + that.y );
187} // coordinate_2d::operator+()
188
189/*----------------------------------------------------------------------------*/
190/**
191 * \brief Subtraction.
192 * \param that Coordinate to subtract.
193 */
194template<typename T>
195claw::math::coordinate_2d<T>
196claw::math::coordinate_2d<T>::operator-(const self_type& that) const
197{
198 return self_type( x - that.x, y - that.y );
199} // coordinate_2d::operator-()
200
201/*----------------------------------------------------------------------------*/
202/**
203 * \brief Add a coordinate.
204 * \param that Coordinate to add.
205 */
206template<typename T>
207claw::math::coordinate_2d<T>&
208claw::math::coordinate_2d<T>::operator+=(const self_type& that)
209{
210 x += that.x;
211 y += that.y;
212
213 return *this;
214} // coordinate_2d::operator+=()
215
216/*----------------------------------------------------------------------------*/
217/**
218 * \brief Subtract a coordinate.
219 * \param that Coordinate to subtract.
220 */
221template<typename T>
222claw::math::coordinate_2d<T>&
223claw::math::coordinate_2d<T>::operator-=(const self_type& that)
224{
225 x -= that.x;
226 y -= that.y;
227
228 return *this;
229} // coordinate_2d::operator-=()
230
231/*----------------------------------------------------------------------------*/
232/**
233 * \brief Multiplication.
234 * \param v Factor.
235 */
236template<typename T>
237claw::math::coordinate_2d<T>
238claw::math::coordinate_2d<T>::operator*(const value_type& v) const
239{
240 return self_type( x * v, y * v );
241} // coordinate_2d::operator*()
242
243/*----------------------------------------------------------------------------*/
244/**
245 * \brief Division.
246 * \param v Divider.
247 */
248template<typename T>
249claw::math::coordinate_2d<T>
250claw::math::coordinate_2d<T>::operator/(const value_type& v) const
251{
252 return self_type( x / v, y / v );
253} // coordinate_2d::operator/()
254
255/*----------------------------------------------------------------------------*/
256/**
257 * \brief Multiply the coordinates.
258 * \param v Factor.
259 */
260template<typename T>
261claw::math::coordinate_2d<T>&
262claw::math::coordinate_2d<T>::operator*=(const value_type& v)
263{
264 x *= v;
265 y *= v;
266
267 return *this;
268} // coordinate_2d::operator*=()
269
270/*----------------------------------------------------------------------------*/
271/**
272 * \brief Divide the coordinates.
273 * \param v Divider.
274 */
275template<typename T>
276claw::math::coordinate_2d<T>&
277claw::math::coordinate_2d<T>::operator/=(const value_type& v)
278{
279 x /= v;
280 y /= v;
281
282 return *this;
283} // coordinate_2d::operator/=()
284
285/*----------------------------------------------------------------------------*/
286/**
287 * \brief Unary minus.
288 * \param that The operand...
289 */
290template<typename T>
291claw::math::coordinate_2d<T>
292claw::math::operator-( const claw::math::coordinate_2d<T>& that )
293{
294 return claw::math::coordinate_2d<T>(-that.x, -that.y);
295} // operator-() [coordinate_2d]
296
297/*----------------------------------------------------------------------------*/
298/**
299 * \brief Multiply coordinates.
300 * \param v The multiplicator.
301 * \param self The coordinates to multiply.
302 */
303template<typename T, typename U>
304claw::math::coordinate_2d<T>
305claw::math::operator*( U v, const coordinate_2d<T>& self )
306{
307 return self * typename coordinate_2d<T>::value_type(v);
308} // operator*() [coordinate_2d]