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