Flutter Windows Embedder
geometry.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_SHELL_PLATFORM_COMMON_GEOMETRY_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_GEOMETRY_H_
7 
8 #include <cmath>
9 #include <limits>
10 #include <optional>
11 
12 namespace flutter {
13 
14 // A point in Cartesian space relative to a separately-maintained origin.
15 class Point {
16  public:
17  Point() = default;
18  Point(double x, double y) : x_(x), y_(y) {}
19  Point(const Point& point) = default;
20  Point& operator=(const Point& other) = default;
21 
22  double x() const { return x_; }
23  double y() const { return y_; }
24 
25  bool operator==(const Point& other) const {
26  return x_ == other.x_ && y_ == other.y_;
27  }
28 
29  private:
30  double x_ = 0.0;
31  double y_ = 0.0;
32 };
33 
34 // A 2D floating-point size with non-negative dimensions.
35 class Size {
36  public:
37  Size() = default;
38  Size(double width, double height)
39  : width_(std::fmax(0.0, width)), height_(std::fmax(0.0, height)) {}
40 
41  Size(const Size& size) = default;
42  Size& operator=(const Size& other) = default;
43 
44  double width() const { return width_; }
45  double height() const { return height_; }
46 
47  bool operator==(const Size& other) const {
48  return width_ == other.width_ && height_ == other.height_;
49  }
50  bool operator!=(const Size& other) const { return !(*this == other); }
51 
52  private:
53  double width_ = 0.0;
54  double height_ = 0.0;
55 };
56 
57 // A rectangle with position in Cartesian space specified relative to a
58 // separately-maintained origin.
59 class Rect {
60  public:
61  Rect() = default;
62  Rect(const Point& origin, const Size& size) : origin_(origin), size_(size) {}
63  Rect(const Rect& rect) = default;
64  Rect& operator=(const Rect& other) = default;
65 
66  double left() const { return origin_.x(); }
67  double top() const { return origin_.y(); }
68  double right() const { return origin_.x() + size_.width(); }
69  double bottom() const { return origin_.y() + size_.height(); }
70  double width() const { return size_.width(); }
71  double height() const { return size_.height(); }
72  Point origin() const { return origin_; }
73  Size size() const { return size_; }
74 
75  bool operator==(const Rect& other) const {
76  return origin_ == other.origin_ && size_ == other.size_;
77  }
78 
79  private:
80  Point origin_;
81  Size size_;
82 };
83 
84 // Encapsulates a min and max size that represents the constraints that some
85 // arbitrary box is able to take up.
87  public:
88  BoxConstraints() = default;
89  BoxConstraints(const std::optional<Size>& smallest,
90  const std::optional<Size>& biggest)
91  : smallest_(smallest.value_or(Size(0, 0))),
92  biggest_(
93  biggest.value_or(Size(std::numeric_limits<double>::infinity(),
94  std::numeric_limits<double>::infinity()))) {}
95  BoxConstraints(const BoxConstraints& other) = default;
96  Size biggest() const { return biggest_; }
97  Size smallest() const { return smallest_; }
98 
99  private:
100  Size smallest_ = Size(0, 0);
101  Size biggest_ = Size(std::numeric_limits<double>::infinity(),
102  std::numeric_limits<double>::infinity());
103 };
104 
105 } // namespace flutter
106 
107 #endif // FLUTTER_SHELL_PLATFORM_COMMON_GEOMETRY_H_
BoxConstraints(const BoxConstraints &other)=default
BoxConstraints(const std::optional< Size > &smallest, const std::optional< Size > &biggest)
Definition: geometry.h:89
Size smallest() const
Definition: geometry.h:97
Size biggest() const
Definition: geometry.h:96
Point()=default
bool operator==(const Point &other) const
Definition: geometry.h:25
Point & operator=(const Point &other)=default
Point(double x, double y)
Definition: geometry.h:18
double x() const
Definition: geometry.h:22
double y() const
Definition: geometry.h:23
Point(const Point &point)=default
double bottom() const
Definition: geometry.h:69
double right() const
Definition: geometry.h:68
double top() const
Definition: geometry.h:67
Size size() const
Definition: geometry.h:73
double height() const
Definition: geometry.h:71
double left() const
Definition: geometry.h:66
Rect(const Point &origin, const Size &size)
Definition: geometry.h:62
Point origin() const
Definition: geometry.h:72
Rect & operator=(const Rect &other)=default
Rect(const Rect &rect)=default
double width() const
Definition: geometry.h:70
Rect()=default
bool operator==(const Rect &other) const
Definition: geometry.h:75
Size(const Size &size)=default
Size()=default
bool operator!=(const Size &other) const
Definition: geometry.h:50
Size(double width, double height)
Definition: geometry.h:38
double height() const
Definition: geometry.h:45
bool operator==(const Size &other) const
Definition: geometry.h:47
Size & operator=(const Size &other)=default
double width() const
Definition: geometry.h:44