2 CLAW - a C++ Library Absolutely Wonderful
4 CLAW is a free library without any particular aim but being useful to
7 Copyright (C) 2005-2011 Julien Jorge
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.
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.
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
23 contact: julien.jorge@gamned.org
26 * \file coordinate_2d.tpp
27 * \brief Implementation of claw::math::coordinate_2d class.
28 * \author Julien Jorge
32 /*----------------------------------------------------------------------------*/
37 claw::math::coordinate_2d<T>::coordinate_2d()
40 } // coordinate_2d::coordinate_2d() [constructor]
42 /*----------------------------------------------------------------------------*/
44 * \brief Copy constructor.
48 claw::math::coordinate_2d<T>::coordinate_2d(const coordinate_2d<U>& that)
49 : x(that.x), y(that.y)
52 } // coordinate_2d::coordinate_2d() [copy constructor]
54 /*----------------------------------------------------------------------------*/
56 * \brief Constructor with initialization.
61 claw::math::coordinate_2d<T>::coordinate_2d
62 (const value_type& _x, const value_type& _y)
66 } // coordinate_2d::coordinate_2d() [constructor whit values]
68 /*----------------------------------------------------------------------------*/
70 * \brief Get a copy of the rectangle by converting its members to a given type.
72 * Consider the following code:
74 * <tt> coordinate_2d<float> a;
78 * coordinate_2d<int> b(a); </tt>
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
86 * If you really want to convert the type, this method will explicitly cast the
91 claw::math::coordinate_2d<U>
92 claw::math::coordinate_2d<T>::cast_value_type_to() const
94 return claw::math::coordinate_2d<U>( (U)x, (U)y );
95 } // coordinate_2d::cast_value_type_to()
97 /*----------------------------------------------------------------------------*/
99 * \brief Sets new values to the coordinate.
100 * \param _x New x value.
101 * \param _y New y Value.
105 claw::math::coordinate_2d<T>::set(const value_type& _x, const value_type& _y)
109 } // coordinate_2d::set()
111 /*----------------------------------------------------------------------------*/
113 * \brief Get the distance separing two coordinates.
114 * \param p The second coordinate
117 typename claw::math::coordinate_2d<T>::value_type
118 claw::math::coordinate_2d<T>::distance(const self_type& p) const
120 return (value_type)sqrt( (p.x - x)*(p.x - x) + (p.y - y)*(p.y - y) );
121 } // coordinate_2d::distance()
123 /*----------------------------------------------------------------------------*/
125 * \brief Rotate this point around an other point.
126 * \param center The other point.
127 * \param angle The angle of the rotation.
131 claw::math::coordinate_2d<T>::rotate( const self_type& center, double angle )
133 self_type result(center);
136 (x - center.x) * std::cos(angle) - (y - center.y) * std::sin(angle);
138 (x - center.x) * std::sin(angle) + (y - center.y) * std::cos(angle);
141 } // coordinate_2d::rotate()
143 /*----------------------------------------------------------------------------*/
145 * \brief Get the angle of the slope starting from this and ending with an other
147 * \param to The other point.
150 double claw::math::coordinate_2d<T>::slope_angle( const self_type& to ) const
152 return std::atan2( to.y - y, to.x - x );
153 } // coordinate_2d::slope_angle()
155 /*----------------------------------------------------------------------------*/
157 * \brief Equality operator.
158 * \param that Coordinate to compare to.
161 bool claw::math::coordinate_2d<T>::operator==(const self_type& that) const
163 return (x == that.x) && (y == that.y);
164 } // coordinate_2d::operator==()
166 /*----------------------------------------------------------------------------*/
168 * \brief Difference operator.
169 * \param that Coordinate to compare to.
172 bool claw::math::coordinate_2d<T>::operator!=(const self_type& that) const
174 return !(*this == that);
175 } // coordinate_2d::operator!=()
177 /*----------------------------------------------------------------------------*/
180 * \param that Coordinate to add.
183 claw::math::coordinate_2d<T>
184 claw::math::coordinate_2d<T>::operator+(const self_type& that) const
186 return self_type( x + that.x, y + that.y );
187 } // coordinate_2d::operator+()
189 /*----------------------------------------------------------------------------*/
191 * \brief Subtraction.
192 * \param that Coordinate to subtract.
195 claw::math::coordinate_2d<T>
196 claw::math::coordinate_2d<T>::operator-(const self_type& that) const
198 return self_type( x - that.x, y - that.y );
199 } // coordinate_2d::operator-()
201 /*----------------------------------------------------------------------------*/
203 * \brief Add a coordinate.
204 * \param that Coordinate to add.
207 claw::math::coordinate_2d<T>&
208 claw::math::coordinate_2d<T>::operator+=(const self_type& that)
214 } // coordinate_2d::operator+=()
216 /*----------------------------------------------------------------------------*/
218 * \brief Subtract a coordinate.
219 * \param that Coordinate to subtract.
222 claw::math::coordinate_2d<T>&
223 claw::math::coordinate_2d<T>::operator-=(const self_type& that)
229 } // coordinate_2d::operator-=()
231 /*----------------------------------------------------------------------------*/
233 * \brief Multiplication.
237 claw::math::coordinate_2d<T>
238 claw::math::coordinate_2d<T>::operator*(const value_type& v) const
240 return self_type( x * v, y * v );
241 } // coordinate_2d::operator*()
243 /*----------------------------------------------------------------------------*/
249 claw::math::coordinate_2d<T>
250 claw::math::coordinate_2d<T>::operator/(const value_type& v) const
252 return self_type( x / v, y / v );
253 } // coordinate_2d::operator/()
255 /*----------------------------------------------------------------------------*/
257 * \brief Multiply the coordinates.
261 claw::math::coordinate_2d<T>&
262 claw::math::coordinate_2d<T>::operator*=(const value_type& v)
268 } // coordinate_2d::operator*=()
270 /*----------------------------------------------------------------------------*/
272 * \brief Divide the coordinates.
276 claw::math::coordinate_2d<T>&
277 claw::math::coordinate_2d<T>::operator/=(const value_type& v)
283 } // coordinate_2d::operator/=()
285 /*----------------------------------------------------------------------------*/
287 * \brief Unary minus.
288 * \param that The operand...
291 claw::math::coordinate_2d<T>
292 claw::math::operator-( const claw::math::coordinate_2d<T>& that )
294 return claw::math::coordinate_2d<T>(-that.x, -that.y);
295 } // operator-() [coordinate_2d]
297 /*----------------------------------------------------------------------------*/
299 * \brief Multiply coordinates.
300 * \param v The multiplicator.
301 * \param self The coordinates to multiply.
303 template<typename T, typename U>
304 claw::math::coordinate_2d<T>
305 claw::math::operator*( U v, const coordinate_2d<T>& self )
307 return self * typename coordinate_2d<T>::value_type(v);
308 } // operator*() [coordinate_2d]