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_conversions.h"
10 #include "base/logging.h"
11 #include "ui/gfx/geometry/safe_integer_conversions.h"
15 Rect
ToEnclosingRect(const RectF
& rect
) {
16 int min_x
= ToFlooredInt(rect
.x());
17 int min_y
= ToFlooredInt(rect
.y());
18 float max_x
= rect
.right();
19 float max_y
= rect
.bottom();
24 ToCeiledInt(static_cast<float>(ToCeiledInt(max_x
)) - min_x
), 0);
29 ToCeiledInt(static_cast<float>(ToCeiledInt(max_y
)) - min_y
), 0);
30 return Rect(min_x
, min_y
, width
, height
);
33 Rect
ToEnclosedRect(const RectF
& rect
) {
34 int min_x
= ToCeiledInt(rect
.x());
35 int min_y
= ToCeiledInt(rect
.y());
36 float max_x
= rect
.right();
37 float max_y
= rect
.bottom();
39 ToFlooredInt(static_cast<float>(ToFlooredInt(max_x
)) - min_x
), 0);
40 int height
= std::max(
41 ToFlooredInt(static_cast<float>(ToFlooredInt(max_y
)) - min_y
), 0);
42 return Rect(min_x
, min_y
, width
, height
);
45 Rect
ToNearestRect(const RectF
& rect
) {
46 float float_min_x
= rect
.x();
47 float float_min_y
= rect
.y();
48 float float_max_x
= rect
.right();
49 float float_max_y
= rect
.bottom();
51 int min_x
= ToRoundedInt(float_min_x
);
52 int min_y
= ToRoundedInt(float_min_y
);
53 int max_x
= ToRoundedInt(float_max_x
);
54 int max_y
= ToRoundedInt(float_max_y
);
56 // If these DCHECKs fail, you're using the wrong method, consider using
57 // ToEnclosingRect or ToEnclosedRect instead.
58 DCHECK(std::abs(min_x
- float_min_x
) < 0.01f
);
59 DCHECK(std::abs(min_y
- float_min_y
) < 0.01f
);
60 DCHECK(std::abs(max_x
- float_max_x
) < 0.01f
);
61 DCHECK(std::abs(max_y
- float_max_y
) < 0.01f
);
63 return Rect(min_x
, min_y
, max_x
- min_x
, max_y
- min_y
);
66 bool IsNearestRectWithinDistance(const gfx::RectF
& rect
, float distance
) {
67 float float_min_x
= rect
.x();
68 float float_min_y
= rect
.y();
69 float float_max_x
= rect
.right();
70 float float_max_y
= rect
.bottom();
72 int min_x
= ToRoundedInt(float_min_x
);
73 int min_y
= ToRoundedInt(float_min_y
);
74 int max_x
= ToRoundedInt(float_max_x
);
75 int max_y
= ToRoundedInt(float_max_y
);
78 (std::abs(min_x
- float_min_x
) < distance
) &&
79 (std::abs(min_y
- float_min_y
) < distance
) &&
80 (std::abs(max_x
- float_max_x
) < distance
) &&
81 (std::abs(max_y
- float_max_y
) < distance
);
84 Rect
ToFlooredRectDeprecated(const RectF
& rect
) {
85 return Rect(ToFlooredInt(rect
.x()),
86 ToFlooredInt(rect
.y()),
87 ToFlooredInt(rect
.width()),
88 ToFlooredInt(rect
.height()));