Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / gfx / geometry / rect_f.h
blobdc233a77154a37c9424d3e305783e90f51d84fdf
1 // Copyright (c) 2012 The Chromium 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.
5 #ifndef UI_GFX_GEOMETRY_RECT_F_H_
6 #define UI_GFX_GEOMETRY_RECT_F_H_
8 #include <iosfwd>
9 #include <string>
11 #include "ui/gfx/geometry/point_f.h"
12 #include "ui/gfx/geometry/size_f.h"
13 #include "ui/gfx/geometry/vector2d_f.h"
15 namespace gfx {
17 class InsetsF;
19 // A floating version of gfx::Rect.
20 class GFX_EXPORT RectF {
21 public:
22 RectF() {}
23 RectF(float width, float height) : size_(width, height) {}
24 RectF(float x, float y, float width, float height)
25 : origin_(x, y), size_(width, height) {}
26 explicit RectF(const SizeF& size) : size_(size) {}
27 RectF(const PointF& origin, const SizeF& size)
28 : origin_(origin), size_(size) {}
30 ~RectF() {}
32 float x() const { return origin_.x(); }
33 void set_x(float x) { origin_.set_x(x); }
35 float y() const { return origin_.y(); }
36 void set_y(float y) { origin_.set_y(y); }
38 float width() const { return size_.width(); }
39 void set_width(float width) { size_.set_width(width); }
41 float height() const { return size_.height(); }
42 void set_height(float height) { size_.set_height(height); }
44 const PointF& origin() const { return origin_; }
45 void set_origin(const PointF& origin) { origin_ = origin; }
47 const SizeF& size() const { return size_; }
48 void set_size(const SizeF& size) { size_ = size; }
50 float right() const { return x() + width(); }
51 float bottom() const { return y() + height(); }
53 PointF top_right() const { return PointF(right(), y()); }
54 PointF bottom_left() const { return PointF(x(), bottom()); }
55 PointF bottom_right() const { return PointF(right(), bottom()); }
57 Vector2dF OffsetFromOrigin() const { return Vector2dF(x(), y()); }
59 void SetRect(float x, float y, float width, float height) {
60 origin_.SetPoint(x, y);
61 size_.SetSize(width, height);
64 // Shrink the rectangle by a horizontal and vertical distance on all sides.
65 void Inset(float horizontal, float vertical) {
66 Inset(horizontal, vertical, horizontal, vertical);
69 // Shrink the rectangle by the given insets.
70 void Inset(const InsetsF& insets);
72 // Shrink the rectangle by the specified amount on each side.
73 void Inset(float left, float top, float right, float bottom);
75 // Move the rectangle by a horizontal and vertical distance.
76 void Offset(float horizontal, float vertical);
77 void Offset(const Vector2dF& distance) { Offset(distance.x(), distance.y()); }
78 void operator+=(const Vector2dF& offset);
79 void operator-=(const Vector2dF& offset);
81 InsetsF InsetsFrom(const RectF& inner) const;
83 // Returns true if the area of the rectangle is zero.
84 bool IsEmpty() const { return size_.IsEmpty(); }
86 // A rect is less than another rect if its origin is less than
87 // the other rect's origin. If the origins are equal, then the
88 // shortest rect is less than the other. If the origin and the
89 // height are equal, then the narrowest rect is less than.
90 // This comparison is required to use Rects in sets, or sorted
91 // vectors.
92 bool operator<(const RectF& other) const;
94 // Returns true if the point identified by point_x and point_y falls inside
95 // this rectangle. The point (x, y) is inside the rectangle, but the
96 // point (x + width, y + height) is not.
97 bool Contains(float point_x, float point_y) const;
99 // Returns true if the specified point is contained by this rectangle.
100 bool Contains(const PointF& point) const {
101 return Contains(point.x(), point.y());
104 // Returns true if this rectangle contains the specified rectangle.
105 bool Contains(const RectF& rect) const;
107 // Returns true if this rectangle intersects the specified rectangle.
108 // An empty rectangle doesn't intersect any rectangle.
109 bool Intersects(const RectF& rect) const;
111 // Computes the intersection of this rectangle with the given rectangle.
112 void Intersect(const RectF& rect);
114 // Computes the union of this rectangle with the given rectangle. The union
115 // is the smallest rectangle containing both rectangles.
116 void Union(const RectF& rect);
118 // Computes the rectangle resulting from subtracting |rect| from |*this|,
119 // i.e. the bounding rect of |Region(*this) - Region(rect)|.
120 void Subtract(const RectF& rect);
122 // Fits as much of the receiving rectangle into the supplied rectangle as
123 // possible, becoming the result. For example, if the receiver had
124 // a x-location of 2 and a width of 4, and the supplied rectangle had
125 // an x-location of 0 with a width of 5, the returned rectangle would have
126 // an x-location of 1 with a width of 4.
127 void AdjustToFit(const RectF& rect);
129 // Returns the center of this rectangle.
130 PointF CenterPoint() const;
132 // Becomes a rectangle that has the same center point but with a size capped
133 // at given |size|.
134 void ClampToCenteredSize(const SizeF& size);
136 // Splits |this| in two halves, |left_half| and |right_half|.
137 void SplitVertically(RectF* left_half, RectF* right_half) const;
139 // Returns true if this rectangle shares an entire edge (i.e., same width or
140 // same height) with the given rectangle, and the rectangles do not overlap.
141 bool SharesEdgeWith(const RectF& rect) const;
143 // Returns the manhattan distance from the rect to the point. If the point is
144 // inside the rect, returns 0.
145 float ManhattanDistanceToPoint(const PointF& point) const;
147 // Returns the manhattan distance between the contents of this rect and the
148 // contents of the given rect. That is, if the intersection of the two rects
149 // is non-empty then the function returns 0. If the rects share a side, it
150 // returns the smallest non-zero value appropriate for float.
151 float ManhattanInternalDistance(const RectF& rect) const;
153 // Scales the rectangle by |scale|.
154 void Scale(float scale) {
155 Scale(scale, scale);
158 void Scale(float x_scale, float y_scale) {
159 set_origin(ScalePoint(origin(), x_scale, y_scale));
160 set_size(ScaleSize(size(), x_scale, y_scale));
163 // This method reports if the RectF can be safely converted to an integer
164 // Rect. When it is false, some dimension of the RectF is outside the bounds
165 // of what an integer can represent, and converting it to a Rect will require
166 // clamping.
167 bool IsExpressibleAsRect() const;
169 std::string ToString() const;
171 private:
172 PointF origin_;
173 SizeF size_;
176 inline bool operator==(const RectF& lhs, const RectF& rhs) {
177 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size();
180 inline bool operator!=(const RectF& lhs, const RectF& rhs) {
181 return !(lhs == rhs);
184 inline RectF operator+(const RectF& lhs, const Vector2dF& rhs) {
185 return RectF(lhs.x() + rhs.x(), lhs.y() + rhs.y(),
186 lhs.width(), lhs.height());
189 inline RectF operator-(const RectF& lhs, const Vector2dF& rhs) {
190 return RectF(lhs.x() - rhs.x(), lhs.y() - rhs.y(),
191 lhs.width(), lhs.height());
194 inline RectF operator+(const Vector2dF& lhs, const RectF& rhs) {
195 return rhs + lhs;
198 GFX_EXPORT RectF IntersectRects(const RectF& a, const RectF& b);
199 GFX_EXPORT RectF UnionRects(const RectF& a, const RectF& b);
200 GFX_EXPORT RectF SubtractRects(const RectF& a, const RectF& b);
202 inline RectF ScaleRect(const RectF& r, float x_scale, float y_scale) {
203 return RectF(r.x() * x_scale, r.y() * y_scale,
204 r.width() * x_scale, r.height() * y_scale);
207 inline RectF ScaleRect(const RectF& r, float scale) {
208 return ScaleRect(r, scale, scale);
211 // Constructs a rectangle with |p1| and |p2| as opposite corners.
213 // This could also be thought of as "the smallest rect that contains both
214 // points", except that we consider points on the right/bottom edges of the
215 // rect to be outside the rect. So technically one or both points will not be
216 // contained within the rect, because they will appear on one of these edges.
217 GFX_EXPORT RectF BoundingRect(const PointF& p1, const PointF& p2);
219 // This is declared here for use in gtest-based unit tests but is defined in
220 // the gfx_test_support target. Depend on that to use this in your unit test.
221 // This should not be used in production code - call ToString() instead.
222 void PrintTo(const RectF& rect, ::std::ostream* os);
224 } // namespace gfx
226 #endif // UI_GFX_GEOMETRY_RECT_F_H_