xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
rectangle_f.h
Go to the documentation of this file.
1 #pragma once
5 #include <cstdint>
6 #include <ostream>
7 #include <string>
8 #include <xtd/object.h>
9 #include <xtd/ustring.h>
10 #include "../drawing_export.h"
11 #include "point_f.h"
12 #include "size_f.h"
13 
15 namespace xtd {
17  namespace drawing {
19  class rectangle;
21 
28  class drawing_export_ rectangle_f : public object {
29  public:
30  static const rectangle_f empty;
31 
32  rectangle_f() = default;
33  rectangle_f(float x, float y, float width, float height) : x_(x), y_(y), width_(width), height_(height) {}
34  rectangle_f(const point_f& location, const size_f& size) : rectangle_f(location.x(), location.y(), size.width(), size.height()) {}
35 
37  rectangle_f(const rectangle_f&) = default;
38  rectangle_f& operator=(const rectangle_f&) = default;
39  bool operator==(const rectangle_f& value) const {return x_ == value.x_ && y_ == value.y_ && width_ == value.width_ && height_ == value.height_;}
40  bool operator!=(const rectangle_f& value) const {return !operator==(value);}
42 
43  float bottom() const {return y_ + height_;}
44 
45  float height() const {return height_;}
46  void height(float height) {height_ = height;}
47 
48  bool is_empty() const {return *this == rectangle_f::empty;}
49 
50  float left() const {return x_;}
51  void left(float left) {x_ = left;}
52 
53  point_f location() const {return {x_, y_};}
54  void location(const point_f& location) {
55  x_ = location.x();
56  y_ = location.y();
57  }
58 
59  float right() const {return x_ + width_;}
60 
61  size_f size() const {return {width_, height_};}
62  void size(const size_f& size) {
63  width_ = size.width();
64  height_ = size.height();
65  }
66 
67  float top() const {return y_;}
68  void top(float top) {y_ = top;}
69 
70  float x() const {return x_;}
71  void x(float x) {x_ = x;}
72 
73  float y() const {return y_;}
74  void y(float y) {y_ = y;}
75 
76  float width() const {return width_;}
77  void width(float width) {width_ = width;}
78 
79  bool contains(float x, float y) const {return x_ <= x && x < x_ + width_ && y_ <= y && y < y_ + height_;}
80  bool contains(const point_f& pt) const {return contains(pt.x(), pt.y());}
81  bool contains(const rectangle_f& rect) const {return x_ <= rect.x_ && (rect.x_ + rect.width_) <= (x_ + width_) && y_ <= rect.y_ && (rect.y_ + rect.height_) <= (y_ + height_);}
82 
83  static rectangle_f from_ltrb(float left, float top, float right, float bottom) {return rectangle_f(left, top, right - left, bottom - top);}
84 
85  void inflate(const drawing::size_f& sz) {inflate(sz.width(), sz.height());}
86  void inflate(float width, float height) {
87  width_ += width;
88  height_ += height;
89  }
90  static rectangle_f inflate(const rectangle_f& rect, const drawing::size_f& sz) {return inflate(rect, sz.width(), sz.height());}
91  static rectangle_f inflate(const rectangle_f& rect, float width, float height) {
92  auto result = rect;
93  result.inflate(width, height);
94  return result;
95  }
96 
97  bool intersects_with(const rectangle_f& 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_f make_intersect(const rectangle_f& a, const rectangle_f& b) {
100  auto result = a;
101  result.make_intersect(b);
102  return result;
103  }
104  void make_intersect(const rectangle_f& rect);
105 
106  static rectangle_f make_union(const rectangle_f& a, const rectangle_f& b) {
107  auto result = a;
108  result.make_union(b);
109  return result;
110  }
111  void make_union(const rectangle_f& rect);
112 
113  void offset(const point_f& pt) {offset(pt.x(), pt.y());}
114  void offset(float dx, float dy) {
115  x_ += dx;
116  y_ += dy;
117  }
118  static rectangle_f offset(const rectangle_f& rect, const point_f& pt) {return offset(rect, pt.x(), pt.y());}
119  static rectangle_f offset(const rectangle_f& rect, float x, float y) {
120  auto result = rect;
121  result.offset(x, y);
122  return result;
123  }
124 
125  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_) + "}";}
126 
128  friend std::ostream& operator<<(std::ostream& os, const xtd::drawing::rectangle_f& rectangle) noexcept {return os << rectangle.to_string();}
130 
131  private:
132  float x_ = 0;
133  float y_ = 0;
134  float width_ = 0;
135  float height_ = 0;
136  };
137  }
138 }
xtd::ustring to_string() const noexcept override
Returns a std::string that represents the current object.
Definition: rectangle_f.h:125
Stores an ordered pair of integers, which specify a height and width.
Definition: size.h:25
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
Stores a set of four floating-points that represent the location and size of a rectangle.
Definition: rectangle_f.h:28
Contains xtd::drawing::point_f class.
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:48
Represents an ordered pair of floating-point x- and y-coordinates that defines a point in a two-dimen...
Definition: point_f.h:26
Stores an ordered pair of floating-point, which specify a height and width.
Definition: size_f.h:24
Stores a set of four integers that represent the location and size of a rectangle.
Definition: rectangle.h:25
Contains xtd::ustring class.
Contains xtd::object class.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes...
Definition: object.h:26
Contains xtd::drawing::size_f class.
float width() const
Gets the horizontal component of this Size class.
Definition: size_f.h:65
float height() const
Gets he vertical component of this Size Class.
Definition: size_f.h:55
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