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.h"
13 #include "base/logging.h"
14 #include "base/strings/stringprintf.h"
15 #include "ui/gfx/geometry/insets.h"
20 Rect::Rect(const RECT& r)
21 : origin_(r.left, r.top),
22 size_(std::abs(r.right - r.left), std::abs(r.bottom - r.top)) {
24 #elif defined(OS_MACOSX)
25 Rect::Rect(const CGRect& r)
26 : origin_(r.origin.x, r.origin.y), size_(r.size.width, r.size.height) {
31 RECT Rect::ToRECT() const {
39 #elif defined(OS_MACOSX)
40 CGRect Rect::ToCGRect() const {
41 return CGRectMake(x(), y(), width(), height());
45 void AdjustAlongAxis(int dst_origin, int dst_size, int* origin, int* size) {
46 *size = std::min(dst_size, *size);
47 if (*origin < dst_origin)
50 *origin = std::min(dst_origin + dst_size, *origin + *size) - *size;
57 void Rect::Inset(const Insets& insets) {
58 Inset(insets.left(), insets.top(), insets.right(), insets.bottom());
61 void Rect::Inset(int left, int top, int right, int bottom) {
62 origin_ += Vector2d(left, top);
63 set_width(std::max(width() - left - right, static_cast<int>(0)));
64 set_height(std::max(height() - top - bottom, static_cast<int>(0)));
67 void Rect::Offset(int horizontal, int vertical) {
68 origin_ += Vector2d(horizontal, vertical);
71 void Rect::operator+=(const Vector2d& offset) {
75 void Rect::operator-=(const Vector2d& offset) {
79 Insets Rect::InsetsFrom(const Rect& inner) const {
80 return Insets(inner.y() - y(),
82 bottom() - inner.bottom(),
83 right() - inner.right());
86 bool Rect::operator<(const Rect& other) const {
87 if (origin_ == other.origin_) {
88 if (width() == other.width()) {
89 return height() < other.height();
91 return width() < other.width();
94 return origin_ < other.origin_;
98 bool Rect::Contains(int point_x, int point_y) const {
99 return (point_x >= x()) && (point_x < right()) && (point_y >= y()) &&
100 (point_y < bottom());
103 bool Rect::Contains(const Rect& rect) const {
104 return (rect.x() >= x() && rect.right() <= right() && rect.y() >= y() &&
105 rect.bottom() <= bottom());
108 bool Rect::Intersects(const Rect& rect) const {
109 return !(IsEmpty() || rect.IsEmpty() || rect.x() >= right() ||
110 rect.right() <= x() || rect.y() >= bottom() || rect.bottom() <= y());
113 void Rect::Intersect(const Rect& rect) {
114 if (IsEmpty() || rect.IsEmpty()) {
119 int rx = std::max(x(), rect.x());
120 int ry = std::max(y(), rect.y());
121 int rr = std::min(right(), rect.right());
122 int rb = std::min(bottom(), rect.bottom());
124 if (rx >= rr || ry >= rb)
125 rx = ry = rr = rb = 0; // non-intersecting
127 SetRect(rx, ry, rr - rx, rb - ry);
130 void Rect::Union(const Rect& rect) {
138 int rx = std::min(x(), rect.x());
139 int ry = std::min(y(), rect.y());
140 int rr = std::max(right(), rect.right());
141 int rb = std::max(bottom(), rect.bottom());
143 SetRect(rx, ry, rr - rx, rb - ry);
146 void Rect::Subtract(const Rect& rect) {
147 if (!Intersects(rect))
149 if (rect.Contains(*this)) {
159 if (rect.y() <= y() && rect.bottom() >= bottom()) {
160 // complete intersection in the y-direction
161 if (rect.x() <= x()) {
163 } else if (rect.right() >= right()) {
166 } else if (rect.x() <= x() && rect.right() >= right()) {
167 // complete intersection in the x-direction
168 if (rect.y() <= y()) {
170 } else if (rect.bottom() >= bottom()) {
174 SetRect(rx, ry, rr - rx, rb - ry);
177 void Rect::AdjustToFit(const Rect& rect) {
180 int new_width = width();
181 int new_height = height();
182 AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width);
183 AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height);
184 SetRect(new_x, new_y, new_width, new_height);
187 Point Rect::CenterPoint() const {
188 return Point(x() + width() / 2, y() + height() / 2);
191 void Rect::ClampToCenteredSize(const Size& size) {
192 int new_width = std::min(width(), size.width());
193 int new_height = std::min(height(), size.height());
194 int new_x = x() + (width() - new_width) / 2;
195 int new_y = y() + (height() - new_height) / 2;
196 SetRect(new_x, new_y, new_width, new_height);
199 void Rect::SplitVertically(Rect* left_half, Rect* right_half) const {
203 left_half->SetRect(x(), y(), width() / 2, height());
205 left_half->right(), y(), width() - left_half->width(), height());
208 bool Rect::SharesEdgeWith(const Rect& rect) const {
209 return (y() == rect.y() && height() == rect.height() &&
210 (x() == rect.right() || right() == rect.x())) ||
211 (x() == rect.x() && width() == rect.width() &&
212 (y() == rect.bottom() || bottom() == rect.y()));
215 int Rect::ManhattanDistanceToPoint(const Point& point) const {
217 std::max<int>(0, std::max(x() - point.x(), point.x() - right()));
219 std::max<int>(0, std::max(y() - point.y(), point.y() - bottom()));
221 return x_distance + y_distance;
224 int Rect::ManhattanInternalDistance(const Rect& rect) const {
228 static const int kEpsilon = std::numeric_limits<int>::is_integer
230 : std::numeric_limits<int>::epsilon();
232 int x = std::max<int>(0, c.width() - width() - rect.width() + kEpsilon);
233 int y = std::max<int>(0, c.height() - height() - rect.height() + kEpsilon);
237 std::string Rect::ToString() const {
238 return base::StringPrintf("%s %s",
239 origin().ToString().c_str(),
240 size().ToString().c_str());
243 Rect operator+(const Rect& lhs, const Vector2d& rhs) {
249 Rect operator-(const Rect& lhs, const Vector2d& rhs) {
255 Rect IntersectRects(const Rect& a, const Rect& b) {
261 Rect UnionRects(const Rect& a, const Rect& b) {
267 Rect SubtractRects(const Rect& a, const Rect& b) {
273 Rect BoundingRect(const Point& p1, const Point& p2) {
274 int rx = std::min(p1.x(), p2.x());
275 int ry = std::min(p1.y(), p2.y());
276 int rr = std::max(p1.x(), p2.x());
277 int rb = std::max(p1.y(), p2.y());
278 return Rect(rx, ry, rr - rx, rb - ry);