Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: ui/gfx/geometry/rect_f.cc

Issue 506803004: gfx: Don't template gfx::Rect and gfx::RectF (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: inlines: rm-rectbase Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/geometry/rect_f.h ('k') | ui/gfx/gfx.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/geometry/rect_f.h" 5 #include "ui/gfx/geometry/rect_f.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "ui/gfx/geometry/insets_f.h" 11 #include "ui/gfx/geometry/insets_f.h"
12 #include "ui/gfx/geometry/rect_base_impl.h"
13 #include "ui/gfx/geometry/safe_integer_conversions.h" 12 #include "ui/gfx/geometry/safe_integer_conversions.h"
14 13
15 namespace gfx { 14 namespace gfx {
16 15
17 template class RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, float>; 16 static void AdjustAlongAxis(float dst_origin,
18 17 float dst_size,
19 typedef class RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, 18 float* origin,
20 float> RectBaseT; 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;
25 }
26
27 void RectF::Inset(const InsetsF& insets) {
28 Inset(insets.left(), insets.top(), insets.right(), insets.bottom());
29 }
30
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)));
35 }
36
37 void RectF::Offset(float horizontal, float vertical) {
38 origin_ += Vector2dF(horizontal, vertical);
39 }
40
41 void RectF::operator+=(const Vector2dF& offset) {
42 origin_ += offset;
43 }
44
45 void RectF::operator-=(const Vector2dF& offset) {
46 origin_ -= offset;
47 }
48
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());
54 }
55
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();
62 }
63 } else {
64 return origin_ < other.origin_;
65 }
66 }
67
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());
71 }
72
73 bool RectF::Contains(const RectF& rect) const {
74 return (rect.x() >= x() && rect.right() <= right() && rect.y() >= y() &&
75 rect.bottom() <= bottom());
76 }
77
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());
81 }
82
83 void RectF::Intersect(const RectF& rect) {
84 if (IsEmpty() || rect.IsEmpty()) {
85 SetRect(0, 0, 0, 0);
86 return;
87 }
88
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());
93
94 if (rx >= rr || ry >= rb)
95 rx = ry = rr = rb = 0; // non-intersecting
96
97 SetRect(rx, ry, rr - rx, rb - ry);
98 }
99
100 void RectF::Union(const RectF& rect) {
101 if (IsEmpty()) {
102 *this = rect;
103 return;
104 }
105 if (rect.IsEmpty())
106 return;
107
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());
112
113 SetRect(rx, ry, rr - rx, rb - ry);
114 }
115
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;
122 }
123
124 float rx = x();
125 float ry = y();
126 float rr = right();
127 float rb = bottom();
128
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();
135 }
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();
142 }
143 }
144 SetRect(rx, ry, rr - rx, rb - ry);
145 }
146
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);
155 }
156
157 PointF RectF::CenterPoint() const {
158 return PointF(x() + width() / 2, y() + height() / 2);
159 }
160
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);
167 }
168
169 void RectF::SplitVertically(RectF* left_half, RectF* right_half) const {
170 DCHECK(left_half);
171 DCHECK(right_half);
172
173 left_half->SetRect(x(), y(), width() / 2, height());
174 right_half->SetRect(
175 left_half->right(), y(), width() - left_half->width(), height());
176 }
177
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()));
183 }
184
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()));
190
191 return x_distance + y_distance;
192 }
193
194 float RectF::ManhattanInternalDistance(const RectF& rect) const {
195 RectF c(*this);
196 c.Union(rect);
197
198 static const float kEpsilon = std::numeric_limits<float>::is_integer
199 ? 1
200 : std::numeric_limits<float>::epsilon();
201
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;
206 }
21 207
22 bool RectF::IsExpressibleAsRect() const { 208 bool RectF::IsExpressibleAsRect() const {
23 return IsExpressibleAsInt(x()) && IsExpressibleAsInt(y()) && 209 return IsExpressibleAsInt(x()) && IsExpressibleAsInt(y()) &&
24 IsExpressibleAsInt(width()) && IsExpressibleAsInt(height()) && 210 IsExpressibleAsInt(width()) && IsExpressibleAsInt(height()) &&
25 IsExpressibleAsInt(right()) && IsExpressibleAsInt(bottom()); 211 IsExpressibleAsInt(right()) && IsExpressibleAsInt(bottom());
26 } 212 }
27 213
28 std::string RectF::ToString() const { 214 std::string RectF::ToString() const {
29 return base::StringPrintf("%s %s", 215 return base::StringPrintf("%s %s",
30 origin().ToString().c_str(), 216 origin().ToString().c_str(),
(...skipping 20 matching lines...) Expand all
51 237
52 RectF BoundingRect(const PointF& p1, const PointF& p2) { 238 RectF BoundingRect(const PointF& p1, const PointF& p2) {
53 float rx = std::min(p1.x(), p2.x()); 239 float rx = std::min(p1.x(), p2.x());
54 float ry = std::min(p1.y(), p2.y()); 240 float ry = std::min(p1.y(), p2.y());
55 float rr = std::max(p1.x(), p2.x()); 241 float rr = std::max(p1.x(), p2.x());
56 float rb = std::max(p1.y(), p2.y()); 242 float rb = std::max(p1.y(), p2.y());
57 return RectF(rx, ry, rr - rx, rb - ry); 243 return RectF(rx, ry, rr - rx, rb - ry);
58 } 244 }
59 245
60 } // namespace gfx 246 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/geometry/rect_f.h ('k') | ui/gfx/gfx.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698