xtd - Reference Guide  0.1.1
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
rectangle.h
Go to the documentation of this file.
1 #pragma once
5 #include <cmath>
6 #include <cstdint>
7 #include <ostream>
8 #include <xtd/object.h>
9 #include <xtd/ustring.h>
10 #include "../drawing_export.h"
11 #include "point.h"
12 #include "size.h"
13 #include "rectangle_f.h"
14 
16 namespace xtd {
18  namespace drawing {
25  class drawing_export_ rectangle : public object {
26  public:
27  static const rectangle empty;
28 
29  rectangle() = default;
30  rectangle(int32_t x, int32_t y, int32_t width, int32_t height) : x_(x), y_(y), width_(width), height_(height) {}
31  rectangle(const point& location, const drawing::size& size) : rectangle(location.x(), location.y(), size.width(), size.height()) {}
32 
34  rectangle(const rectangle&) = default;
35  rectangle& operator=(const rectangle&) = default;
36  operator rectangle_f() const {return rectangle_f(static_cast<float>(x_), static_cast<float>(y_), static_cast<float>(width_), static_cast<float>(height_));}
37  bool operator==(const rectangle& value) const {return x_ == value.x_ && y_ == value.y_ && width_ == value.width_ && height_ == value.height_;}
38  bool operator!=(const rectangle& value) const {return !operator==(value);}
40 
41  int32_t bottom() const {return y_ + height_;}
42 
43  int32_t height() const {return height_;}
44  void height(int32_t height) {height_ = height;}
45 
46  bool is_empty() const {return *this == rectangle::empty;}
47 
48  int32_t left() const {return x_;}
49  void left(int32_t left) {x_ = left;}
50 
51  point location() const {return {x_, y_};}
52  void location(const point& location) {
53  x_ = location.x();
54  y_ = location.y();
55  }
56 
57  int32_t right() const {return x_ + width_;}
58 
59  drawing::size size() const {return {width_, height_};}
60  void size(const drawing::size& size) {
61  width_ = size.width();
62  height_ = size.height();
63  }
64 
65  int32_t top() const {return y_;}
66  void top(int32_t top) {y_ = top;}
67 
68  int32_t x() const {return x_;}
69  void x(int32_t x) {x_ = x;}
70 
71  int32_t y() const {return y_;}
72  void y(int32_t y) {y_ = y;}
73 
74  int32_t width() const {return width_;}
75  void width(int32_t width) {width_ = width;}
76 
77  static rectangle ceiling(const rectangle_f& rect) {return rectangle(static_cast<int32_t>(std::ceil(rect.x())), static_cast<int32_t>(std::ceil(rect.y())), static_cast<int32_t>(std::ceil(rect.width())), static_cast<int32_t>(std::ceil(rect.height())));}
78 
79  bool contains(const point& pt) const {return contains(pt.x(), pt.y());}
80  bool contains(const rectangle& rect) const {return x_ <= rect.x_ && (rect.x_ + rect.width_) <= (x_ + width_) && y_ <= rect.y_ && (rect.y_ + rect.height_) <= (y_ + height_);}
81  bool contains(int32_t x, int32_t y) const {return x_ <= x && x < x_ + width_ && y_ <= y && y < y_ + height_;}
82 
83  static rectangle from_ltrb(int32_t left, int32_t top, int32_t right, int32_t bottom) {return rectangle(left, top, right - left, bottom - top);}
84 
85  void inflate(const drawing::size& sz) {inflate(sz.width(), sz.height());}
86  void inflate(int width, int height) {
87  width_ += width;
88  height_ += height;
89  }
90  static rectangle inflate(const rectangle& rect, const drawing::size& sz) {return inflate(rect, sz.width(), sz.height());}
91  static rectangle inflate(const rectangle& rect, int width, int height) {
92  auto result = rect;
93  result.inflate(width, height);
94  return result;
95  }
96 
97  bool intersects_with(const rectangle& rect) const {return (rect.x_ < x_ + width_) && (x_ < (rect.x_ + rect.width_)) && (rect.y_ < y_ + height_) && (y_ < rect.y_ + rect.height_);}
98 
99  static rectangle make_intersect(const rectangle& a, const rectangle& b) {
100  auto result = a;
101  result.make_intersect(b);
102  return result;
103  }
104  void make_intersect(const rectangle& rect);
105 
106  static rectangle make_union(const rectangle& a, const rectangle& b) {
107  auto result = a;
108  result.make_union(b);
109  return result;
110  }
111  void make_union(const rectangle& rect);
112 
113  void offset(const point& pt) {offset(pt.x(), pt.y());}
114  void offset(int32_t dx, int32_t dy) {
115  x_ += dx;
116  y_ += dy;
117  }
118  static rectangle offset(const rectangle& rect, const point& pt) {return offset(rect, pt.x(), pt.y());}
119  static rectangle offset(const rectangle& rect, int x, int y) {
120  auto result = rect;
121  result.offset(x, y);
122  return result;
123  }
124 
125  static rectangle round(const rectangle_f& rect) {return rectangle(static_cast<int32_t>(std::round(rect.x())), static_cast<int32_t>(std::round(rect.y())), static_cast<int32_t>(std::round(rect.width())), static_cast<int32_t>(std::round(rect.height())));}
126 
127  static rectangle trunc(const rectangle_f& rect) {return rectangle(static_cast<int32_t>(std::trunc(rect.x())), static_cast<int32_t>(std::trunc(rect.y())), static_cast<int32_t>(std::trunc(rect.width())), static_cast<int32_t>(std::trunc(rect.height())));}
128 
129  xtd::ustring to_string() const noexcept override {return "{x=" + std::to_string(x_) + ", y=" + std::to_string(y_) + ", width=" + std::to_string(width_) + ", height=" + std::to_string(height_) + "}";}
130 
132  friend std::ostream& operator<<(std::ostream& os, const xtd::drawing::rectangle& rectangle) noexcept {return os << rectangle.to_string();}
134 
135  private:
136  int32_t x_ = 0;
137  int32_t y_ = 0;
138  int32_t width_ = 0;
139  int32_t height_ = 0;
140  };
141  }
142 }
Represents an ordered pair of integer x- and y-coordinates that defines a point in a two-dimensional ...
Definition: point.h:48
int32_t y() const
Gets the y-coordinate of this point.
Definition: point.h:205
int32_t x() const
Gets the x-coordinate of this point.
Definition: point.h:159
Stores a set of four floating-points that represent the location and size of a rectangle.
Definition: rectangle_f.h:28
Stores a set of four integers that represent the location and size of a rectangle.
Definition: rectangle.h:25
xtd::ustring to_string() const noexcept override
Returns a std::string that represents the current object.
Definition: rectangle.h:129
Stores an ordered pair of integers, which specify a height and width.
Definition: size.h:25
int32_t width() const
Gets the horizontal component of this Size class.
Definition: size.h:67
int32_t height() const
Gets he vertical component of this Size Class.
Definition: size.h:57
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition: object.h:26
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:48
#define drawing_export_
Define shared library export.
Definition: drawing_export.h:13
std::string to_string(const value_t &value, const std::string &fmt, const std::locale &loc)
Convert a specified value into a string with specified format and locale.
Definition: to_string.h:37
@ a
The A key.
@ y
The Y key.
@ b
The B key.
@ x
The X key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17
Contains xtd::object class.
Contains xtd::drawing::point class.
Contains xtd::drawing::rectangle_f class.
Contains xtd::drawing::size class.
Contains xtd::ustring class.