Claw  1.7.3
box_2d.hpp
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 #ifndef __CLAW_BOX_2D_HPP__
31 #define __CLAW_BOX_2D_HPP__
32 
33 #include <claw/coordinate_2d.hpp>
34 
35 namespace claw
36 {
37  namespace math
38  {
39  template<class T> class rectangle;
40 
45  template <class T>
46  class box_2d
47  {
48  public:
50  typedef T value_type;
51 
55 
58 
59  public:
60  box_2d();
61  box_2d( const self_type& that );
62  box_2d( const rectangle<value_type>& that );
63  box_2d( const point_type& p1, const point_type& p2 );
64  box_2d( const value_type& x1, const value_type& y1,
65  const value_type& x2, const value_type& y2 );
66 
67  void set( const value_type& x1, const value_type& y1,
68  const value_type& x2, const value_type& y2 );
69 
70  template<typename U>
71  box_2d<U> cast_value_type_to() const;
72 
73  value_type area() const;
74  bool includes( const coordinate_2d<value_type>& p ) const;
75  bool includes( const self_type& r ) const;
76  bool intersects( const self_type& r ) const;
77  self_type intersection( const self_type& r ) const;
78  self_type join( const self_type& r ) const;
79  bool empty() const;
80 
81  value_type top() const;
82  value_type bottom() const;
83  value_type left() const;
84  value_type right() const;
85  point_type top_left() const;
86  point_type top_right() const;
87  point_type bottom_left() const;
88  point_type bottom_right() const;
89 
90  void top( const value_type& p );
91  void bottom( const value_type& p );
92  void left( const value_type& p );
93  void right( const value_type& p );
94  void top_left( const coordinate_2d<value_type>& p );
95  void top_right( const coordinate_2d<value_type>& p );
96  void bottom_left( const coordinate_2d<value_type>& p );
97  void bottom_right( const coordinate_2d<value_type>& p );
98 
99  void shift_x( const value_type& d );
100  void shift_y( const value_type& d );
101 
102  value_type width() const;
103  value_type height() const;
104 
106 
107  bool operator==(const self_type& vect) const;
108  bool operator!=(const self_type& vect) const;
109  self_type operator+(const point_type& vect) const;
110  self_type operator-(const point_type& vect) const;
111  self_type& operator+=(const point_type& vect);
112  self_type& operator-=(const point_type& vect);
113 
114  private:
115  void x_intersection( const self_type& r, self_type& result ) const;
116  void y_intersection( const self_type& r, self_type& result ) const;
117 
118  public:
121 
124 
125  }; // class box_2d
126  } // namespace math
127 } // namespace claw
128 
129 #include <claw/impl/box_2d.tpp>
130 
131 #endif // __CLAW_BOX_2D_HPP__
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
Coordinates in a two dimensional space.
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
Implementation of claw::math::box_2d class.
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
box_2d< value_type > self_type
The type of the current class.
Definition: box_2d.hpp:57
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
coordinate_2d< value_type > point_type
The type of the coordinates of the points representing the corners.
Definition: box_2d.hpp:54
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
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
value_type height() const
Return box' height.
Definition: box_2d.tpp:543