• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "ui/gfx/geometry/rect_conversions.h"
6 
7 #include <algorithm>
8 #include <cmath>
9 
10 #include "base/logging.h"
11 #include "ui/gfx/geometry/safe_integer_conversions.h"
12 
13 namespace gfx {
14 
ToEnclosingRect(const RectF & r)15 Rect ToEnclosingRect(const RectF& r) {
16   int left = ToFlooredInt(r.x());
17   int right = r.width() ? ToCeiledInt(r.right()) : left;
18   int top = ToFlooredInt(r.y());
19   int bottom = r.height() ? ToCeiledInt(r.bottom()) : top;
20 
21   Rect result;
22   result.SetByBounds(left, top, right, bottom);
23   return result;
24 }
25 
ToEnclosedRect(const RectF & rect)26 Rect ToEnclosedRect(const RectF& rect) {
27   Rect result;
28   result.SetByBounds(ToCeiledInt(rect.x()), ToCeiledInt(rect.y()),
29                      ToFlooredInt(rect.right()), ToFlooredInt(rect.bottom()));
30   return result;
31 }
32 
ToNearestRect(const RectF & rect)33 Rect ToNearestRect(const RectF& rect) {
34   float float_min_x = rect.x();
35   float float_min_y = rect.y();
36   float float_max_x = rect.right();
37   float float_max_y = rect.bottom();
38 
39   int min_x = ToRoundedInt(float_min_x);
40   int min_y = ToRoundedInt(float_min_y);
41   int max_x = ToRoundedInt(float_max_x);
42   int max_y = ToRoundedInt(float_max_y);
43 
44   // If these DCHECKs fail, you're using the wrong method, consider using
45   // ToEnclosingRect or ToEnclosedRect instead.
46   DCHECK(std::abs(min_x - float_min_x) < 0.01f);
47   DCHECK(std::abs(min_y - float_min_y) < 0.01f);
48   DCHECK(std::abs(max_x - float_max_x) < 0.01f);
49   DCHECK(std::abs(max_y - float_max_y) < 0.01f);
50 
51   Rect result;
52   result.SetByBounds(min_x, min_y, max_x, max_y);
53 
54   return result;
55 }
56 
IsNearestRectWithinDistance(const gfx::RectF & rect,float distance)57 bool IsNearestRectWithinDistance(const gfx::RectF& rect, float distance) {
58   float float_min_x = rect.x();
59   float float_min_y = rect.y();
60   float float_max_x = rect.right();
61   float float_max_y = rect.bottom();
62 
63   int min_x = ToRoundedInt(float_min_x);
64   int min_y = ToRoundedInt(float_min_y);
65   int max_x = ToRoundedInt(float_max_x);
66   int max_y = ToRoundedInt(float_max_y);
67 
68   return
69       (std::abs(min_x - float_min_x) < distance) &&
70       (std::abs(min_y - float_min_y) < distance) &&
71       (std::abs(max_x - float_max_x) < distance) &&
72       (std::abs(max_y - float_max_y) < distance);
73 }
74 
ToFlooredRectDeprecated(const RectF & rect)75 Rect ToFlooredRectDeprecated(const RectF& rect) {
76   return Rect(ToFlooredInt(rect.x()),
77               ToFlooredInt(rect.y()),
78               ToFlooredInt(rect.width()),
79               ToFlooredInt(rect.height()));
80 }
81 
82 }  // namespace gfx
83