Claw  1.7.0
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 */
31 /*----------------------------------------------------------------------------*/
35 template<class T>
37 {
38 
39 } // rectangle::rectangle() [constructor]
40 
41 /*----------------------------------------------------------------------------*/
46 template<class T>
47 template<class U>
49  : position(that.position), width(that.width), height(that.height)
50 {
51 
52 } // rectangle::rectangle() [copy constructor]
53 
54 /*----------------------------------------------------------------------------*/
59 template<class T>
60 template<class U>
62  : position(that.left(), that.top()), width(that.width()),
63  height(that.height())
64 {
65 
66 } // rectangle::rectangle() [copy constructor]
67 
68 /*----------------------------------------------------------------------------*/
76 template<class T>
78 ( const value_type& _x, const value_type& _y,
79  const value_type& _width, const value_type& _height )
80  : position(_x, _y), width(_width), height(_height)
81 {
82 
83 } // rectangle::rectangle() [constructor with values]
84 
85 /*----------------------------------------------------------------------------*/
92 template<class T>
93 template<typename U>
95 ( const coordinate_2d<U>& pos, const value_type& _width,
96  const value_type& _height )
97  : position(pos), width(_width), height(_height)
98 {
99 
100 } // rectangle::rectangle() [constructor from position and size]
101 
102 /*----------------------------------------------------------------------------*/
108 template<class T>
109 template<typename U>
111 ( const coordinate_2d<U>& pos, const coordinate_2d<U>& size )
112  : position(pos), width(size.x), height(size.y)
113 {
114 
115 } // rectangle::rectangle() [constructor from position and size]
116 
117 /*----------------------------------------------------------------------------*/
137 template<class T>
138 template<typename U>
140 {
142  ( position.cast_value_type_to<U>(), (U)width, (U)height );
143 } // rectangle::cast_value_type_to()
144 
145 /*----------------------------------------------------------------------------*/
150 template<class T>
152 {
153  return (position == that.position) && (width == that.width)
154  && (height == that.height);
155 } // rectangle::operator==()
156 
157 /*----------------------------------------------------------------------------*/
162 template<class T>
164 {
165  return !(*this == that);
166 } // rectangle::operator!=()
167 
168 /*----------------------------------------------------------------------------*/
172 template<class T>
175 {
176  return width * height;
177 } // rectangle::area()
178 
179 /*----------------------------------------------------------------------------*/
184 template<class T>
185 bool
187 {
188  return (position.x <= p.x) && (right() >= p.x)
189  && (position.y <= p.y) && (bottom() >= p.y);
190 } // rectangle::includes()
191 
192 /*----------------------------------------------------------------------------*/
197 template<class T>
199 {
200  box_2d<value_type> his_box(r);
201 
202  return includes(his_box.first_point) && includes(his_box.second_point);
203 } // rectangle::includes() [rectangle]
204 
205 /*----------------------------------------------------------------------------*/
210 template<class T>
212 {
213  return (right() >= r.position.x)
214  && (r.right() >= position.x)
215  && (bottom() >= r.position.y)
216  && (r.bottom() >= position.y);
217 } // rectangle::intersects()
218 
219 /*----------------------------------------------------------------------------*/
224 template<class T>
227 {
228  self_type result;
229 
230  if ( intersects(r) )
231  {
232  x_intersection(r, result);
233  y_intersection(r, result);
234  }
235 
236  return result;
237 } // rectangle::intersection()
238 
239 /*----------------------------------------------------------------------------*/
247 template<class T>
249 ( const value_type& new_x, const value_type& new_y,
250  const value_type& new_width, const value_type& new_height )
251 {
252  position.x = new_x;
253  position.y = new_y;
254  width = new_width;
255  height = new_height;
256 } // rectangle::set()
257 
258 /*----------------------------------------------------------------------------*/
262 template<class T>
265 {
266  return position.x;
267 } // rectangle::left()
268 
269 /*----------------------------------------------------------------------------*/
273 template<class T>
276 {
277  return position.x + width;
278 } // rectangle::right()
279 
280 /*----------------------------------------------------------------------------*/
284 template<class T>
287 {
288  return position.y + height;
289 } // rectangle::bottom()
290 
291 /*----------------------------------------------------------------------------*/
295 template<class T>
298 {
299  return position.y;
300 } // rectangle::top()
301 
302 /*----------------------------------------------------------------------------*/
306 template<class T>
309 {
310  return claw::math::coordinate_2d<value_type>(width, height);
311 } // rectangle::size()
312 
313 /*----------------------------------------------------------------------------*/
319 template<class T>
321 ( const self_type& r, self_type& result ) const
322 {
323  if (position.x <= r.position.x)
324  {
325  result.position.x = r.position.x;
326 
327  if (right() >= r.right())
328  result.width = r.width;
329  else
330  result.width = right() - r.position.x;
331  }
332  else
333  r.x_intersection(*this, result);
334 
335 } // rectangle::x_intersection()
336 
337 /*----------------------------------------------------------------------------*/
343 template<class T>
345 ( const self_type& r, self_type& result ) const
346 {
347  if (position.y <= r.position.y)
348  {
349  result.position.y = r.position.y;
350 
351  if (bottom() >= r.bottom())
352  result.height = r.height;
353  else
354  result.height = bottom() - r.position.y;
355  }
356  else
357  r.y_intersection(*this, result);
358 
359 } // rectangle::y_intersection()