Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | List of all members
gfx::Rect Class Reference

#include <rect.h>

Public Member Functions

constexpr Rect ()=default
 
constexpr Rect (int width, int height)
 
constexpr Rect (int x, int y, int width, int height)
 
constexpr Rect (const Size &size)
 
constexpr Rect (const Point &origin, const Size &size)
 
constexpr int x () const
 
void set_x (int x)
 
constexpr int y () const
 
void set_y (int y)
 
constexpr int width () const
 
void set_width (int width)
 
constexpr int height () const
 
void set_height (int height)
 
constexpr const Pointorigin () const
 
void set_origin (const Point &origin)
 
constexpr const Sizesize () const
 
void set_size (const Size &size)
 
constexpr int right () const
 
constexpr int bottom () const
 
constexpr Point top_right () const
 
constexpr Point bottom_left () const
 
constexpr Point bottom_right () const
 
constexpr Point left_center () const
 
constexpr Point top_center () const
 
constexpr Point right_center () const
 
constexpr Point bottom_center () const
 
Vector2d OffsetFromOrigin () const
 
void SetRect (int x, int y, int width, int height)
 
void SetByBounds (int left, int top, int right, int bottom)
 
void Inset (int horizontal, int vertical)
 
void Inset (const Insets &insets)
 
void Inset (int left, int top, int right, int bottom)
 
void Offset (int horizontal, int vertical)
 
void Offset (const Vector2d &distance)
 
void operator+= (const Vector2d &offset)
 
void operator-= (const Vector2d &offset)
 
Insets InsetsFrom (const Rect &inner) const
 
bool IsEmpty () const
 
bool operator< (const Rect &other) const
 
bool Contains (int point_x, int point_y) const
 
bool Contains (const Point &point) const
 
bool Contains (const Rect &rect) const
 
bool Intersects (const Rect &rect) const
 
void Intersect (const Rect &rect)
 
void Union (const Rect &rect)
 
void Subtract (const Rect &rect)
 
void AdjustToFit (const Rect &rect)
 
Point CenterPoint () const
 
void ClampToCenteredSize (const Size &size)
 
void Transpose ()
 
void SplitVertically (Rect *left_half, Rect *right_half) const
 
bool SharesEdgeWith (const Rect &rect) const
 
int ManhattanDistanceToPoint (const Point &point) const
 
int ManhattanInternalDistance (const Rect &rect) const
 
std::string ToString () const
 
bool ApproximatelyEqual (const Rect &rect, int tolerance) const
 

Detailed Description

Definition at line 36 of file rect.h.

Constructor & Destructor Documentation

◆ Rect() [1/5]

constexpr gfx::Rect::Rect ( )
constexprdefault

◆ Rect() [2/5]

constexpr gfx::Rect::Rect ( int  width,
int  height 
)
inlineconstexpr

Definition at line 39 of file rect.h.

39: size_(width, height) {}
constexpr int height() const
Definition rect.h:79
constexpr int width() const
Definition rect.h:76

◆ Rect() [3/5]

constexpr gfx::Rect::Rect ( int  x,
int  y,
int  width,
int  height 
)
inlineconstexpr

Definition at line 40 of file rect.h.

41 : origin_(x, y), size_(GetClampedValue(x, width), GetClampedValue(y, height)) {}
constexpr int y() const
Definition rect.h:69
constexpr int x() const
Definition rect.h:62

◆ Rect() [4/5]

constexpr gfx::Rect::Rect ( const Size size)
inlineexplicitconstexpr

Definition at line 42 of file rect.h.

42: size_(size) {}
constexpr const Size & size() const
Definition rect.h:90

◆ Rect() [5/5]

constexpr gfx::Rect::Rect ( const Point origin,
const Size size 
)
inlineconstexpr

Definition at line 43 of file rect.h.

44 : origin_(origin),
45 size_(GetClampedValue(origin.x(), size.width()),
46 GetClampedValue(origin.y(), size.height())) {}
constexpr int y() const
Definition point.h:49
constexpr int x() const
Definition point.h:48
constexpr const Point & origin() const
Definition rect.h:82
constexpr int height() const
Definition size.h:50
constexpr int width() const
Definition size.h:49

Member Function Documentation

◆ AdjustToFit()

void gfx::Rect::AdjustToFit ( const Rect rect)

Definition at line 234 of file rect.cc.

234 {
235 int new_x = x();
236 int new_y = y();
237 int new_width = width();
238 int new_height = height();
239 AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width);
240 AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height);
241 SetRect(new_x, new_y, new_width, new_height);
242}
void SetRect(int x, int y, int width, int height)
Definition rect.h:110
sk_sp< SkBlender > blender SkRect rect
Definition SkRecords.h:350
void AdjustAlongAxis(int dst_origin, int dst_size, int *origin, int *size)
Definition rect.cc:49

◆ ApproximatelyEqual()

bool gfx::Rect::ApproximatelyEqual ( const Rect rect,
int  tolerance 
) const

Definition at line 299 of file rect.cc.

299 {
300 return std::abs(x() - rect.x()) <= tolerance &&
301 std::abs(y() - rect.y()) <= tolerance &&
302 std::abs(right() - rect.right()) <= tolerance &&
303 std::abs(bottom() - rect.bottom()) <= tolerance;
304}
constexpr int right() const
Definition rect.h:96
constexpr int bottom() const
Definition rect.h:97

◆ bottom()

constexpr int gfx::Rect::bottom ( ) const
inlineconstexpr

Definition at line 97 of file rect.h.

97{ return y() + height(); }

◆ bottom_center()

constexpr Point gfx::Rect::bottom_center ( ) const
inlineconstexpr

Definition at line 106 of file rect.h.

106{ return Point(x() + width() / 2, bottom()); }
TPoint< Scalar > Point
Definition point.h:316

◆ bottom_left()

constexpr Point gfx::Rect::bottom_left ( ) const
inlineconstexpr

Definition at line 100 of file rect.h.

100{ return Point(x(), bottom()); }

◆ bottom_right()

constexpr Point gfx::Rect::bottom_right ( ) const
inlineconstexpr

Definition at line 101 of file rect.h.

101{ return Point(right(), bottom()); }

◆ CenterPoint()

Point gfx::Rect::CenterPoint ( ) const

Definition at line 244 of file rect.cc.

244 {
245 return Point(x() + width() / 2, y() + height() / 2);
246}

◆ ClampToCenteredSize()

void gfx::Rect::ClampToCenteredSize ( const Size size)

Definition at line 248 of file rect.cc.

248 {
249 int new_width = std::min(width(), size.width());
250 int new_height = std::min(height(), size.height());
251 int new_x = x() + (width() - new_width) / 2;
252 int new_y = y() + (height() - new_height) / 2;
253 SetRect(new_x, new_y, new_width, new_height);
254}

◆ Contains() [1/3]

bool gfx::Rect::Contains ( const Point point) const
inline

Definition at line 156 of file rect.h.

156{ return Contains(point.x(), point.y()); }
bool Contains(int point_x, int point_y) const
Definition rect.cc:156

◆ Contains() [2/3]

bool gfx::Rect::Contains ( const Rect rect) const

Definition at line 161 of file rect.cc.

161 {
162 return (rect.x() >= x() && rect.right() <= right() && rect.y() >= y() &&
163 rect.bottom() <= bottom());
164}

◆ Contains() [3/3]

bool gfx::Rect::Contains ( int  point_x,
int  point_y 
) const

Definition at line 156 of file rect.cc.

156 {
157 return (point_x >= x()) && (point_x < right()) && (point_y >= y()) &&
158 (point_y < bottom());
159}

◆ height()

constexpr int gfx::Rect::height ( ) const
inlineconstexpr

Definition at line 79 of file rect.h.

79{ return size_.height(); }

◆ Inset() [1/3]

void gfx::Rect::Inset ( const Insets insets)

Definition at line 109 of file rect.cc.

109 {
110 Inset(insets.left(), insets.top(), insets.right(), insets.bottom());
111}
void Inset(int horizontal, int vertical)
Definition rect.h:123

◆ Inset() [2/3]

void gfx::Rect::Inset ( int  horizontal,
int  vertical 
)
inline

Definition at line 123 of file rect.h.

123{ Inset(horizontal, vertical, horizontal, vertical); }

◆ Inset() [3/3]

void gfx::Rect::Inset ( int  left,
int  top,
int  right,
int  bottom 
)

Definition at line 113 of file rect.cc.

113 {
114 origin_ += Vector2d(left, top);
115 // left+right might overflow/underflow, but width() - (left+right) might
116 // overflow as well.
117 set_width(base::ClampSub(width(), base::ClampAdd(left, right)));
118 set_height(base::ClampSub(height(), base::ClampAdd(top, bottom)));
119}
static bool left(const SkPoint &p0, const SkPoint &p1)
void set_width(int width)
Definition rect.h:77
void set_height(int height)
Definition rect.h:80

◆ InsetsFrom()

Insets gfx::Rect::InsetsFrom ( const Rect inner) const

Definition at line 139 of file rect.cc.

139 {
140 return Insets(inner.y() - y(), inner.x() - x(), bottom() - inner.bottom(),
141 right() - inner.right());
142}

◆ Intersect()

void gfx::Rect::Intersect ( const Rect rect)

Definition at line 171 of file rect.cc.

171 {
172 if (IsEmpty() || rect.IsEmpty()) {
173 SetRect(0, 0, 0, 0); // Throws away empty position.
174 return;
175 }
176
177 int left = std::max(x(), rect.x());
178 int top = std::max(y(), rect.y());
179 int new_right = std::min(right(), rect.right());
180 int new_bottom = std::min(bottom(), rect.bottom());
181
182 if (left >= new_right || top >= new_bottom) {
183 SetRect(0, 0, 0, 0); // Throws away empty position.
184 return;
185 }
186
187 SetByBounds(left, top, new_right, new_bottom);
188}
void SetByBounds(int left, int top, int right, int bottom)
Definition rect.cc:100
bool IsEmpty() const
Definition rect.h:140

◆ Intersects()

bool gfx::Rect::Intersects ( const Rect rect) const

Definition at line 166 of file rect.cc.

166 {
167 return !(IsEmpty() || rect.IsEmpty() || rect.x() >= right() ||
168 rect.right() <= x() || rect.y() >= bottom() || rect.bottom() <= y());
169}

◆ IsEmpty()

bool gfx::Rect::IsEmpty ( ) const
inline

Definition at line 140 of file rect.h.

140{ return size_.IsEmpty(); }
bool IsEmpty() const
Definition size.h:70

◆ left_center()

constexpr Point gfx::Rect::left_center ( ) const
inlineconstexpr

Definition at line 103 of file rect.h.

103{ return Point(x(), y() + height() / 2); }

◆ ManhattanDistanceToPoint()

int gfx::Rect::ManhattanDistanceToPoint ( const Point point) const

Definition at line 276 of file rect.cc.

276 {
277 int x_distance =
278 std::max<int>(0, std::max(x() - point.x(), point.x() - right()));
279 int y_distance =
280 std::max<int>(0, std::max(y() - point.y(), point.y() - bottom()));
281
282 return x_distance + y_distance;
283}

◆ ManhattanInternalDistance()

int gfx::Rect::ManhattanInternalDistance ( const Rect rect) const

Definition at line 285 of file rect.cc.

285 {
286 Rect c(*this);
287 c.Union(rect);
288
289 int x = std::max(0, c.width() - width() - rect.width() + 1);
290 int y = std::max(0, c.height() - height() - rect.height() + 1);
291 return x + y;
292}
constexpr Rect()=default

◆ Offset() [1/2]

void gfx::Rect::Offset ( const Vector2d distance)
inline

Definition at line 133 of file rect.h.

133{ Offset(distance.x(), distance.y()); }
void Offset(int horizontal, int vertical)
Definition rect.cc:121

◆ Offset() [2/2]

void gfx::Rect::Offset ( int  horizontal,
int  vertical 
)

Definition at line 121 of file rect.cc.

121 {
122 origin_ += Vector2d(horizontal, vertical);
123 // Ensure that width and height remain valid.
124 set_width(width());
126}

◆ OffsetFromOrigin()

Vector2d gfx::Rect::OffsetFromOrigin ( ) const
inline

Definition at line 108 of file rect.h.

108{ return Vector2d(x(), y()); }

◆ operator+=()

void gfx::Rect::operator+= ( const Vector2d offset)

Definition at line 128 of file rect.cc.

128 {
129 origin_ += offset;
130 // Ensure that width and height remain valid.
131 set_width(width());
133}
Point offset

◆ operator-=()

void gfx::Rect::operator-= ( const Vector2d offset)

Definition at line 135 of file rect.cc.

135 {
136 origin_ -= offset;
137}

◆ operator<()

bool gfx::Rect::operator< ( const Rect other) const

Definition at line 144 of file rect.cc.

144 {
145 if (origin_ == other.origin_) {
146 if (width() == other.width()) {
147 return height() < other.height();
148 } else {
149 return width() < other.width();
150 }
151 } else {
152 return origin_ < other.origin_;
153 }
154}

◆ origin()

constexpr const Point & gfx::Rect::origin ( ) const
inlineconstexpr

Definition at line 82 of file rect.h.

82{ return origin_; }

◆ right()

constexpr int gfx::Rect::right ( ) const
inlineconstexpr

Definition at line 96 of file rect.h.

96{ return x() + width(); }

◆ right_center()

constexpr Point gfx::Rect::right_center ( ) const
inlineconstexpr

Definition at line 105 of file rect.h.

105{ return Point(right(), y() + height() / 2); }

◆ set_height()

void gfx::Rect::set_height ( int  height)
inline

Definition at line 80 of file rect.h.

80{ size_.set_height(GetClampedValue(y(), height)); }
void set_height(int height)
Definition size.h:53

◆ set_origin()

void gfx::Rect::set_origin ( const Point origin)
inline

Definition at line 83 of file rect.h.

83 {
84 origin_ = origin;
85 // Ensure that width and height remain valid.
88 }

◆ set_size()

void gfx::Rect::set_size ( const Size size)
inline

Definition at line 91 of file rect.h.

91 {
94 }

◆ set_width()

void gfx::Rect::set_width ( int  width)
inline

Definition at line 77 of file rect.h.

77{ size_.set_width(GetClampedValue(x(), width)); }
void set_width(int width)
Definition size.h:52

◆ set_x()

void gfx::Rect::set_x ( int  x)
inline

Definition at line 64 of file rect.h.

64 {
65 origin_.set_x(x);
66 size_.set_width(GetClampedValue(x, width()));
67 }
void set_x(int x)
Definition point.h:50

◆ set_y()

void gfx::Rect::set_y ( int  y)
inline

Definition at line 71 of file rect.h.

71 {
72 origin_.set_y(y);
73 size_.set_height(GetClampedValue(y, height()));
74 }
void set_y(int y)
Definition point.h:51

◆ SetByBounds()

void gfx::Rect::SetByBounds ( int  left,
int  top,
int  right,
int  bottom 
)

Definition at line 100 of file rect.cc.

100 {
101 int x, y;
102 int width, height;
105 origin_.SetPoint(x, y);
106 size_.SetSize(width, height);
107}
void SetPoint(int x, int y)
Definition point.h:53
void SetSize(int width, int height)
Definition size.h:60
static void SaturatedClampRange(int min, int max, int *origin, int *span)
Definition rect.cc:63

◆ SetRect()

void gfx::Rect::SetRect ( int  x,
int  y,
int  width,
int  height 
)
inline

Definition at line 110 of file rect.h.

110 {
111 origin_.SetPoint(x, y);
112 // Ensure that width and height remain valid.
115 }

◆ SharesEdgeWith()

bool gfx::Rect::SharesEdgeWith ( const Rect rect) const

Definition at line 269 of file rect.cc.

269 {
270 return (y() == rect.y() && height() == rect.height() &&
271 (x() == rect.right() || right() == rect.x())) ||
272 (x() == rect.x() && width() == rect.width() &&
273 (y() == rect.bottom() || bottom() == rect.y()));
274}

◆ size()

constexpr const Size & gfx::Rect::size ( ) const
inlineconstexpr

Definition at line 90 of file rect.h.

90{ return size_; }

◆ SplitVertically()

void gfx::Rect::SplitVertically ( Rect left_half,
Rect right_half 
) const

Definition at line 260 of file rect.cc.

260 {
261 BASE_DCHECK(left_half);
262 BASE_DCHECK(right_half);
263
264 left_half->SetRect(x(), y(), width() / 2, height());
265 right_half->SetRect(left_half->right(), y(), width() - left_half->width(),
266 height());
267}
#define BASE_DCHECK(condition)
Definition logging.h:63

◆ Subtract()

void gfx::Rect::Subtract ( const Rect rect)

Definition at line 203 of file rect.cc.

203 {
204 if (!Intersects(rect))
205 return;
206 if (rect.Contains(*this)) {
207 SetRect(0, 0, 0, 0);
208 return;
209 }
210
211 int rx = x();
212 int ry = y();
213 int rr = right();
214 int rb = bottom();
215
216 if (rect.y() <= y() && rect.bottom() >= bottom()) {
217 // complete intersection in the y-direction
218 if (rect.x() <= x()) {
219 rx = rect.right();
220 } else if (rect.right() >= right()) {
221 rr = rect.x();
222 }
223 } else if (rect.x() <= x() && rect.right() >= right()) {
224 // complete intersection in the x-direction
225 if (rect.y() <= y()) {
226 ry = rect.bottom();
227 } else if (rect.bottom() >= bottom()) {
228 rb = rect.y();
229 }
230 }
231 SetByBounds(rx, ry, rr, rb);
232}
bool Intersects(const Rect &rect) const
Definition rect.cc:166

◆ top_center()

constexpr Point gfx::Rect::top_center ( ) const
inlineconstexpr

Definition at line 104 of file rect.h.

104{ return Point(x() + width() / 2, y()); }

◆ top_right()

constexpr Point gfx::Rect::top_right ( ) const
inlineconstexpr

Definition at line 99 of file rect.h.

99{ return Point(right(), y()); }

◆ ToString()

std::string gfx::Rect::ToString ( ) const

Definition at line 294 of file rect.cc.

294 {
295 return base::StringPrintf("%s %s", origin().ToString().c_str(),
296 size().ToString().c_str());
297}
std::string ToString() const
Definition rect.cc:294
std::string StringPrintf(const std::string &format, Args... args)

◆ Transpose()

void gfx::Rect::Transpose ( )

Definition at line 256 of file rect.cc.

256 {
257 SetRect(y(), x(), height(), width());
258}

◆ Union()

void gfx::Rect::Union ( const Rect rect)

Definition at line 190 of file rect.cc.

190 {
191 if (IsEmpty()) {
192 *this = rect;
193 return;
194 }
195 if (rect.IsEmpty())
196 return;
197
198 SetByBounds(std::min(x(), rect.x()), std::min(y(), rect.y()),
199 std::max(right(), rect.right()),
200 std::max(bottom(), rect.bottom()));
201}

◆ width()

constexpr int gfx::Rect::width ( ) const
inlineconstexpr

Definition at line 76 of file rect.h.

76{ return size_.width(); }

◆ x()

constexpr int gfx::Rect::x ( ) const
inlineconstexpr

Definition at line 62 of file rect.h.

62{ return origin_.x(); }

◆ y()

constexpr int gfx::Rect::y ( ) const
inlineconstexpr

Definition at line 69 of file rect.h.

69{ return origin_.y(); }

The documentation for this class was generated from the following files: