Claw
1.7.0
|
00001 /* 00002 CLAW - a C++ Library Absolutely Wonderful 00003 00004 CLAW is a free library without any particular aim but being useful to 00005 anyone. 00006 00007 Copyright (C) 2005-2011 Julien Jorge 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00022 00023 contact: julien.jorge@gamned.org 00024 */ 00030 #include <cmath> 00031 00032 /*----------------------------------------------------------------------------*/ 00036 template<typename T> 00037 claw::math::coordinate_2d<T>::coordinate_2d() 00038 { 00039 00040 } // coordinate_2d::coordinate_2d() [constructor] 00041 00042 /*----------------------------------------------------------------------------*/ 00046 template<typename T> 00047 template<typename U> 00048 claw::math::coordinate_2d<T>::coordinate_2d(const coordinate_2d<U>& that) 00049 : x(that.x), y(that.y) 00050 { 00051 00052 } // coordinate_2d::coordinate_2d() [copy constructor] 00053 00054 /*----------------------------------------------------------------------------*/ 00060 template<typename T> 00061 claw::math::coordinate_2d<T>::coordinate_2d 00062 (const value_type& _x, const value_type& _y) 00063 : x(_x), y(_y) 00064 { 00065 00066 } // coordinate_2d::coordinate_2d() [constructor whit values] 00067 00068 /*----------------------------------------------------------------------------*/ 00089 template<class T> 00090 template<typename U> 00091 claw::math::coordinate_2d<U> 00092 claw::math::coordinate_2d<T>::cast_value_type_to() const 00093 { 00094 return claw::math::coordinate_2d<U>( (U)x, (U)y ); 00095 } // coordinate_2d::cast_value_type_to() 00096 00097 /*----------------------------------------------------------------------------*/ 00103 template<typename T> 00104 void 00105 claw::math::coordinate_2d<T>::set(const value_type& _x, const value_type& _y) 00106 { 00107 x = _x; 00108 y = _y; 00109 } // coordinate_2d::set() 00110 00111 /*----------------------------------------------------------------------------*/ 00116 template<typename T> 00117 typename claw::math::coordinate_2d<T>::value_type 00118 claw::math::coordinate_2d<T>::distance(const self_type& p) const 00119 { 00120 return (value_type)sqrt( (p.x - x)*(p.x - x) + (p.y - y)*(p.y - y) ); 00121 } // coordinate_2d::distance() 00122 00123 /*----------------------------------------------------------------------------*/ 00129 template<typename T> 00130 void 00131 claw::math::coordinate_2d<T>::rotate( const self_type& center, double angle ) 00132 { 00133 self_type result(center); 00134 00135 result.x += 00136 (x - center.x) * std::cos(angle) - (y - center.y) * std::sin(angle); 00137 result.y += 00138 (x - center.x) * std::sin(angle) + (y - center.y) * std::cos(angle); 00139 00140 *this = result; 00141 } // coordinate_2d::rotate() 00142 00143 /*----------------------------------------------------------------------------*/ 00149 template<typename T> 00150 double claw::math::coordinate_2d<T>::slope_angle( const self_type& to ) const 00151 { 00152 return std::atan2( to.y - y, to.x - x ); 00153 } // coordinate_2d::slope_angle() 00154 00155 /*----------------------------------------------------------------------------*/ 00160 template<typename T> 00161 bool claw::math::coordinate_2d<T>::operator==(const self_type& that) const 00162 { 00163 return (x == that.x) && (y == that.y); 00164 } // coordinate_2d::operator==() 00165 00166 /*----------------------------------------------------------------------------*/ 00171 template<typename T> 00172 bool claw::math::coordinate_2d<T>::operator!=(const self_type& that) const 00173 { 00174 return !(*this == that); 00175 } // coordinate_2d::operator!=() 00176 00177 /*----------------------------------------------------------------------------*/ 00182 template<typename T> 00183 claw::math::coordinate_2d<T> 00184 claw::math::coordinate_2d<T>::operator+(const self_type& that) const 00185 { 00186 return self_type( x + that.x, y + that.y ); 00187 } // coordinate_2d::operator+() 00188 00189 /*----------------------------------------------------------------------------*/ 00194 template<typename T> 00195 claw::math::coordinate_2d<T> 00196 claw::math::coordinate_2d<T>::operator-(const self_type& that) const 00197 { 00198 return self_type( x - that.x, y - that.y ); 00199 } // coordinate_2d::operator-() 00200 00201 /*----------------------------------------------------------------------------*/ 00206 template<typename T> 00207 claw::math::coordinate_2d<T>& 00208 claw::math::coordinate_2d<T>::operator+=(const self_type& that) 00209 { 00210 x += that.x; 00211 y += that.y; 00212 00213 return *this; 00214 } // coordinate_2d::operator+=() 00215 00216 /*----------------------------------------------------------------------------*/ 00221 template<typename T> 00222 claw::math::coordinate_2d<T>& 00223 claw::math::coordinate_2d<T>::operator-=(const self_type& that) 00224 { 00225 x -= that.x; 00226 y -= that.y; 00227 00228 return *this; 00229 } // coordinate_2d::operator-=() 00230 00231 /*----------------------------------------------------------------------------*/ 00236 template<typename T> 00237 claw::math::coordinate_2d<T> 00238 claw::math::coordinate_2d<T>::operator*(const value_type& v) const 00239 { 00240 return self_type( x * v, y * v ); 00241 } // coordinate_2d::operator*() 00242 00243 /*----------------------------------------------------------------------------*/ 00248 template<typename T> 00249 claw::math::coordinate_2d<T> 00250 claw::math::coordinate_2d<T>::operator/(const value_type& v) const 00251 { 00252 return self_type( x / v, y / v ); 00253 } // coordinate_2d::operator/() 00254 00255 /*----------------------------------------------------------------------------*/ 00260 template<typename T> 00261 claw::math::coordinate_2d<T>& 00262 claw::math::coordinate_2d<T>::operator*=(const value_type& v) 00263 { 00264 x *= v; 00265 y *= v; 00266 00267 return *this; 00268 } // coordinate_2d::operator*=() 00269 00270 /*----------------------------------------------------------------------------*/ 00275 template<typename T> 00276 claw::math::coordinate_2d<T>& 00277 claw::math::coordinate_2d<T>::operator/=(const value_type& v) 00278 { 00279 x /= v; 00280 y /= v; 00281 00282 return *this; 00283 } // coordinate_2d::operator/=() 00284 00285 /*----------------------------------------------------------------------------*/ 00290 template<typename T> 00291 claw::math::coordinate_2d<T> 00292 claw::math::operator-( const claw::math::coordinate_2d<T>& that ) 00293 { 00294 return claw::math::coordinate_2d<T>(-that.x, -that.y); 00295 } // operator-() [coordinate_2d] 00296 00297 /*----------------------------------------------------------------------------*/ 00303 template<typename T, typename U> 00304 claw::math::coordinate_2d<T> 00305 claw::math::operator*( U v, const coordinate_2d<T>& self ) 00306 { 00307 return self * v; 00308 } // operator*() [coordinate_2d]