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_conversions.h"
9 #include "base/logging.h"
10 #include "ui/gfx/safe_integer_conversions.h"
14 Rect
ToEnclosingRect(const RectF
& rect
) {
15 int min_x
= ToFlooredInt(rect
.x());
16 int min_y
= ToFlooredInt(rect
.y());
17 float max_x
= rect
.right();
18 float max_y
= rect
.bottom();
19 int width
= rect
.width() == 0 ? 0 : std::max(ToCeiledInt(max_x
) - min_x
, 0);
20 int height
= rect
.height() == 0 ? 0 : std::max(ToCeiledInt(max_y
) - min_y
, 0);
21 return Rect(min_x
, min_y
, width
, height
);
24 Rect
ToEnclosedRect(const RectF
& rect
) {
25 int min_x
= ToCeiledInt(rect
.x());
26 int min_y
= ToCeiledInt(rect
.y());
27 float max_x
= rect
.right();
28 float max_y
= rect
.bottom();
29 int width
= std::max(ToFlooredInt(max_x
) - min_x
, 0);
30 int height
= std::max(ToFlooredInt(max_y
) - min_y
, 0);
31 return Rect(min_x
, min_y
, width
, height
);
34 Rect
ToNearestRect(const RectF
& rect
) {
35 float float_min_x
= rect
.x();
36 float float_min_y
= rect
.y();
37 float float_max_x
= rect
.right();
38 float float_max_y
= rect
.bottom();
40 int min_x
= ToRoundedInt(float_min_x
);
41 int min_y
= ToRoundedInt(float_min_y
);
42 int max_x
= ToRoundedInt(float_max_x
);
43 int max_y
= ToRoundedInt(float_max_y
);
45 // If these DCHECKs fail, you're using the wrong method, consider using
46 // ToEnclosingRect or ToEnclosedRect instead.
47 DCHECK(std::abs(min_x
- float_min_x
) < 0.01f
);
48 DCHECK(std::abs(min_y
- float_min_y
) < 0.01f
);
49 DCHECK(std::abs(max_x
- float_max_x
) < 0.01f
);
50 DCHECK(std::abs(max_y
- float_max_y
) < 0.01f
);
52 return Rect(min_x
, min_y
, max_x
- min_x
, max_y
- min_y
);
55 bool IsNearestRectWithinDistance(const gfx::RectF
& rect
, float distance
) {
56 float float_min_x
= rect
.x();
57 float float_min_y
= rect
.y();
58 float float_max_x
= rect
.right();
59 float float_max_y
= rect
.bottom();
61 int min_x
= ToRoundedInt(float_min_x
);
62 int min_y
= ToRoundedInt(float_min_y
);
63 int max_x
= ToRoundedInt(float_max_x
);
64 int max_y
= ToRoundedInt(float_max_y
);
67 (std::abs(min_x
- float_min_x
) < distance
) &&
68 (std::abs(min_y
- float_min_y
) < distance
) &&
69 (std::abs(max_x
- float_max_x
) < distance
) &&
70 (std::abs(max_y
- float_max_y
) < distance
);
73 Rect
ToFlooredRectDeprecated(const RectF
& rect
) {
74 return Rect(ToFlooredInt(rect
.x()),
75 ToFlooredInt(rect
.y()),
76 ToFlooredInt(rect
.width()),
77 ToFlooredInt(rect
.height()));