00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #include <claw/box_2d.hpp>
00031
00032
00036 template<class T>
00037 claw::math::rectangle<T>::rectangle()
00038 {
00039
00040 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }