Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / geometry / rect_f.cc
bloba15bdf36a7808aedf5f5fa5924bfacc962522db4
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 #if defined(OS_IOS)
10 #include <CoreGraphics/CoreGraphics.h>
11 #elif defined(OS_MACOSX)
12 #include <ApplicationServices/ApplicationServices.h>
13 #endif
15 #include "base/logging.h"
16 #include "base/strings/stringprintf.h"
17 #include "ui/gfx/geometry/insets_f.h"
18 #include "ui/gfx/geometry/safe_integer_conversions.h"
20 namespace gfx {
22 static void AdjustAlongAxis(float dst_origin,
23 float dst_size,
24 float* origin,
25 float* size) {
26 *size = std::min(dst_size, *size);
27 if (*origin < dst_origin)
28 *origin = dst_origin;
29 else
30 *origin = std::min(dst_origin + dst_size, *origin + *size) - *size;
33 #if defined(OS_MACOSX)
34 RectF::RectF(const CGRect& r)
35 : origin_(r.origin.x, r.origin.y), size_(r.size.width, r.size.height) {
38 CGRect RectF::ToCGRect() const {
39 return CGRectMake(x(), y(), width(), height());
41 #endif
43 void RectF::Inset(const InsetsF& insets) {
44 Inset(insets.left(), insets.top(), insets.right(), insets.bottom());
47 void RectF::Inset(float left, float top, float right, float bottom) {
48 origin_ += Vector2dF(left, top);
49 set_width(std::max(width() - left - right, static_cast<float>(0)));
50 set_height(std::max(height() - top - bottom, static_cast<float>(0)));
53 void RectF::Offset(float horizontal, float vertical) {
54 origin_ += Vector2dF(horizontal, vertical);
57 void RectF::operator+=(const Vector2dF& offset) {
58 origin_ += offset;
61 void RectF::operator-=(const Vector2dF& offset) {
62 origin_ -= offset;
65 InsetsF RectF::InsetsFrom(const RectF& inner) const {
66 return InsetsF(inner.y() - y(),
67 inner.x() - x(),
68 bottom() - inner.bottom(),
69 right() - inner.right());
72 bool RectF::operator<(const RectF& other) const {
73 if (origin_ == other.origin_) {
74 if (width() == other.width()) {
75 return height() < other.height();
76 } else {
77 return width() < other.width();
79 } else {
80 return origin_ < other.origin_;
84 bool RectF::Contains(float point_x, float point_y) const {
85 return (point_x >= x()) && (point_x < right()) && (point_y >= y()) &&
86 (point_y < bottom());
89 bool RectF::Contains(const RectF& rect) const {
90 return (rect.x() >= x() && rect.right() <= right() && rect.y() >= y() &&
91 rect.bottom() <= bottom());
94 bool RectF::Intersects(const RectF& rect) const {
95 return !(IsEmpty() || rect.IsEmpty() || rect.x() >= right() ||
96 rect.right() <= x() || rect.y() >= bottom() || rect.bottom() <= y());
99 void RectF::Intersect(const RectF& rect) {
100 if (IsEmpty() || rect.IsEmpty()) {
101 SetRect(0, 0, 0, 0);
102 return;
105 float rx = std::max(x(), rect.x());
106 float ry = std::max(y(), rect.y());
107 float rr = std::min(right(), rect.right());
108 float rb = std::min(bottom(), rect.bottom());
110 if (rx >= rr || ry >= rb)
111 rx = ry = rr = rb = 0; // non-intersecting
113 SetRect(rx, ry, rr - rx, rb - ry);
116 void RectF::Union(const RectF& rect) {
117 if (IsEmpty()) {
118 *this = rect;
119 return;
121 if (rect.IsEmpty())
122 return;
124 float rx = std::min(x(), rect.x());
125 float ry = std::min(y(), rect.y());
126 float rr = std::max(right(), rect.right());
127 float rb = std::max(bottom(), rect.bottom());
129 SetRect(rx, ry, rr - rx, rb - ry);
132 void RectF::Subtract(const RectF& rect) {
133 if (!Intersects(rect))
134 return;
135 if (rect.Contains(*static_cast<const RectF*>(this))) {
136 SetRect(0, 0, 0, 0);
137 return;
140 float rx = x();
141 float ry = y();
142 float rr = right();
143 float rb = bottom();
145 if (rect.y() <= y() && rect.bottom() >= bottom()) {
146 // complete intersection in the y-direction
147 if (rect.x() <= x()) {
148 rx = rect.right();
149 } else if (rect.right() >= right()) {
150 rr = rect.x();
152 } else if (rect.x() <= x() && rect.right() >= right()) {
153 // complete intersection in the x-direction
154 if (rect.y() <= y()) {
155 ry = rect.bottom();
156 } else if (rect.bottom() >= bottom()) {
157 rb = rect.y();
160 SetRect(rx, ry, rr - rx, rb - ry);
163 void RectF::AdjustToFit(const RectF& rect) {
164 float new_x = x();
165 float new_y = y();
166 float new_width = width();
167 float new_height = height();
168 AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width);
169 AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height);
170 SetRect(new_x, new_y, new_width, new_height);
173 PointF RectF::CenterPoint() const {
174 return PointF(x() + width() / 2, y() + height() / 2);
177 void RectF::ClampToCenteredSize(const SizeF& size) {
178 float new_width = std::min(width(), size.width());
179 float new_height = std::min(height(), size.height());
180 float new_x = x() + (width() - new_width) / 2;
181 float new_y = y() + (height() - new_height) / 2;
182 SetRect(new_x, new_y, new_width, new_height);
185 void RectF::SplitVertically(RectF* left_half, RectF* right_half) const {
186 DCHECK(left_half);
187 DCHECK(right_half);
189 left_half->SetRect(x(), y(), width() / 2, height());
190 right_half->SetRect(
191 left_half->right(), y(), width() - left_half->width(), height());
194 bool RectF::SharesEdgeWith(const RectF& rect) const {
195 return (y() == rect.y() && height() == rect.height() &&
196 (x() == rect.right() || right() == rect.x())) ||
197 (x() == rect.x() && width() == rect.width() &&
198 (y() == rect.bottom() || bottom() == rect.y()));
201 float RectF::ManhattanDistanceToPoint(const PointF& point) const {
202 float x_distance =
203 std::max<float>(0, std::max(x() - point.x(), point.x() - right()));
204 float y_distance =
205 std::max<float>(0, std::max(y() - point.y(), point.y() - bottom()));
207 return x_distance + y_distance;
210 float RectF::ManhattanInternalDistance(const RectF& rect) const {
211 RectF c(*this);
212 c.Union(rect);
214 static const float kEpsilon = std::numeric_limits<float>::is_integer
216 : std::numeric_limits<float>::epsilon();
218 float x = std::max<float>(0, c.width() - width() - rect.width() + kEpsilon);
219 float y =
220 std::max<float>(0, c.height() - height() - rect.height() + kEpsilon);
221 return x + y;
224 bool RectF::IsExpressibleAsRect() const {
225 return IsExpressibleAsInt(x()) && IsExpressibleAsInt(y()) &&
226 IsExpressibleAsInt(width()) && IsExpressibleAsInt(height()) &&
227 IsExpressibleAsInt(right()) && IsExpressibleAsInt(bottom());
230 std::string RectF::ToString() const {
231 return base::StringPrintf("%s %s",
232 origin().ToString().c_str(),
233 size().ToString().c_str());
236 RectF IntersectRects(const RectF& a, const RectF& b) {
237 RectF result = a;
238 result.Intersect(b);
239 return result;
242 RectF UnionRects(const RectF& a, const RectF& b) {
243 RectF result = a;
244 result.Union(b);
245 return result;
248 RectF SubtractRects(const RectF& a, const RectF& b) {
249 RectF result = a;
250 result.Subtract(b);
251 return result;
254 RectF BoundingRect(const PointF& p1, const PointF& p2) {
255 float rx = std::min(p1.x(), p2.x());
256 float ry = std::min(p1.y(), p2.y());
257 float rr = std::max(p1.x(), p2.x());
258 float rb = std::max(p1.y(), p2.y());
259 return RectF(rx, ry, rr - rx, rb - ry);
262 } // namespace gfx