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/rect_base.h"
7 #include "base/logging.h"
8 #include "base/strings/stringprintf.h"
10 // This file provides the implementation for RectBaese template and
11 // used to instantiate the base class for Rect and RectF classes.
12 #if !defined(GFX_IMPLEMENTATION)
13 #error "This file is intended for UI implementation only"
18 template<typename Type>
19 void AdjustAlongAxis(Type dst_origin, Type dst_size, Type* origin, Type* size) {
20 *size = std::min(dst_size, *size);
21 if (*origin < dst_origin)
24 *origin = std::min(dst_origin + dst_size, *origin + *size) - *size;
31 template<typename Class,
37 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
38 SetRect(Type x, Type y, Type width, Type height) {
39 origin_.SetPoint(x, y);
44 template<typename Class,
50 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
51 Inset(const InsetsClass& insets) {
52 Inset(insets.left(), insets.top(), insets.right(), insets.bottom());
55 template<typename Class,
61 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
62 Inset(Type left, Type top, Type right, Type bottom) {
63 origin_ += VectorClass(left, top);
64 set_width(std::max(width() - left - right, static_cast<Type>(0)));
65 set_height(std::max(height() - top - bottom, static_cast<Type>(0)));
68 template<typename Class,
74 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
75 Offset(Type horizontal, Type vertical) {
76 origin_ += VectorClass(horizontal, vertical);
79 template<typename Class,
85 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
86 operator+=(const VectorClass& offset) {
90 template<typename Class,
96 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
97 operator-=(const VectorClass& offset) {
101 template<typename Class,
104 typename InsetsClass,
105 typename VectorClass,
107 bool RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
108 operator<(const Class& other) const {
109 if (origin_ == other.origin_) {
110 if (width() == other.width()) {
111 return height() < other.height();
113 return width() < other.width();
116 return origin_ < other.origin_;
120 template<typename Class,
123 typename InsetsClass,
124 typename VectorClass,
126 bool RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
127 Contains(Type point_x, Type point_y) const {
128 return (point_x >= x()) && (point_x < right()) &&
129 (point_y >= y()) && (point_y < bottom());
132 template<typename Class,
135 typename InsetsClass,
136 typename VectorClass,
138 bool RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
139 Contains(const Class& rect) const {
140 return (rect.x() >= x() && rect.right() <= right() &&
141 rect.y() >= y() && rect.bottom() <= bottom());
144 template<typename Class,
147 typename InsetsClass,
148 typename VectorClass,
150 bool RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
151 Intersects(const Class& rect) const {
152 return !(IsEmpty() || rect.IsEmpty() ||
153 rect.x() >= right() || rect.right() <= x() ||
154 rect.y() >= bottom() || rect.bottom() <= y());
157 template<typename Class,
160 typename InsetsClass,
161 typename VectorClass,
163 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
164 Intersect(const Class& rect) {
165 if (IsEmpty() || rect.IsEmpty()) {
170 Type rx = std::max(x(), rect.x());
171 Type ry = std::max(y(), rect.y());
172 Type rr = std::min(right(), rect.right());
173 Type rb = std::min(bottom(), rect.bottom());
175 if (rx >= rr || ry >= rb)
176 rx = ry = rr = rb = 0; // non-intersecting
178 SetRect(rx, ry, rr - rx, rb - ry);
181 template<typename Class,
184 typename InsetsClass,
185 typename VectorClass,
187 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
188 Union(const Class& rect) {
196 Type rx = std::min(x(), rect.x());
197 Type ry = std::min(y(), rect.y());
198 Type rr = std::max(right(), rect.right());
199 Type rb = std::max(bottom(), rect.bottom());
201 SetRect(rx, ry, rr - rx, rb - ry);
204 template<typename Class,
207 typename InsetsClass,
208 typename VectorClass,
210 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
211 Subtract(const Class& rect) {
212 if (!Intersects(rect))
214 if (rect.Contains(*static_cast<const Class*>(this))) {
224 if (rect.y() <= y() && rect.bottom() >= bottom()) {
225 // complete intersection in the y-direction
226 if (rect.x() <= x()) {
228 } else if (rect.right() >= right()) {
231 } else if (rect.x() <= x() && rect.right() >= right()) {
232 // complete intersection in the x-direction
233 if (rect.y() <= y()) {
235 } else if (rect.bottom() >= bottom()) {
239 SetRect(rx, ry, rr - rx, rb - ry);
242 template<typename Class,
245 typename InsetsClass,
246 typename VectorClass,
248 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
249 AdjustToFit(const Class& rect) {
252 Type new_width = width();
253 Type new_height = height();
254 AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width);
255 AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height);
256 SetRect(new_x, new_y, new_width, new_height);
259 template<typename Class,
262 typename InsetsClass,
263 typename VectorClass,
265 PointClass RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass,
266 Type>::CenterPoint() const {
267 return PointClass(x() + width() / 2, y() + height() / 2);
270 template<typename Class,
273 typename InsetsClass,
274 typename VectorClass,
276 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
277 ClampToCenteredSize(const SizeClass& size) {
278 Type new_width = std::min(width(), size.width());
279 Type new_height = std::min(height(), size.height());
280 Type new_x = x() + (width() - new_width) / 2;
281 Type new_y = y() + (height() - new_height) / 2;
282 SetRect(new_x, new_y, new_width, new_height);
285 template<typename Class,
288 typename InsetsClass,
289 typename VectorClass,
291 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
292 SplitVertically(Class* left_half, Class* right_half) const {
296 left_half->SetRect(x(), y(), width() / 2, height());
297 right_half->SetRect(left_half->right(),
299 width() - left_half->width(),
303 template<typename Class,
306 typename InsetsClass,
307 typename VectorClass,
309 bool RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
310 SharesEdgeWith(const Class& rect) const {
311 return (y() == rect.y() && height() == rect.height() &&
312 (x() == rect.right() || right() == rect.x())) ||
313 (x() == rect.x() && width() == rect.width() &&
314 (y() == rect.bottom() || bottom() == rect.y()));
317 template<typename Class,
320 typename InsetsClass,
321 typename VectorClass,
323 Type RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
324 ManhattanDistanceToPoint(const PointClass& point) const {
325 Type x_distance = std::max<Type>(0, std::max(
326 x() - point.x(), point.x() - right()));
327 Type y_distance = std::max<Type>(0, std::max(
328 y() - point.y(), point.y() - bottom()));
330 return x_distance + y_distance;