Claw  1.7.3
coordinate_2d.tpp
Go to the documentation of this file.
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 */
30 #include <cmath>
31 
32 /*----------------------------------------------------------------------------*/
36 template<typename T>
38 {
39 
40 } // coordinate_2d::coordinate_2d() [constructor]
41 
42 /*----------------------------------------------------------------------------*/
46 template<typename T>
47 template<typename U>
49  : x(that.x), y(that.y)
50 {
51 
52 } // coordinate_2d::coordinate_2d() [copy constructor]
53 
54 /*----------------------------------------------------------------------------*/
60 template<typename T>
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 /*----------------------------------------------------------------------------*/
89 template<class T>
90 template<typename U>
93 {
94  return claw::math::coordinate_2d<U>( (U)x, (U)y );
95 } // coordinate_2d::cast_value_type_to()
96 
97 /*----------------------------------------------------------------------------*/
103 template<typename T>
104 void
106 {
107  x = _x;
108  y = _y;
109 } // coordinate_2d::set()
110 
111 /*----------------------------------------------------------------------------*/
116 template<typename T>
119 {
120  return (value_type)sqrt( (p.x - x)*(p.x - x) + (p.y - y)*(p.y - y) );
121 } // coordinate_2d::distance()
122 
123 /*----------------------------------------------------------------------------*/
129 template<typename T>
130 void
131 claw::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 /*----------------------------------------------------------------------------*/
149 template<typename T>
151 {
152  return std::atan2( to.y - y, to.x - x );
153 } // coordinate_2d::slope_angle()
154 
155 /*----------------------------------------------------------------------------*/
160 template<typename T>
162 {
163  return (x == that.x) && (y == that.y);
164 } // coordinate_2d::operator==()
165 
166 /*----------------------------------------------------------------------------*/
171 template<typename T>
173 {
174  return !(*this == that);
175 } // coordinate_2d::operator!=()
176 
177 /*----------------------------------------------------------------------------*/
182 template<typename T>
185 {
186  return self_type( x + that.x, y + that.y );
187 } // coordinate_2d::operator+()
188 
189 /*----------------------------------------------------------------------------*/
194 template<typename T>
197 {
198  return self_type( x - that.x, y - that.y );
199 } // coordinate_2d::operator-()
200 
201 /*----------------------------------------------------------------------------*/
206 template<typename T>
209 {
210  x += that.x;
211  y += that.y;
212 
213  return *this;
214 } // coordinate_2d::operator+=()
215 
216 /*----------------------------------------------------------------------------*/
221 template<typename T>
224 {
225  x -= that.x;
226  y -= that.y;
227 
228  return *this;
229 } // coordinate_2d::operator-=()
230 
231 /*----------------------------------------------------------------------------*/
236 template<typename T>
239 {
240  return self_type( x * v, y * v );
241 } // coordinate_2d::operator*()
242 
243 /*----------------------------------------------------------------------------*/
248 template<typename T>
251 {
252  return self_type( x / v, y / v );
253 } // coordinate_2d::operator/()
254 
255 /*----------------------------------------------------------------------------*/
260 template<typename T>
263 {
264  x *= v;
265  y *= v;
266 
267  return *this;
268 } // coordinate_2d::operator*=()
269 
270 /*----------------------------------------------------------------------------*/
275 template<typename T>
278 {
279  x /= v;
280  y /= v;
281 
282  return *this;
283 } // coordinate_2d::operator/=()
284 
285 /*----------------------------------------------------------------------------*/
290 template<typename T>
293 {
294  return claw::math::coordinate_2d<T>(-that.x, -that.y);
295 } // operator-() [coordinate_2d]
296 
297 /*----------------------------------------------------------------------------*/
303 template<typename T, typename U>
306 {
307  return self * typename coordinate_2d<T>::value_type(v);
308 } // operator*() [coordinate_2d]