Claw  1.7.0
rectangle.tpp
Go to the documentation of this file.
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 */
00031 /*----------------------------------------------------------------------------*/
00035 template<class T>
00036 claw::math::rectangle<T>::rectangle()
00037 {
00038 
00039 } // rectangle::rectangle() [constructor]
00040 
00041 /*----------------------------------------------------------------------------*/
00046 template<class T>
00047 template<class U>
00048 claw::math::rectangle<T>::rectangle( const rectangle<U>& that )
00049   : position(that.position), width(that.width), height(that.height)
00050 {
00051 
00052 } // rectangle::rectangle() [copy constructor]
00053 
00054 /*----------------------------------------------------------------------------*/
00059 template<class T>
00060 template<class U>
00061 claw::math::rectangle<T>::rectangle( const box_2d<U>& that )
00062   : position(that.left(), that.top()), width(that.width()),
00063     height(that.height())
00064 {
00065 
00066 } // rectangle::rectangle() [copy constructor]
00067 
00068 /*----------------------------------------------------------------------------*/
00076 template<class T>
00077 claw::math::rectangle<T>::rectangle
00078 ( const value_type& _x, const value_type& _y,
00079   const value_type& _width, const value_type& _height )
00080   : position(_x, _y), width(_width), height(_height)
00081 {
00082 
00083 } // rectangle::rectangle() [constructor with values]
00084 
00085 /*----------------------------------------------------------------------------*/
00092 template<class T>
00093 template<typename U>
00094 claw::math::rectangle<T>::rectangle
00095 ( const coordinate_2d<U>& pos, const value_type& _width,
00096   const value_type& _height )
00097   : position(pos), width(_width), height(_height)
00098 {
00099   
00100 } // rectangle::rectangle() [constructor from position and size]
00101 
00102 /*----------------------------------------------------------------------------*/
00108 template<class T>
00109 template<typename U>
00110 claw::math::rectangle<T>::rectangle
00111 ( const coordinate_2d<U>& pos, const coordinate_2d<U>& size )
00112   : position(pos), width(size.x), height(size.y)
00113 {
00114   
00115 } // rectangle::rectangle() [constructor from position and size]
00116 
00117 /*----------------------------------------------------------------------------*/
00137 template<class T>
00138 template<typename U>
00139 claw::math::rectangle<U> claw::math::rectangle<T>::cast_value_type_to() const
00140 {
00141   return claw::math::rectangle<U>
00142     ( position.cast_value_type_to<U>(), (U)width, (U)height );
00143 } // rectangle::cast_value_type_to()
00144 
00145 /*----------------------------------------------------------------------------*/
00150 template<class T>
00151 bool claw::math::rectangle<T>::operator==( const self_type& that ) const
00152 {
00153   return (position == that.position) && (width == that.width)
00154     && (height == that.height);
00155 } // rectangle::operator==()
00156 
00157 /*----------------------------------------------------------------------------*/
00162 template<class T>
00163 bool claw::math::rectangle<T>::operator!=( const self_type& that ) const
00164 {
00165   return !(*this == that);
00166 } // rectangle::operator!=()
00167 
00168 /*----------------------------------------------------------------------------*/
00172 template<class T>
00173 typename claw::math::rectangle<T>::value_type
00174 claw::math::rectangle<T>::area() const
00175 {
00176   return width * height;
00177 } // rectangle::area()
00178 
00179 /*----------------------------------------------------------------------------*/
00184 template<class T>
00185 bool
00186 claw::math::rectangle<T>::includes( const coordinate_2d<value_type>& p ) const
00187 {
00188   return (position.x <= p.x) && (right() >= p.x)
00189     && (position.y <= p.y) && (bottom() >= p.y);
00190 } // rectangle::includes()
00191 
00192 /*----------------------------------------------------------------------------*/
00197 template<class T>
00198 bool claw::math::rectangle<T>::includes( const self_type& r ) const
00199 {
00200   box_2d<value_type> his_box(r);
00201 
00202   return includes(his_box.first_point) && includes(his_box.second_point);
00203 } // rectangle::includes() [rectangle]
00204 
00205 /*----------------------------------------------------------------------------*/
00210 template<class T>
00211 bool claw::math::rectangle<T>::intersects( const self_type& r ) const
00212 {
00213   return (right() >= r.position.x)
00214     && (r.right() >= position.x) 
00215     && (bottom() >= r.position.y)
00216     && (r.bottom() >= position.y);
00217 } // rectangle::intersects()
00218 
00219 /*----------------------------------------------------------------------------*/
00224 template<class T>
00225 claw::math::rectangle<T>
00226 claw::math::rectangle<T>::intersection( const self_type& r ) const
00227 {
00228   self_type result;
00229 
00230   if ( intersects(r) )
00231     {
00232       x_intersection(r, result);
00233       y_intersection(r, result);
00234     }
00235 
00236   return result;
00237 } // rectangle::intersection()
00238 
00239 /*----------------------------------------------------------------------------*/
00247 template<class T>
00248 void claw::math::rectangle<T>::set
00249 ( const value_type& new_x, const value_type& new_y,
00250   const value_type& new_width, const value_type& new_height )
00251 {
00252   position.x = new_x;
00253   position.y = new_y;
00254   width = new_width;
00255   height = new_height;
00256 } // rectangle::set()
00257 
00258 /*----------------------------------------------------------------------------*/
00262 template<class T>
00263 typename claw::math::rectangle<T>::value_type
00264 claw::math::rectangle<T>::left() const
00265 {
00266   return position.x;
00267 } // rectangle::left()
00268 
00269 /*----------------------------------------------------------------------------*/
00273 template<class T>
00274 typename claw::math::rectangle<T>::value_type
00275 claw::math::rectangle<T>::right() const
00276 {
00277   return position.x + width;
00278 } // rectangle::right()
00279 
00280 /*----------------------------------------------------------------------------*/
00284 template<class T>
00285 typename claw::math::rectangle<T>::value_type
00286 claw::math::rectangle<T>::bottom() const
00287 {
00288   return position.y + height;
00289 } // rectangle::bottom()
00290 
00291 /*----------------------------------------------------------------------------*/
00295 template<class T>
00296 typename claw::math::rectangle<T>::value_type
00297 claw::math::rectangle<T>::top() const
00298 {
00299   return position.y;
00300 } // rectangle::top()
00301 
00302 /*----------------------------------------------------------------------------*/
00306 template<class T>
00307 claw::math::coordinate_2d< typename claw::math::rectangle<T>::value_type >
00308 claw::math::rectangle<T>::size() const
00309 {
00310   return claw::math::coordinate_2d<value_type>(width, height);
00311 } // rectangle::size()
00312 
00313 /*----------------------------------------------------------------------------*/
00319 template<class T>
00320 void claw::math::rectangle<T>::x_intersection
00321 ( const self_type& r, self_type& result ) const
00322 {
00323   if (position.x <= r.position.x)
00324     {
00325       result.position.x = r.position.x;
00326 
00327       if (right() >= r.right())
00328         result.width = r.width;
00329       else
00330         result.width = right() - r.position.x;
00331     }
00332   else
00333     r.x_intersection(*this, result);
00334 
00335 } // rectangle::x_intersection()
00336 
00337 /*----------------------------------------------------------------------------*/
00343 template<class T>
00344 void claw::math::rectangle<T>::y_intersection
00345 ( const self_type& r, self_type& result ) const
00346 {
00347   if (position.y <= r.position.y)
00348     {
00349       result.position.y = r.position.y;
00350 
00351       if (bottom() >= r.bottom())
00352         result.height = r.height;
00353       else
00354         result.height = bottom() - r.position.y;
00355     }
00356   else
00357     r.y_intersection(*this, result);
00358 
00359 } // rectangle::y_intersection()