1 // This file is part of the utio library, a terminal I/O library.
3 // Copyright (C) 2004 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
9 #ifndef GDT_H_10D99A7949ED8990743247ED3E6897DC
10 #define GDT_H_10D99A7949ED8990743247ED3E6897DC
15 /// Contains geometric primitive objects.
18 typedef int16_t coord_t
; ///< A geometric coordinate.
19 typedef uint16_t dim_t
; ///< A dimension.
20 typedef tuple
<2, dim_t
> Size2d
; ///< A geometric size.
21 typedef tuple
<2, coord_t
> Point2d
; ///< A geometric point.
23 /// Represents a geometric rectangle.
24 class Rect
: public tuple
<2, Point2d
> {
26 inline Rect (const Rect
& r
)
27 : tuple
<2, Point2d
> (r
) {}
28 inline Rect (coord_t x
= 0, coord_t y
= 0, dim_t w
= 0, dim_t h
= 0)
29 { at(0)[0] = x
; at(0)[1] = y
;
30 at(1)[0] = x
+ w
; at(1)[1] = y
+ h
; }
31 inline Rect (const Point2d
& tl
, const Point2d
& br
)
32 { at(0) = tl
; at(1) = br
; }
33 inline Rect (const Point2d
& tl
, const Size2d
& wh
)
34 { at(0) = at(1) = tl
; at(1) += wh
; }
35 inline size_t Width (void) const { return (at(1)[0] - at(0)[0]); }
36 inline size_t Height (void) const { return (at(1)[1] - at(0)[1]); }
37 inline bool Empty (void) const { return (!Width() | !Height()); }
38 inline Size2d
Size (void) const { return (at(1) - at(0)); }
39 inline void Expand (coord_t d
) { at(0) -= d
; at(1) += d
; }
40 inline const Rect
& operator+= (const Point2d
& d
) { at(0) += d
; at(1) += d
; return (*this); }
41 inline const Rect
& operator-= (const Point2d
& d
) { at(0) -= d
; at(1) -= d
; return (*this); }
42 inline const Rect
& operator+= (const Size2d
& d
) { at(0) += d
; at(1) += d
; return (*this); }
43 inline const Rect
& operator-= (const Size2d
& d
) { at(0) -= d
; at(1) -= d
; return (*this); }
44 inline Rect
operator+ (const Point2d
& d
) const { Rect
r (*this); r
+= d
; return (r
); }
45 inline Rect
operator- (const Point2d
& d
) const { Rect
r (*this); r
-= d
; return (r
); }
46 inline Rect
operator+ (const Size2d
& d
) const { Rect
r (*this); r
+= d
; return (r
); }
47 inline Rect
operator- (const Size2d
& d
) const { Rect
r (*this); r
-= d
; return (r
); }