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"
10 #include <CoreGraphics/CoreGraphics.h>
11 #elif defined(OS_MACOSX)
12 #include <ApplicationServices/ApplicationServices.h>
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"
22 static void AdjustAlongAxis(float dst_origin
,
26 *size
= std::min(dst_size
, *size
);
27 if (*origin
< dst_origin
)
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());
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
) {
61 void RectF::operator-=(const Vector2dF
& offset
) {
65 InsetsF
RectF::InsetsFrom(const RectF
& inner
) const {
66 return InsetsF(inner
.y() - y(),
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();
77 return width() < other
.width();
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()) &&
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()) {
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
) {
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
))
135 if (rect
.Contains(*static_cast<const RectF
*>(this))) {
145 if (rect
.y() <= y() && rect
.bottom() >= bottom()) {
146 // complete intersection in the y-direction
147 if (rect
.x() <= x()) {
149 } else if (rect
.right() >= right()) {
152 } else if (rect
.x() <= x() && rect
.right() >= right()) {
153 // complete intersection in the x-direction
154 if (rect
.y() <= y()) {
156 } else if (rect
.bottom() >= bottom()) {
160 SetRect(rx
, ry
, rr
- rx
, rb
- ry
);
163 void RectF::AdjustToFit(const RectF
& rect
) {
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 {
189 left_half
->SetRect(x(), y(), width() / 2, height());
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 {
203 std::max
<float>(0, std::max(x() - point
.x(), point
.x() - right()));
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 {
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
);
220 std::max
<float>(0, c
.height() - height() - rect
.height() + kEpsilon
);
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
) {
242 RectF
UnionRects(const RectF
& a
, const RectF
& b
) {
248 RectF
SubtractRects(const RectF
& a
, const RectF
& b
) {
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
);