remove \r
[extl.git] / extl / graphic / basic_rect.h
blobac95892a00ce4e3dbf9d680707212221c992bbda
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: basic_rect.h
4 * Created: 08.12.31
5 * Updated: 08.12.31
7 * Brief: The basic_rect class
9 * [<Home>]
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_GRAPHIC_BASIC_RECT_H
14 #define EXTL_GRAPHIC_BASIC_RECT_H
16 /*!\file basic_rect.h
17 * \brief basic_rect class
20 /* ///////////////////////////////////////////////////////////////////////
21 * Includes
23 #include "prefix.h"
24 #include "basic_point.h"
25 /* ///////////////////////////////////////////////////////////////////////
26 * ::extl::graphic namespace
28 EXTL_GRAPHIC_BEGIN_WHOLE_NAMESPACE
30 /*!brief basic_rect
32 * \param Val the value type
34 * \code
36 * basic_rect<int> r(0, 0, 2, 2);
38 * or
40 * basic_rect<int> r;
41 * r.top(0);
42 * r.left(0);
43 * r.bottom(1);
44 * r.right(1);
46 * or
48 * basic_rect<int> r;
49 * r.top(0);
50 * r.left(0);
51 * r.height(2);
52 * r.width(2);
54 * \endcode
56 * \ingroup extl_group_graphic
59 template<typename_param_k Val>
60 class basic_rect
62 /// \name Types
63 /// @{
64 public:
65 typedef basic_rect class_type;
66 typedef Val value_type;
67 typedef value_type& reference;
68 typedef value_type const& const_reference;
69 typedef value_type* pointer;
70 typedef value_type const* const_pointer;
71 typedef e_bool_t bool_type;
72 /// @}
74 /// \name Members
75 /// @{
76 private:
77 value_type m_left;
78 value_type m_top;
79 value_type m_width;
80 value_type m_height;
81 /// @}
83 /// \name Constructors
84 /// @{
85 public:
86 explicit_k basic_rect ( const_reference left = value_type()
87 , const_reference top = value_type()
88 , const_reference w = value_type()
89 , const_reference h = value_type()
91 : m_left(left)
92 , m_top(top)
93 , m_width(w)
94 , m_height(h)
97 basic_rect(class_type const& rhs)
98 : m_left(rhs.m_left)
99 , m_top(rhs.m_top)
100 , m_width(rhs.m_width)
101 , m_height(rhs.m_height)
104 /// @}
106 /// \name Attributes
107 /// @{
108 public:
109 value_type const top() const { return m_top; }
110 void top(const_reference val) { m_top = val; }
112 value_type const bottom() const { return (top() + height() - 1); }
113 void bottom(const_reference val) { height(val + 1 - top()); }
115 value_type const left() const { return m_left; }
116 void left(const_reference val) { m_left = val; }
118 value_type const right() const { return (left() + width() - 1); }
119 void right(const_reference val) { width(val + 1 - left()); }
121 value_type const height() const { return m_height; }
122 void height(const_reference val) { m_height = val; }
124 value_type const width() const { return m_width; }
125 void width(const_reference val) { m_width = val; }
127 bool_type contains(const_reference x, const_reference y, const_reference w, const_reference h) const;
128 bool_type contains(class_type const& rhs) const { return contains(rhs.left(), rhs.top(), rhs.width(), rhs.height()); }
129 bool_type contains(const_reference x, const_reference y) const { return contains(x, y, 1, 1); }
131 bool_type intersects(const_reference x, const_reference y, const_reference w, const_reference h) const;
132 bool_type intersects(class_type const& rhs) const { return contains(rhs.left(), rhs.top(), rhs.width(), rhs.height()); }
133 bool_type intersects(const_reference x, const_reference y) const { return contains(x, y, 1, 1); }
135 class_type make_intersection(const_reference x, const_reference y, const_reference w, const_reference h) const;
136 class_type make_intersection(class_type const& rhs) const { return make_intersection(rhs.left(), rhs.top(), rhs.width(), rhs.height()); }
138 bool_type is_empty() const { return (0 == width()) && (0 == height()); }
139 /// @}
141 /// \name Mutators
142 /// @{
143 public:
144 void swap(class_type& rhs) { std_swap(m_top, rhs.m_top); std_swap(m_left, rhs.m_left); std_swap(m_width, rhs.m_width); std_swap(m_height, rhs.m_height); }
145 void clear() { top(value_type()); left(value_type()); width(value_type()); height(value_type());}
146 /// @}
148 /// \name Operators
149 /// @{
150 public:
151 bool_type operator ==(class_type const& rhs) const { return (top() == rhs.top()) && (height() == rhs.height()) && (left() == rhs.left()) && (width() == rhs.width()); }
152 bool_type operator !=(class_type const& rhs) const { return !((*this) == rhs); }
153 /// @}
156 /* ///////////////////////////////////////////////////////////////////////
157 * Implemention
160 template<typename_param_k Val>
161 typename_type_ret_k basic_rect<Val>::
162 bool_type basic_rect<Val>::contains(const_reference x, const_reference y, const_reference w, const_reference h) const
164 return ( !(left() > x)
165 && !(top() > y)
166 && !(right() < (x + w - 1))
167 && !(bottom() < (y + h - 1))
171 template<typename_param_k Val>
172 typename_type_ret_k basic_rect<Val>::
173 bool_type basic_rect<Val>::intersects(const_reference x, const_reference y, const_reference w, const_reference h) const
175 return !( (right() < x)
176 && (bottom() < y)
177 && (left() > (x + w - 1))
178 && (top() > (y + h - 1))
183 template<typename_param_k Val>
184 typename_type_ret_k basic_rect<Val>::
185 class_type basic_rect<Val>::make_intersection(const_reference x, const_reference y, const_reference w, const_reference h) const
187 if (intersects(x, y, w, h))
189 class_type rect;
190 rect.left(xtl_max(x, left()));
191 rect.top(xtl_max(y, top()));
192 rect.right(xtl_min(x + w - 1, right()));
193 rect.bottom(xtl_min(y + h - 1, bottom()));
194 return rect;
197 return class_type();
199 /* ///////////////////////////////////////////////////////////////////////
200 * ::extl::graphic namespace
202 EXTL_GRAPHIC_END_WHOLE_NAMESPACE
204 /* //////////////////////////////////////////////////////////////////// */
205 #endif /* EXTL_GRAPHIC_BASIC_RECT_H */
206 /* //////////////////////////////////////////////////////////////////// */