Claw  1.7.3
rectangle.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 <algorithm>
31 
32 /*----------------------------------------------------------------------------*/
36 template<class T>
38 {
39 
40 } // rectangle::rectangle() [constructor]
41 
42 /*----------------------------------------------------------------------------*/
47 template<class T>
48 template<class U>
50  : position(that.position), width(that.width), height(that.height)
51 {
52 
53 } // rectangle::rectangle() [copy constructor]
54 
55 /*----------------------------------------------------------------------------*/
60 template<class T>
61 template<class U>
63  : position(that.left(), that.top()), width(that.width()),
64  height(that.height())
65 {
66 
67 } // rectangle::rectangle() [copy constructor]
68 
69 /*----------------------------------------------------------------------------*/
77 template<class T>
79 ( const value_type& _x, const value_type& _y,
80  const value_type& _width, const value_type& _height )
81  : position(_x, _y), width(_width), height(_height)
82 {
83 
84 } // rectangle::rectangle() [constructor with values]
85 
86 /*----------------------------------------------------------------------------*/
93 template<class T>
94 template<typename U>
96 ( const coordinate_2d<U>& pos, const value_type& _width,
97  const value_type& _height )
98  : position(pos), width(_width), height(_height)
99 {
100 
101 } // rectangle::rectangle() [constructor from position and size]
102 
103 /*----------------------------------------------------------------------------*/
109 template<class T>
110 template<typename U>
112 ( const coordinate_2d<U>& pos, const coordinate_2d<U>& size )
113  : position(pos), width(size.x), height(size.y)
114 {
115 
116 } // rectangle::rectangle() [constructor from position and size]
117 
118 /*----------------------------------------------------------------------------*/
138 template<class T>
139 template<typename U>
141 {
143  ( position.cast_value_type_to<U>(), (U)width, (U)height );
144 } // rectangle::cast_value_type_to()
145 
146 /*----------------------------------------------------------------------------*/
151 template<class T>
153 {
154  return (position == that.position) && (width == that.width)
155  && (height == that.height);
156 } // rectangle::operator==()
157 
158 /*----------------------------------------------------------------------------*/
163 template<class T>
165 {
166  return !(*this == that);
167 } // rectangle::operator!=()
168 
169 /*----------------------------------------------------------------------------*/
173 template<class T>
176 {
177  return width * height;
178 } // rectangle::area()
179 
180 /*----------------------------------------------------------------------------*/
185 template<class T>
186 bool
188 {
189  return (position.x <= p.x) && (right() >= p.x)
190  && (position.y <= p.y) && (bottom() >= p.y);
191 } // rectangle::includes()
192 
193 /*----------------------------------------------------------------------------*/
198 template<class T>
200 {
201  box_2d<value_type> his_box(r);
202 
203  return includes(his_box.first_point) && includes(his_box.second_point);
204 } // rectangle::includes() [rectangle]
205 
206 /*----------------------------------------------------------------------------*/
211 template<class T>
213 {
214  return (right() >= r.position.x)
215  && (r.right() >= position.x)
216  && (bottom() >= r.position.y)
217  && (r.bottom() >= position.y);
218 } // rectangle::intersects()
219 
220 /*----------------------------------------------------------------------------*/
225 template<class T>
228 {
229  self_type result;
230 
231  if ( intersects(r) )
232  {
233  x_intersection(r, result);
234  y_intersection(r, result);
235  }
236 
237  return result;
238 } // rectangle::intersection()
239 
240 /*----------------------------------------------------------------------------*/
246 template<class T>
249 {
250  const T result_left = std::min( left(), r.left() );
251  const T result_top = std::min( top(), r.top() );
252  const T result_bottom = std::max( bottom(), r.bottom() );
253  const T result_right = std::max( right(), r.right() );
254 
255  return self_type
256  ( result_left, result_top, result_right - result_left,
257  result_bottom - result_top );
258 } // rectangle::join()
259 
260 /*----------------------------------------------------------------------------*/
268 template<class T>
270 ( const value_type& new_x, const value_type& new_y,
271  const value_type& new_width, const value_type& new_height )
272 {
273  position.x = new_x;
274  position.y = new_y;
275  width = new_width;
276  height = new_height;
277 } // rectangle::set()
278 
279 /*----------------------------------------------------------------------------*/
283 template<class T>
286 {
287  return position.x;
288 } // rectangle::left()
289 
290 /*----------------------------------------------------------------------------*/
294 template<class T>
297 {
298  return position.x + width;
299 } // rectangle::right()
300 
301 /*----------------------------------------------------------------------------*/
305 template<class T>
308 {
309  return position.y + height;
310 } // rectangle::bottom()
311 
312 /*----------------------------------------------------------------------------*/
316 template<class T>
319 {
320  return position.y;
321 } // rectangle::top()
322 
323 /*----------------------------------------------------------------------------*/
327 template<class T>
330 {
331  return claw::math::coordinate_2d<value_type>(width, height);
332 } // rectangle::size()
333 
334 /*----------------------------------------------------------------------------*/
340 template<class T>
342 ( const self_type& r, self_type& result ) const
343 {
344  if (position.x <= r.position.x)
345  {
346  result.position.x = r.position.x;
347 
348  if (right() >= r.right())
349  result.width = r.width;
350  else
351  result.width = right() - r.position.x;
352  }
353  else
354  r.x_intersection(*this, result);
355 
356 } // rectangle::x_intersection()
357 
358 /*----------------------------------------------------------------------------*/
364 template<class T>
366 ( const self_type& r, self_type& result ) const
367 {
368  if (position.y <= r.position.y)
369  {
370  result.position.y = r.position.y;
371 
372  if (bottom() >= r.bottom())
373  result.height = r.height;
374  else
375  result.height = bottom() - r.position.y;
376  }
377  else
378  r.y_intersection(*this, result);
379 
380 } // rectangle::y_intersection()