38 #if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT) 39 #error "Only <final/final.h> can be included directly." 45 #include "final/util/fpoint.h" 46 #include "final/util/fsize.h" 47 #include "final/util/fstring.h" 60 using coordinate_type = int;
61 using coordinate_reference = coordinate_type&;
62 using size_type = std::size_t;
65 constexpr
FRect () noexcept =
default;
67 constexpr
FRect (coordinate_type, coordinate_type, size_type, size_type) noexcept;
72 constexpr
FRect(
const FRect&) noexcept =
default;
78 ~
FRect() noexcept =
default;
81 constexpr
auto operator = (
const FRect&) noexcept ->
FRect& =
default;
84 constexpr
auto operator = (
FRect&&) noexcept ->
FRect& =
default;
87 auto getClassName()
const ->
FString;
88 constexpr
auto getX1()
const noexcept -> coordinate_type;
89 constexpr
auto getY1()
const noexcept -> coordinate_type;
90 constexpr
auto getX2()
const noexcept -> coordinate_type;
91 constexpr
auto getY2()
const noexcept -> coordinate_type;
92 constexpr
auto getX()
const noexcept -> coordinate_type;
93 constexpr
auto getY()
const noexcept -> coordinate_type;
94 constexpr
auto getPos()
const noexcept ->
FPoint;
95 constexpr
auto getUpperLeftPos()
const noexcept ->
FPoint;
96 constexpr
auto getUpperRightPos()
const noexcept ->
FPoint;
97 constexpr
auto getLowerLeftPos()
const noexcept ->
FPoint;
98 constexpr
auto getLowerRightPos()
const noexcept ->
FPoint;
99 constexpr
auto getWidth()
const noexcept -> size_type;
100 constexpr
auto getHeight()
const noexcept -> size_type;
101 constexpr
auto getSize()
const noexcept ->
FSize;
104 constexpr
void setX1 (coordinate_type) noexcept;
105 constexpr
void setY1 (coordinate_type) noexcept;
106 constexpr
void setX2 (coordinate_type) noexcept;
107 constexpr
void setY2 (coordinate_type) noexcept;
108 constexpr
void setX (coordinate_type) noexcept;
109 constexpr
void setY (coordinate_type) noexcept;
110 constexpr
void setPos (coordinate_type, coordinate_type) noexcept;
111 constexpr
void setPos (
const FPoint&) noexcept;
112 constexpr
void setWidth (size_type) noexcept;
113 constexpr
void setHeight (size_type) noexcept;
114 constexpr
void setSize (size_type, size_type) noexcept;
115 constexpr
void setSize (
const FSize&) noexcept;
116 constexpr
void setRect (
const FRect&) noexcept;
117 constexpr
void setRect (
const FPoint&,
const FSize&) noexcept;
118 constexpr
void setRect (coordinate_type, coordinate_type, size_type, size_type) noexcept;
119 constexpr
void setCoordinates (
const FPoint&,
const FPoint&) noexcept;
120 constexpr
void setCoordinates ( coordinate_type, coordinate_type
121 , coordinate_type, coordinate_type ) noexcept;
124 constexpr
auto isEmpty()
const -> bool;
127 constexpr
auto x1_ref() & noexcept -> coordinate_reference;
128 constexpr
auto y1_ref() & noexcept -> coordinate_reference;
129 constexpr
auto x2_ref() & noexcept -> coordinate_reference;
130 constexpr
auto y2_ref() & noexcept -> coordinate_reference;
133 constexpr
void move (coordinate_type, coordinate_type) noexcept;
134 constexpr
void move (
const FPoint&) noexcept;
135 constexpr
void scaleBy (coordinate_type, coordinate_type) noexcept;
136 constexpr
void scaleBy (
const FPoint&) noexcept;
137 constexpr
auto contains (coordinate_type, coordinate_type)
const noexcept -> bool;
138 constexpr
auto contains (
const FPoint&)
const noexcept -> bool;
139 constexpr
auto contains (
const FRect&)
const noexcept -> bool;
140 constexpr
auto overlap (
const FRect&)
const noexcept -> bool;
141 auto intersect (
const FRect&)
const noexcept ->
FRect;
142 auto combined (
const FRect&)
const noexcept ->
FRect;
143 constexpr
void swap (
FRect&) noexcept;
147 coordinate_type X1{0};
148 coordinate_type Y1{0};
149 coordinate_type X2{-1};
150 coordinate_type Y2{-1};
153 friend constexpr
auto operator + (
const FRect& r,
const FSize& s) ->
FRect 157 , size_type(r.X2 - r.X1) + 1 + s.getWidth()
158 , size_type(r.Y2 - r.Y1) + 1 + s.getHeight() };
161 friend constexpr
auto operator - (
const FRect& r,
const FSize& s) ->
FRect 165 , size_type(r.X2 - r.X1) + 1 - s.getWidth()
166 , size_type(r.Y2 - r.Y1) + 1 - s.getHeight() };
169 friend constexpr
auto operator == (
const FRect& r1,
const FRect& r2) ->
bool 171 return r1.X1 == r2.X1
177 friend constexpr
auto operator != (
const FRect& r1,
const FRect& r2) ->
bool 179 return r1.X1 != r2.X1
185 friend inline auto operator << (std::ostream& outstr,
const FRect& r) -> std::ostream&
187 outstr << r.X1 <<
" " 194 friend inline auto operator >> (std::istream& instr,
FRect& r) -> std::istream&
196 coordinate_type x1{};
197 coordinate_type y1{};
198 coordinate_type x2{};
199 coordinate_type y2{};
201 if ( instr >> x1 >> y1 >> x2 >> y2 )
202 r.setCoordinates (x1, y1, x2, y2);
210 constexpr FRect::FRect ( coordinate_type x, coordinate_type y
211 , size_type width, size_type height ) noexcept
214 , X2{x + coordinate_type(width) - 1}
215 , Y2{y + coordinate_type(height) - 1}
219 constexpr FRect::FRect (
const FPoint& p,
const FSize& s) noexcept
222 , X2{p.getX() + int(s.getWidth()) - 1}
223 , Y2{p.getY() + int(s.getHeight()) - 1}
227 constexpr FRect::FRect (
const FPoint& p1,
const FPoint& p2) noexcept
235 inline auto FRect::getClassName()
const ->
FString 239 constexpr
auto FRect::getX1()
const noexcept -> coordinate_type
243 constexpr
auto FRect::getY1()
const noexcept -> coordinate_type
247 constexpr
auto FRect::getX2()
const noexcept -> coordinate_type
251 constexpr
auto FRect::getY2()
const noexcept -> coordinate_type
255 constexpr
auto FRect::getX()
const noexcept -> coordinate_type
259 constexpr
auto FRect::getY()
const noexcept -> coordinate_type
263 constexpr
auto FRect::getPos()
const noexcept ->
FPoint 264 {
return { X1, Y1 }; }
267 constexpr
auto FRect::getUpperLeftPos()
const noexcept ->
FPoint 268 {
return { X1, Y1 }; }
271 constexpr
auto FRect::getUpperRightPos()
const noexcept ->
FPoint 272 {
return { X2, Y1 }; }
275 constexpr
auto FRect::getLowerLeftPos()
const noexcept ->
FPoint 276 {
return { X1, Y2 }; }
279 constexpr
auto FRect::getLowerRightPos()
const noexcept ->
FPoint 280 {
return { X2, Y2 }; }
283 constexpr
auto FRect::getWidth()
const noexcept -> size_type
286 ?
static_cast<size_type
>(X2 - X1 + 1)
291 constexpr
auto FRect::getHeight()
const noexcept -> size_type
294 ?
static_cast<size_type
>(Y2 - Y1 + 1)
299 constexpr
auto FRect::getSize()
const noexcept ->
FSize 300 {
return {
FSize{getWidth(), getHeight()} }; }
303 constexpr
void FRect::setX1 (coordinate_type n) noexcept
307 constexpr
void FRect::setY1 (coordinate_type n) noexcept
311 constexpr
void FRect::setX2 (coordinate_type n) noexcept
315 constexpr
void FRect::setY2 (coordinate_type n) noexcept
319 constexpr
void FRect::setX (coordinate_type n) noexcept
321 const coordinate_type dX = X2 - X1;
327 constexpr
void FRect::setY (coordinate_type n) noexcept
329 const coordinate_type dY = Y2 - Y1;
335 constexpr
void FRect::setPos (coordinate_type x, coordinate_type y) noexcept
337 const coordinate_type dX = X2 - X1;
338 const coordinate_type dY = Y2 - Y1;
346 constexpr
void FRect::setPos (
const FPoint& p) noexcept
348 const coordinate_type dX = X2 - X1;
349 const coordinate_type dY = Y2 - Y1;
357 constexpr
void FRect::setWidth (size_type w) noexcept
358 { X2 = X1 + coordinate_type(w) - 1; }
361 constexpr
void FRect::setHeight (size_type h) noexcept
362 { Y2 = Y1 + coordinate_type(h) - 1; }
365 constexpr
void FRect::setSize (size_type width, size_type height) noexcept
367 X2 = X1 + coordinate_type(width) - 1;
368 Y2 = Y1 + coordinate_type(height) - 1;
372 constexpr
void FRect::setSize (
const FSize& s) noexcept
374 X2 = X1 + coordinate_type(s.getWidth()) - 1;
375 Y2 = Y1 + coordinate_type(s.getHeight()) - 1;
379 constexpr
void FRect::setRect (
const FRect& r) noexcept
388 constexpr
void FRect::setRect (
const FPoint& p,
const FSize& s) noexcept
392 X2 = p.getX() + coordinate_type(s.getWidth()) - 1;
393 Y2 = p.getY() + coordinate_type(s.getHeight()) - 1;
397 constexpr
void FRect::setRect ( coordinate_type x, coordinate_type y
398 , size_type width, size_type height ) noexcept
402 X2 = x + coordinate_type(width) - 1;
403 Y2 = y + coordinate_type(height) - 1;
407 constexpr
void FRect::setCoordinates (
const FPoint& p1,
const FPoint& p2) noexcept
409 setCoordinates (p1.getX(), p1.getY(), p2.getX(), p2.getY());
413 constexpr
void FRect::setCoordinates ( coordinate_type x1, coordinate_type y1
414 , coordinate_type x2, coordinate_type y2 ) noexcept
423 constexpr
auto FRect::isEmpty()
const ->
bool 424 {
return X2 == X1 - 1 && Y2 == Y1 - 1; }
427 constexpr
auto FRect::x1_ref() & noexcept -> coordinate_reference
431 constexpr
auto FRect::y1_ref() & noexcept -> coordinate_reference
435 constexpr
auto FRect::x2_ref() & noexcept -> coordinate_reference
439 constexpr
auto FRect::y2_ref() & noexcept -> coordinate_reference
443 constexpr
void FRect::move (coordinate_type dx, coordinate_type dy) noexcept
452 constexpr
void FRect::move (
const FPoint& d) noexcept
461 constexpr
void FRect::scaleBy (
int dx,
int dy) noexcept
468 constexpr
void FRect::scaleBy (
const FPoint& d) noexcept
475 constexpr
auto FRect::contains (coordinate_type x, coordinate_type y)
const noexcept ->
bool 477 return x >= X1 && x <= X2
478 && y >= Y1 && y <= Y2;
482 constexpr
auto FRect::contains (
const FPoint& p)
const noexcept ->
bool 484 return p.getX() >= X1 && p.getX() <= X2
485 && p.getY() >= Y1 && p.getY() <= Y2;
489 constexpr
auto FRect::contains (
const FRect& r)
const noexcept ->
bool 491 return r.X1 >= X1 && r.X2 <= X2
492 && r.Y1 >= Y1 && r.Y2 <= Y2;
496 constexpr
auto FRect::overlap (
const FRect& r)
const noexcept ->
bool 498 return ! (X2 < r.X1 || X1 > r.X2 || Y2 < r.Y1 || Y1 > r.Y2);
502 constexpr
void FRect::swap (
FRect& r) noexcept
504 coordinate_type tmp_X1{X1};
505 coordinate_type tmp_Y1{Y1};
506 coordinate_type tmp_X2{X2};
507 coordinate_type tmp_Y2{Y2};
Definition: class_template.cpp:25