Fix search results being clipped in app list.
[chromium-blink-merge.git] / ui / gfx / geometry / rect_f.cc
blobc87ea3de6ac05f9fb700a92a3ff28b874244f3b4
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"
7 #include <algorithm>
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"
14 namespace gfx {
16 static void AdjustAlongAxis(float dst_origin,
17 float dst_size,
18 float* origin,
19 float* size) {
20 *size = std::min(dst_size, *size);
21 if (*origin < dst_origin)
22 *origin = dst_origin;
23 else
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) {
42 origin_ += offset;
45 void RectF::operator-=(const Vector2dF& offset) {
46 origin_ -= offset;
49 InsetsF RectF::InsetsFrom(const RectF& inner) const {
50 return InsetsF(inner.y() - y(),
51 inner.x() - x(),
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();
60 } else {
61 return width() < other.width();
63 } else {
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()) &&
70 (point_y < bottom());
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()) {
85 SetRect(0, 0, 0, 0);
86 return;
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) {
101 if (IsEmpty()) {
102 *this = rect;
103 return;
105 if (rect.IsEmpty())
106 return;
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))
118 return;
119 if (rect.Contains(*static_cast<const RectF*>(this))) {
120 SetRect(0, 0, 0, 0);
121 return;
124 float rx = x();
125 float ry = y();
126 float rr = right();
127 float rb = bottom();
129 if (rect.y() <= y() && rect.bottom() >= bottom()) {
130 // complete intersection in the y-direction
131 if (rect.x() <= x()) {
132 rx = rect.right();
133 } else if (rect.right() >= right()) {
134 rr = rect.x();
136 } else if (rect.x() <= x() && rect.right() >= right()) {
137 // complete intersection in the x-direction
138 if (rect.y() <= y()) {
139 ry = rect.bottom();
140 } else if (rect.bottom() >= bottom()) {
141 rb = rect.y();
144 SetRect(rx, ry, rr - rx, rb - ry);
147 void RectF::AdjustToFit(const RectF& rect) {
148 float new_x = x();
149 float new_y = y();
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 {
170 DCHECK(left_half);
171 DCHECK(right_half);
173 left_half->SetRect(x(), y(), width() / 2, height());
174 right_half->SetRect(
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 {
186 float x_distance =
187 std::max<float>(0, std::max(x() - point.x(), point.x() - right()));
188 float y_distance =
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 {
195 RectF c(*this);
196 c.Union(rect);
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);
203 float y =
204 std::max<float>(0, c.height() - height() - rect.height() + kEpsilon);
205 return x + y;
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) {
221 RectF result = a;
222 result.Intersect(b);
223 return result;
226 RectF UnionRects(const RectF& a, const RectF& b) {
227 RectF result = a;
228 result.Union(b);
229 return result;
232 RectF SubtractRects(const RectF& a, const RectF& b) {
233 RectF result = a;
234 result.Subtract(b);
235 return result;
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);
246 } // namespace gfx