rect_f.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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.
  4. #ifndef UI_GFX_GEOMETRY_RECT_F_H_
  5. #define UI_GFX_GEOMETRY_RECT_F_H_
  6. #include <iosfwd>
  7. #include <string>
  8. #include "build/build_config.h"
  9. #include "ui/gfx/geometry/insets_f.h"
  10. #include "ui/gfx/geometry/outsets_f.h"
  11. #include "ui/gfx/geometry/point_f.h"
  12. #include "ui/gfx/geometry/rect.h"
  13. #include "ui/gfx/geometry/size_f.h"
  14. #include "ui/gfx/geometry/vector2d_f.h"
  15. #if BUILDFLAG(IS_APPLE)
  16. typedef struct CGRect CGRect;
  17. #endif
  18. namespace gfx {
  19. // A floating version of gfx::Rect.
  20. class GEOMETRY_EXPORT RectF {
  21. public:
  22. constexpr RectF() = default;
  23. constexpr RectF(float width, float height) : size_(width, height) {}
  24. constexpr RectF(float x, float y, float width, float height)
  25. : origin_(x, y), size_(width, height) {}
  26. constexpr explicit RectF(const SizeF& size) : size_(size) {}
  27. constexpr RectF(const PointF& origin, const SizeF& size)
  28. : origin_(origin), size_(size) {}
  29. constexpr explicit RectF(const Rect& r)
  30. : RectF(static_cast<float>(r.x()),
  31. static_cast<float>(r.y()),
  32. static_cast<float>(r.width()),
  33. static_cast<float>(r.height())) {}
  34. #if BUILDFLAG(IS_APPLE)
  35. explicit RectF(const CGRect& r);
  36. // Construct an equivalent CoreGraphics object.
  37. CGRect ToCGRect() const;
  38. #endif
  39. constexpr float x() const { return origin_.x(); }
  40. void set_x(float x) { origin_.set_x(x); }
  41. constexpr float y() const { return origin_.y(); }
  42. void set_y(float y) { origin_.set_y(y); }
  43. constexpr float width() const { return size_.width(); }
  44. void set_width(float width) { size_.set_width(width); }
  45. constexpr float height() const { return size_.height(); }
  46. void set_height(float height) { size_.set_height(height); }
  47. constexpr const PointF& origin() const { return origin_; }
  48. void set_origin(const PointF& origin) { origin_ = origin; }
  49. constexpr const SizeF& size() const { return size_; }
  50. void set_size(const SizeF& size) { size_ = size; }
  51. constexpr float right() const { return x() + width(); }
  52. constexpr float bottom() const { return y() + height(); }
  53. constexpr PointF top_right() const { return PointF(right(), y()); }
  54. constexpr PointF bottom_left() const { return PointF(x(), bottom()); }
  55. constexpr PointF bottom_right() const { return PointF(right(), bottom()); }
  56. constexpr PointF left_center() const {
  57. return PointF(x(), y() + height() / 2);
  58. }
  59. constexpr PointF top_center() const { return PointF(x() + width() / 2, y()); }
  60. constexpr PointF right_center() const {
  61. return PointF(right(), y() + height() / 2);
  62. }
  63. constexpr PointF bottom_center() const {
  64. return PointF(x() + width() / 2, bottom());
  65. }
  66. Vector2dF OffsetFromOrigin() const { return Vector2dF(x(), y()); }
  67. void SetRect(float x, float y, float width, float height) {
  68. origin_.SetPoint(x, y);
  69. size_.SetSize(width, height);
  70. }
  71. // Shrinks the rectangle by |inset| on all sides.
  72. void Inset(float inset) { Inset(InsetsF(inset)); }
  73. // Shrinks the rectangle by the given |insets|.
  74. void Inset(const InsetsF& insets);
  75. // Expands the rectangle by |outset| on all sides.
  76. void Outset(float outset) { Inset(-outset); }
  77. // Expands the rectangle by the given |outsets|.
  78. void Outset(const OutsetsF& outsets) { Inset(outsets.ToInsets()); }
  79. // Move the rectangle by a horizontal and vertical distance.
  80. void Offset(float horizontal, float vertical);
  81. void Offset(const Vector2dF& distance) { Offset(distance.x(), distance.y()); }
  82. void operator+=(const Vector2dF& offset);
  83. void operator-=(const Vector2dF& offset);
  84. InsetsF InsetsFrom(const RectF& inner) const;
  85. // Returns true if the area of the rectangle is zero.
  86. constexpr bool IsEmpty() const { return size_.IsEmpty(); }
  87. // A rect is less than another rect if its origin is less than
  88. // the other rect's origin. If the origins are equal, then the
  89. // shortest rect is less than the other. If the origin and the
  90. // height are equal, then the narrowest rect is less than.
  91. // This comparison is required to use Rects in sets, or sorted
  92. // vectors.
  93. bool operator<(const RectF& other) const;
  94. // Returns true if the point identified by point_x and point_y falls inside
  95. // this rectangle (including the left and the top edges, excluding the right
  96. // and the bottom edges). If this rectangle is empty, this method returns
  97. // false regardless of the point.
  98. 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());
  102. }
  103. // Similar to Contains(), but uses edge-inclusive geometry, i.e. also returns
  104. // true if the point is on the right or the bottom edge. If this rectangle
  105. // is empty, this method returns true only if the point is at the origin of
  106. // this rectangle.
  107. bool InclusiveContains(float point_x, float point_y) const;
  108. bool InclusiveContains(const PointF& point) const {
  109. return InclusiveContains(point.x(), point.y());
  110. }
  111. // Returns true if this rectangle contains the specified rectangle.
  112. bool Contains(const RectF& rect) const;
  113. // Returns true if this rectangle intersects the specified rectangle.
  114. // An empty rectangle doesn't intersect any rectangle.
  115. bool Intersects(const RectF& rect) const;
  116. // Sets this rect to be the intersection of this rectangle with the given
  117. // rectangle.
  118. void Intersect(const RectF& rect);
  119. // Sets this rect to be the intersection of itself and |rect| using
  120. // edge-inclusive geometry. If the two rectangles overlap but the overlap
  121. // region is zero-area (either because one of the two rectangles is zero-area,
  122. // or because the rectangles overlap at an edge or a corner), the result is
  123. // the zero-area intersection. The return value indicates whether the two
  124. // rectangle actually have an intersection, since checking the result for
  125. // isEmpty() is not conclusive.
  126. bool InclusiveIntersect(const RectF& rect);
  127. // Sets this rect to be the union of this rectangle with the given rectangle.
  128. // The union is the smallest rectangle containing both rectangles if not
  129. // empty. If both rects are empty, this rect will become |rect|.
  130. void Union(const RectF& rect);
  131. // Similar to Union(), but the result will contain both rectangles even if
  132. // either of them is empty. For example, union of (100, 100, 0x0) and
  133. // (200, 200, 50x0) is (100, 100, 150x100).
  134. void UnionEvenIfEmpty(const RectF& rect);
  135. // Sets this rect to be the rectangle resulting from subtracting |rect| from
  136. // |*this|, i.e. the bounding rect of |Region(*this) - Region(rect)|.
  137. void Subtract(const RectF& rect);
  138. // Fits as much of the receiving rectangle into the supplied rectangle as
  139. // possible, becoming the result. For example, if the receiver had
  140. // a x-location of 2 and a width of 4, and the supplied rectangle had
  141. // an x-location of 0 with a width of 5, the returned rectangle would have
  142. // an x-location of 1 with a width of 4.
  143. void AdjustToFit(const RectF& rect);
  144. // Returns the center of this rectangle.
  145. PointF CenterPoint() const;
  146. // Becomes a rectangle that has the same center point but with a size capped
  147. // at given |size|.
  148. void ClampToCenteredSize(const SizeF& size);
  149. // Transpose x and y axis.
  150. void Transpose();
  151. // Splits |this| in two halves, |left_half| and |right_half|.
  152. void SplitVertically(RectF* left_half, RectF* right_half) const;
  153. // Returns true if this rectangle shares an entire edge (i.e., same width or
  154. // same height) with the given rectangle, and the rectangles do not overlap.
  155. bool SharesEdgeWith(const RectF& rect) const;
  156. // Returns the manhattan distance from the rect to the point. If the point is
  157. // inside the rect, returns 0.
  158. float ManhattanDistanceToPoint(const PointF& point) const;
  159. // Returns the manhattan distance between the contents of this rect and the
  160. // contents of the given rect. That is, if the intersection of the two rects
  161. // is non-empty then the function returns 0. If the rects share a side, it
  162. // returns the smallest non-zero value appropriate for float.
  163. float ManhattanInternalDistance(const RectF& rect) const;
  164. // Returns the closest point in or on an edge of this rect to the given point.
  165. PointF ClosestPoint(const PointF& point) const;
  166. // Scales the rectangle by |scale|.
  167. void Scale(float scale) {
  168. Scale(scale, scale);
  169. }
  170. void Scale(float x_scale, float y_scale) {
  171. set_origin(ScalePoint(origin(), x_scale, y_scale));
  172. set_size(ScaleSize(size(), x_scale, y_scale));
  173. }
  174. // This method reports if the RectF can be safely converted to an integer
  175. // Rect. When it is false, some dimension of the RectF is outside the bounds
  176. // of what an integer can represent, and converting it to a Rect will require
  177. // clamping.
  178. bool IsExpressibleAsRect() const;
  179. std::string ToString() const;
  180. bool ApproximatelyEqual(const RectF& rect,
  181. float tolerance_x,
  182. float tolerance_y) const;
  183. private:
  184. PointF origin_;
  185. SizeF size_;
  186. };
  187. constexpr bool operator==(const RectF& lhs, const RectF& rhs) {
  188. return lhs.origin() == rhs.origin() && lhs.size() == rhs.size();
  189. }
  190. constexpr bool operator!=(const RectF& lhs, const RectF& rhs) {
  191. return !(lhs == rhs);
  192. }
  193. inline RectF operator+(const RectF& lhs, const Vector2dF& rhs) {
  194. return RectF(lhs.x() + rhs.x(), lhs.y() + rhs.y(),
  195. lhs.width(), lhs.height());
  196. }
  197. inline RectF operator-(const RectF& lhs, const Vector2dF& rhs) {
  198. return RectF(lhs.x() - rhs.x(), lhs.y() - rhs.y(),
  199. lhs.width(), lhs.height());
  200. }
  201. inline RectF operator+(const Vector2dF& lhs, const RectF& rhs) {
  202. return rhs + lhs;
  203. }
  204. GEOMETRY_EXPORT RectF IntersectRects(const RectF& a, const RectF& b);
  205. GEOMETRY_EXPORT RectF UnionRects(const RectF& a, const RectF& b);
  206. GEOMETRY_EXPORT RectF UnionRectsEvenIfEmpty(const RectF& a, const RectF& b);
  207. GEOMETRY_EXPORT RectF SubtractRects(const RectF& a, const RectF& b);
  208. inline RectF ScaleRect(const RectF& r, float x_scale, float y_scale) {
  209. return RectF(r.x() * x_scale, r.y() * y_scale,
  210. r.width() * x_scale, r.height() * y_scale);
  211. }
  212. inline RectF ScaleRect(const RectF& r, float scale) {
  213. return ScaleRect(r, scale, scale);
  214. }
  215. inline RectF TransposeRect(const RectF& r) {
  216. return RectF(r.y(), r.x(), r.height(), r.width());
  217. }
  218. // Constructs a rectangle with |p1| and |p2| as opposite corners.
  219. //
  220. // This could also be thought of as "the smallest rect that contains both
  221. // points", except that we consider points on the right/bottom edges of the
  222. // rect to be outside the rect. So technically one or both points will not be
  223. // contained within the rect, because they will appear on one of these edges.
  224. GEOMETRY_EXPORT RectF BoundingRect(const PointF& p1, const PointF& p2);
  225. // Return a maximum rectangle in which any point is covered by either a or b.
  226. GEOMETRY_EXPORT RectF MaximumCoveredRect(const RectF& a, const RectF& b);
  227. // Returns the rect in |dest_rect| corresponding to |r] in |src_rect| when
  228. // |src_rect| is mapped to |dest_rect|.
  229. GEOMETRY_EXPORT RectF MapRect(const RectF& r,
  230. const RectF& src_rect,
  231. const RectF& dest_rect);
  232. // This is declared here for use in gtest-based unit tests but is defined in
  233. // the //ui/gfx:test_support target. Depend on that to use this in your unit
  234. // test. This should not be used in production code - call ToString() instead.
  235. void PrintTo(const RectF& rect, ::std::ostream* os);
  236. } // namespace gfx
  237. #endif // UI_GFX_GEOMETRY_RECT_F_H_