Claw  1.7.3
box_2d.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 <claw/rectangle.hpp>
31 #include <claw/assert.hpp>
32 
33 /*----------------------------------------------------------------------------*/
37 template<class T>
39 {
40 
41 } // box_2d::box_2d() [constructor]
42 
43 /*----------------------------------------------------------------------------*/
48 template<class T>
50  : first_point(that.first_point), second_point(that.second_point)
51 {
52 
53 } // box_2d::box_2d() [copy constructor]
54 
55 /*----------------------------------------------------------------------------*/
60 template<class T>
62  : first_point(that.position),
63  second_point(that.right(), that.bottom())
64 {
65 
66 } // box_2d::box_2d() [constructor from rectangle]
67 
68 /*----------------------------------------------------------------------------*/
74 template<class T>
76  : first_point(p1), second_point(p2)
77 {
78 
79 } // box_2d::box_2d() [constructor from coordinates]
80 
81 /*----------------------------------------------------------------------------*/
89 template<class T>
91  const value_type& x2, const value_type& y2 )
92  : first_point(x1, y1), second_point(x2, y2)
93 {
94 
95 } // box_2d::box_2d() [constructor with values]
96 
97 /*----------------------------------------------------------------------------*/
105 template<class T>
107 ( const value_type& x1, const value_type& y1, const value_type& x2,
108  const value_type& y2 )
109 {
110  first_point.set(x1, y1);
111  second_point.set(x2, y2);
112 } // box_2d::set()
113 
114 /*----------------------------------------------------------------------------*/
134 template<class T>
135 template<typename U>
137 {
138  return claw::math::box_2d<U>
139  ( first_point.cast_value_type_to<U>(),
140  second_point.cast_value_type_to<U>() );
141 } // box_2d::cast_value_type_to()
142 
143 /*----------------------------------------------------------------------------*/
147 template<class T>
149 {
150  return width() * height();
151 } // box_2d::area()
152 
153 /*----------------------------------------------------------------------------*/
158 template<class T>
159 bool
161 {
162  return (left() <= p.x) && (right() >= p.x)
163  && (bottom() <= p.y) && (top() >= p.y);
164 } // box_2d::includes()
165 
166 /*----------------------------------------------------------------------------*/
171 template<class T>
173 {
174  return includes(r.first_point) && includes(r.second_point);
175 } // box_2d::includes()
176 
177 /*----------------------------------------------------------------------------*/
182 template<class T>
184 {
185  return (right() >= r.left()) && (r.right() >= left())
186  && (top() >= r.bottom()) && (r.top() >= bottom());
187 } // box_2d::intersects()
188 
189 /*----------------------------------------------------------------------------*/
194 template<class T>
197 {
198  CLAW_PRECOND( intersects(r) );
199 
200  self_type result;
201 
202  if ( intersects(r) )
203  {
204  x_intersection(r, result);
205  y_intersection(r, result);
206  }
207 
208  return result;
209 } // box_2d::intersection()
210 
211 /*----------------------------------------------------------------------------*/
217 template<class T>
219 {
220  return self_type
221  ( std::min(r.left(), left()), std::min(r.bottom(), bottom()),
222  std::max(r.right(), right()), std::max(r.top(), top()) );
223 } // box_2d::join()
224 
225 /*----------------------------------------------------------------------------*/
229 template<class T>
231 {
232  return (width() == 0) || (height() == 0);
233 } // box_2d::empty()
234 
235 /*----------------------------------------------------------------------------*/
239 template<class T>
241 {
242  return (first_point.y > second_point.y) ? first_point.y : second_point.y;
243 } // box_2d::top()
244 
245 /*----------------------------------------------------------------------------*/
249 template<class T>
251 {
252  return (first_point.y < second_point.y) ? first_point.y : second_point.y;
253 } // box_2d::bottom()
254 
255 /*----------------------------------------------------------------------------*/
259 template<class T>
261 {
262  return (first_point.x < second_point.x) ? first_point.x : second_point.x;
263 } // box_2d::left()
264 
265 /*----------------------------------------------------------------------------*/
269 template<class T>
271 {
272  return (first_point.x > second_point.x) ? first_point.x : second_point.x;
273 } // box_2d::right()
274 
275 /*----------------------------------------------------------------------------*/
279 template<class T>
282 {
283  return point_type(left(), top());
284 } // box_2d::top_left()
285 
286 /*----------------------------------------------------------------------------*/
290 template<class T>
293 {
294  return point_type(right(), top());
295 } // box_2d::top_right()
296 
297 /*----------------------------------------------------------------------------*/
301 template<class T>
304 {
305  return point_type(left(), bottom());
306 } // box_2d::bottom_left()
307 
308 /*----------------------------------------------------------------------------*/
312 template<class T>
315 {
316  return point_type(right(), bottom());
317 } // box_2d::bottom_right()
318 
319 /*----------------------------------------------------------------------------*/
324 template<class T>
326 {
327  shift_y(p - top());
328 } // box_2d::top()
329 
330 /*----------------------------------------------------------------------------*/
335 template<class T>
337 {
338  shift_y(p - bottom());
339 } // box_2d::bottom()
340 
341 /*----------------------------------------------------------------------------*/
346 template<class T>
348 {
349  shift_x(p - left());
350 } // box_2d::left()
351 
352 /*----------------------------------------------------------------------------*/
357 template<class T>
359 {
360  shift_x(p - right());
361 } // box_2d::right()
362 
363 /*----------------------------------------------------------------------------*/
368 template<class T>
369 void
371 {
372  top(p.y);
373  left(p.x);
374 } // box_2d::top_left()
375 
376 /*----------------------------------------------------------------------------*/
381 template<class T>
382 void
384 {
385  top(p.y);
386  right(p.x);
387 } // box_2d::top_right()
388 
389 /*----------------------------------------------------------------------------*/
394 template<class T>
395 void
397 {
398  bottom(p.y);
399  left(p.x);
400 } // box_2d::bottom_left()
401 
402 /*----------------------------------------------------------------------------*/
407 template<class T>
410 {
411  bottom(p.y);
412  right(p.x);
413 } // box_2d::bottom_right()
414 
415 /*----------------------------------------------------------------------------*/
420 template<class T>
422 {
423  first_point.x += d;
424  second_point.x += d;
425 } // box_2d::shift_x()
426 
427 /*----------------------------------------------------------------------------*/
432 template<class T>
434 {
435  first_point.y += d;
436  second_point.y += d;
437 } // box_2d::shift_y()
438 
439 /*----------------------------------------------------------------------------*/
443 template<class T>
446 {
447  return claw::math::coordinate_2d<value_type>(width(), height());
448 } // box_2d::size()
449 
450 /*----------------------------------------------------------------------------*/
455 template<class T>
457 {
458  return (left() == that.left()) && (right() == that.right())
459  && (top() == that.top()) && (bottom() == that.bottom());
460 } // box_2d::operator==()
461 
462 /*----------------------------------------------------------------------------*/
467 template<class T>
469 {
470  return !( *this == that );
471 } // box_2d::operator!=()
472 
473 /*----------------------------------------------------------------------------*/
478 template<class T>
481 {
482  return self_type( first_point + vect, second_point + vect );
483 } // box_2d::operator+()
484 
485 /*----------------------------------------------------------------------------*/
490 template<class T>
493 {
494  return self_type( first_point - vect, second_point - vect );
495 } // box_2d::operator-()
496 
497 /*----------------------------------------------------------------------------*/
502 template<class T>
505 {
506  first_point += vect;
507  second_point += vect;
508 } // box_2d::operator+=()
509 
510 /*----------------------------------------------------------------------------*/
515 template<class T>
518 {
519  first_point -= vect;
520  second_point -= vect;
521 } // box_2d::operator-=()
522 
523 /*----------------------------------------------------------------------------*/
527 template<class T>
530 {
531  if (first_point.x > second_point.x)
532  return first_point.x - second_point.x;
533  else
534  return second_point.x - first_point.x;
535 } // box_2d::width()
536 
537 /*----------------------------------------------------------------------------*/
541 template<class T>
544 {
545  if (first_point.y > second_point.y)
546  return first_point.y - second_point.y;
547  else
548  return second_point.y - first_point.y;
549 } // box_2d::height()
550 
551 /*----------------------------------------------------------------------------*/
557 template<class T>
559 ( const self_type& r, self_type& result ) const
560 {
561  result.first_point.x = std::max(left(), r.left());
562  result.second_point.x = std::min(right(), r.right());
563 } // box_2d::x_intersection()
564 
565 /*----------------------------------------------------------------------------*/
571 template<class T>
573 ( const self_type& r, self_type& result ) const
574 {
575  result.first_point.y = std::max(bottom(), r.bottom());
576  result.second_point.y = std::min(top(), r.top());
577 } // box_2d::y_intersection()
bool operator!=(const self_type &vect) const
Difference operator.
Definition: box_2d.tpp:468
coordinate_2d< value_type > size() const
Get the size of the box_2d.
Definition: box_2d.tpp:445
self_type & operator+=(const point_type &vect)
Translation.
Definition: box_2d.tpp:504
point_type first_point
The first of the two points, representing one corner.
Definition: box_2d.hpp:120
point_type top_left() const
Get the coordinate of the top-left corner.
Definition: box_2d.tpp:281
value_type area() const
Calculate the box's area.
Definition: box_2d.tpp:148
bool includes(const coordinate_2d< value_type > &p) const
Tell if a point is in a box.
Definition: box_2d.tpp:160
bool intersects(const self_type &r) const
Tell if there is an intersection of two boxes.
Definition: box_2d.tpp:183
A class representing a rectangle by his x,y coordinates, width and height.
value_type right() const
Get the x-coordinate of the right edge.
Definition: box_2d.tpp:270
self_type operator+(const point_type &vect) const
Translation.
Definition: box_2d.tpp:480
value_type bottom() const
Get the y-coordinate of the bottom edge.
Definition: box_2d.tpp:250
void set(const value_type &x1, const value_type &y1, const value_type &x2, const value_type &y2)
Set the coordinates of the two points.
Definition: box_2d.tpp:107
bool empty() const
Tell if the box has a dimension equal to zero.
Definition: box_2d.tpp:230
A class representing a rectangle by his x,y coordinates, width and height.
Definition: box_2d.hpp:39
self_type join(const self_type &r) const
Join two box_2ds.
Definition: box_2d.tpp:218
value_type y
Y-coordinate.
self_type & operator-=(const point_type &vect)
Translation.
Definition: box_2d.tpp:517
self_type operator-(const point_type &vect) const
Translation.
Definition: box_2d.tpp:492
point_type top_right() const
Get the coordinate of the top-right corner.
Definition: box_2d.tpp:292
void shift_y(const value_type &d)
Shift the position of the box on the y-axis.
Definition: box_2d.tpp:433
T value_type
The type of the values we store.
Definition: box_2d.hpp:50
value_type left() const
Get the x-coordinate of the left edge.
Definition: box_2d.tpp:260
point_type second_point
The second of the two points, representing an other corner.
Definition: box_2d.hpp:123
point_type bottom_right() const
Get the coordinate of the bottom-right corner.
Definition: box_2d.tpp:314
value_type width() const
Return box' width.
Definition: box_2d.tpp:529
Some assert macros to strengthen you code.
point_type bottom_left() const
Get the coordinate of the bottom-left corner.
Definition: box_2d.tpp:303
A rectangle represented by two points in a 2D space.
Definition: box_2d.hpp:46
void shift_x(const value_type &d)
Shift the position of the box on the x-axis.
Definition: box_2d.tpp:421
self_type intersection(const self_type &r) const
Intersection of two box_2ds.
Definition: box_2d.tpp:196
box_2d()
Constructor.
Definition: box_2d.tpp:38
value_type top() const
Get the y-coordinate of the top edge.
Definition: box_2d.tpp:240
bool operator==(const self_type &vect) const
Equality operator.
Definition: box_2d.tpp:456
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
Definition: assert.hpp:98
value_type x
X-coordinate.
value_type height() const
Return box' height.
Definition: box_2d.tpp:543