1 /* ///////////////////////////////////////////////////////////////////////
7 * Brief: The basic_rect class
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_GRAPHIC_BASIC_RECT_H
14 #define EXTL_GRAPHIC_BASIC_RECT_H
17 * \brief basic_rect class
20 /* ///////////////////////////////////////////////////////////////////////
24 #include "basic_point.h"
25 /* ///////////////////////////////////////////////////////////////////////
26 * ::extl::graphic namespace
28 EXTL_GRAPHIC_BEGIN_WHOLE_NAMESPACE
32 * \param Val the value type
36 * basic_rect<int> r(0, 0, 2, 2);
56 * \ingroup extl_group_graphic
59 template<typename_param_k Val
>
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
;
83 /// \name Constructors
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()
97 basic_rect(class_type
const& rhs
)
100 , m_width(rhs
.m_width
)
101 , m_height(rhs
.m_height
)
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()); }
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());}
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
); }
156 /* ///////////////////////////////////////////////////////////////////////
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
)
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
)
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
))
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()));
199 /* ///////////////////////////////////////////////////////////////////////
200 * ::extl::graphic namespace
202 EXTL_GRAPHIC_END_WHOLE_NAMESPACE
204 /* //////////////////////////////////////////////////////////////////// */
205 #endif /* EXTL_GRAPHIC_BASIC_RECT_H */
206 /* //////////////////////////////////////////////////////////////////// */