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 #include "ui/gfx/geometry/rect_f.h"
9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h"
11 #include "ui/gfx/geometry/insets_f.h"
12 #include "ui/gfx/geometry/safe_integer_conversions.h"
16 static void AdjustAlongAxis(float dst_origin
,
20 *size
= std::min(dst_size
, *size
);
21 if (*origin
< dst_origin
)
24 *origin
= std::min(dst_origin
+ dst_size
, *origin
+ *size
) - *size
;
27 void RectF::Inset(const InsetsF
& insets
) {
28 Inset(insets
.left(), insets
.top(), insets
.right(), insets
.bottom());
31 void RectF::Inset(float left
, float top
, float right
, float bottom
) {
32 origin_
+= Vector2dF(left
, top
);
33 set_width(std::max(width() - left
- right
, static_cast<float>(0)));
34 set_height(std::max(height() - top
- bottom
, static_cast<float>(0)));
37 void RectF::Offset(float horizontal
, float vertical
) {
38 origin_
+= Vector2dF(horizontal
, vertical
);
41 void RectF::operator+=(const Vector2dF
& offset
) {
45 void RectF::operator-=(const Vector2dF
& offset
) {
49 InsetsF
RectF::InsetsFrom(const RectF
& inner
) const {
50 return InsetsF(inner
.y() - y(),
52 bottom() - inner
.bottom(),
53 right() - inner
.right());
56 bool RectF::operator<(const RectF
& other
) const {
57 if (origin_
== other
.origin_
) {
58 if (width() == other
.width()) {
59 return height() < other
.height();
61 return width() < other
.width();
64 return origin_
< other
.origin_
;
68 bool RectF::Contains(float point_x
, float point_y
) const {
69 return (point_x
>= x()) && (point_x
< right()) && (point_y
>= y()) &&
73 bool RectF::Contains(const RectF
& rect
) const {
74 return (rect
.x() >= x() && rect
.right() <= right() && rect
.y() >= y() &&
75 rect
.bottom() <= bottom());
78 bool RectF::Intersects(const RectF
& rect
) const {
79 return !(IsEmpty() || rect
.IsEmpty() || rect
.x() >= right() ||
80 rect
.right() <= x() || rect
.y() >= bottom() || rect
.bottom() <= y());
83 void RectF::Intersect(const RectF
& rect
) {
84 if (IsEmpty() || rect
.IsEmpty()) {
89 float rx
= std::max(x(), rect
.x());
90 float ry
= std::max(y(), rect
.y());
91 float rr
= std::min(right(), rect
.right());
92 float rb
= std::min(bottom(), rect
.bottom());
94 if (rx
>= rr
|| ry
>= rb
)
95 rx
= ry
= rr
= rb
= 0; // non-intersecting
97 SetRect(rx
, ry
, rr
- rx
, rb
- ry
);
100 void RectF::Union(const RectF
& rect
) {
108 float rx
= std::min(x(), rect
.x());
109 float ry
= std::min(y(), rect
.y());
110 float rr
= std::max(right(), rect
.right());
111 float rb
= std::max(bottom(), rect
.bottom());
113 SetRect(rx
, ry
, rr
- rx
, rb
- ry
);
116 void RectF::Subtract(const RectF
& rect
) {
117 if (!Intersects(rect
))
119 if (rect
.Contains(*static_cast<const RectF
*>(this))) {
129 if (rect
.y() <= y() && rect
.bottom() >= bottom()) {
130 // complete intersection in the y-direction
131 if (rect
.x() <= x()) {
133 } else if (rect
.right() >= right()) {
136 } else if (rect
.x() <= x() && rect
.right() >= right()) {
137 // complete intersection in the x-direction
138 if (rect
.y() <= y()) {
140 } else if (rect
.bottom() >= bottom()) {
144 SetRect(rx
, ry
, rr
- rx
, rb
- ry
);
147 void RectF::AdjustToFit(const RectF
& rect
) {
150 float new_width
= width();
151 float new_height
= height();
152 AdjustAlongAxis(rect
.x(), rect
.width(), &new_x
, &new_width
);
153 AdjustAlongAxis(rect
.y(), rect
.height(), &new_y
, &new_height
);
154 SetRect(new_x
, new_y
, new_width
, new_height
);
157 PointF
RectF::CenterPoint() const {
158 return PointF(x() + width() / 2, y() + height() / 2);
161 void RectF::ClampToCenteredSize(const SizeF
& size
) {
162 float new_width
= std::min(width(), size
.width());
163 float new_height
= std::min(height(), size
.height());
164 float new_x
= x() + (width() - new_width
) / 2;
165 float new_y
= y() + (height() - new_height
) / 2;
166 SetRect(new_x
, new_y
, new_width
, new_height
);
169 void RectF::SplitVertically(RectF
* left_half
, RectF
* right_half
) const {
173 left_half
->SetRect(x(), y(), width() / 2, height());
175 left_half
->right(), y(), width() - left_half
->width(), height());
178 bool RectF::SharesEdgeWith(const RectF
& rect
) const {
179 return (y() == rect
.y() && height() == rect
.height() &&
180 (x() == rect
.right() || right() == rect
.x())) ||
181 (x() == rect
.x() && width() == rect
.width() &&
182 (y() == rect
.bottom() || bottom() == rect
.y()));
185 float RectF::ManhattanDistanceToPoint(const PointF
& point
) const {
187 std::max
<float>(0, std::max(x() - point
.x(), point
.x() - right()));
189 std::max
<float>(0, std::max(y() - point
.y(), point
.y() - bottom()));
191 return x_distance
+ y_distance
;
194 float RectF::ManhattanInternalDistance(const RectF
& rect
) const {
198 static const float kEpsilon
= std::numeric_limits
<float>::is_integer
200 : std::numeric_limits
<float>::epsilon();
202 float x
= std::max
<float>(0, c
.width() - width() - rect
.width() + kEpsilon
);
204 std::max
<float>(0, c
.height() - height() - rect
.height() + kEpsilon
);
208 bool RectF::IsExpressibleAsRect() const {
209 return IsExpressibleAsInt(x()) && IsExpressibleAsInt(y()) &&
210 IsExpressibleAsInt(width()) && IsExpressibleAsInt(height()) &&
211 IsExpressibleAsInt(right()) && IsExpressibleAsInt(bottom());
214 std::string
RectF::ToString() const {
215 return base::StringPrintf("%s %s",
216 origin().ToString().c_str(),
217 size().ToString().c_str());
220 RectF
IntersectRects(const RectF
& a
, const RectF
& b
) {
226 RectF
UnionRects(const RectF
& a
, const RectF
& b
) {
232 RectF
SubtractRects(const RectF
& a
, const RectF
& b
) {
238 RectF
BoundingRect(const PointF
& p1
, const PointF
& p2
) {
239 float rx
= std::min(p1
.x(), p2
.x());
240 float ry
= std::min(p1
.y(), p2
.y());
241 float rr
= std::max(p1
.x(), p2
.x());
242 float rb
= std::max(p1
.y(), p2
.y());
243 return RectF(rx
, ry
, rr
- rx
, rb
- ry
);