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-2008 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@yahoo.fr
00024 */
00030 #include <claw/box_2d.hpp>
00031 
00032 /*----------------------------------------------------------------------------*/
00036 template<class T>
00037 claw::math::rectangle<T>::rectangle()
00038 {
00039 
00040 } // rectangle::rectangle() [constructor]
00041 
00042 /*----------------------------------------------------------------------------*/
00047 template<class T>
00048 template<class U>
00049 claw::math::rectangle<T>::rectangle( const rectangle<U>& that )
00050   : position(that.position), width(that.width), height(that.height)
00051 {
00052 
00053 } // rectangle::rectangle() [copy constructor]
00054 
00055 /*----------------------------------------------------------------------------*/
00063 template<class T>
00064 claw::math::rectangle<T>::rectangle
00065 ( const value_type& _x, const value_type& _y,
00066   const value_type& _width, const value_type& _height )
00067   : position(_x, _y), width(_width), height(_height)
00068 {
00069 
00070 } // rectangle::rectangle() [constructor with values]
00071 
00072 /*----------------------------------------------------------------------------*/
00079 template<class T>
00080 template<typename U>
00081 claw::math::rectangle<T>::rectangle
00082 ( const coordinate_2d<U>& pos, const value_type& _width,
00083   const value_type& _height )
00084   : position(pos), width(_width), height(_height)
00085 {
00086   
00087 } // rectangle::rectangle() [constructor from position and size]
00088 
00089 /*----------------------------------------------------------------------------*/
00095 template<class T>
00096 template<typename U>
00097 claw::math::rectangle<T>::rectangle
00098 ( const coordinate_2d<U>& pos, const coordinate_2d<U>& size )
00099   : position(pos), width(size.x), height(size.y)
00100 {
00101   
00102 } // rectangle::rectangle() [constructor from position and size]
00103 
00104 /*----------------------------------------------------------------------------*/
00124 template<class T>
00125 template<typename U>
00126 claw::math::rectangle<U> claw::math::rectangle<T>::cast_value_type_to() const
00127 {
00128   return claw::math::rectangle<U>
00129     ( position.cast_value_type_to<U>(), (U)width, (U)height );
00130 } // rectangle::cast_value_type_to()
00131 
00132 /*----------------------------------------------------------------------------*/
00136 template<class T>
00137 typename claw::math::rectangle<T>::value_type
00138 claw::math::rectangle<T>::area() const
00139 {
00140   return width * height;
00141 } // rectangle::area()
00142 
00143 /*----------------------------------------------------------------------------*/
00148 template<class T>
00149 bool
00150 claw::math::rectangle<T>::includes( const coordinate_2d<value_type>& p ) const
00151 {
00152   return (position.x <= p.x) && (right() >= p.x)
00153     && (position.y <= p.y) && (bottom() >= p.y);
00154 } // rectangle::includes()
00155 
00156 /*----------------------------------------------------------------------------*/
00161 template<class T>
00162 bool claw::math::rectangle<T>::includes( const self_type& r ) const
00163 {
00164   box_2d<value_type> his_box(r);
00165 
00166   return includes(his_box.first_point) && includes(his_box.second_point);
00167 } // rectangle::includes() [rectangle]
00168 
00169 /*----------------------------------------------------------------------------*/
00174 template<class T>
00175 bool claw::math::rectangle<T>::intersects( const self_type& r ) const
00176 {
00177   return (right() >= r.position.x)
00178     && (r.right() >= position.x) 
00179     && (bottom() >= r.position.y)
00180     && (r.bottom() >= position.y);
00181 } // rectangle::intersects()
00182 
00183 /*----------------------------------------------------------------------------*/
00188 template<class T>
00189 claw::math::rectangle<T>
00190 claw::math::rectangle<T>::intersection( const self_type& r ) const
00191 {
00192   self_type result;
00193 
00194   if ( intersects(r) )
00195     {
00196       x_intersection(r, result);
00197       y_intersection(r, result);
00198     }
00199 
00200   return result;
00201 } // rectangle::intersection()
00202 
00203 /*----------------------------------------------------------------------------*/
00211 template<class T>
00212 void claw::math::rectangle<T>::set
00213 ( const value_type& new_x, const value_type& new_y,
00214   const value_type& new_width, const value_type& new_height )
00215 {
00216   position.x = new_x;
00217   position.y = new_y;
00218   width = new_width;
00219   height = new_height;
00220 } // rectangle::set()
00221 
00222 /*----------------------------------------------------------------------------*/
00226 template<class T>
00227 typename claw::math::rectangle<T>::value_type
00228 claw::math::rectangle<T>::right() const
00229 {
00230   return position.x + width;
00231 } // rectangle::right()
00232 
00233 /*----------------------------------------------------------------------------*/
00237 template<class T>
00238 typename claw::math::rectangle<T>::value_type
00239 claw::math::rectangle<T>::bottom() const
00240 {
00241   return position.y + height;
00242 } // rectangle::bottom()
00243 
00244 /*----------------------------------------------------------------------------*/
00248 template<class T>
00249 claw::math::coordinate_2d< typename claw::math::rectangle<T>::value_type >
00250 claw::math::rectangle<T>::size() const
00251 {
00252   return claw::math::coordinate_2d<value_type>(width, height);
00253 } // rectangle::size()
00254 
00255 /*----------------------------------------------------------------------------*/
00261 template<class T>
00262 void claw::math::rectangle<T>::x_intersection( const self_type& r,
00263                                                self_type& result ) const
00264 {
00265   if (position.x <= r.position.x)
00266     {
00267       result.position.x = r.position.x;
00268 
00269       if (right() >= r.right())
00270         result.width = r.width;
00271       else
00272         result.width = right() - r.position.x;
00273     }
00274   else
00275     r.x_intersection(*this, result);
00276 
00277 } // rectangle::x_intersection()
00278 
00279 /*----------------------------------------------------------------------------*/
00285 template<class T>
00286 void claw::math::rectangle<T>::y_intersection( const self_type& r,
00287                                                self_type& result ) const
00288 {
00289   if (position.y <= r.position.y)
00290     {
00291       result.position.y = r.position.y;
00292 
00293       if (bottom() >= r.bottom())
00294         result.height = r.height;
00295       else
00296         result.height = bottom() - r.position.y;
00297     }
00298   else
00299     r.y_intersection(*this, result);
00300 
00301 } // rectangle::y_intersection()

Generated on Thu Jun 26 09:35:04 2008 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.5.6