1 // Copyright 2015 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/range/range_f.h"
6 
7 #include <stddef.h>
8 
9 #include <algorithm>
10 #include <cmath>
11 
12 #include "base/format_macros.h"
13 #include "base/strings/stringprintf.h"
14 
15 namespace gfx {
16 
Intersect(const RangeF & range) const17 RangeF RangeF::Intersect(const RangeF& range) const {
18   float min = std::max(GetMin(), range.GetMin());
19   float max = std::min(GetMax(), range.GetMax());
20 
21   if (min >= max)  // No intersection.
22     return InvalidRange();
23 
24   return RangeF(min, max);
25 }
26 
Intersect(const Range & range) const27 RangeF RangeF::Intersect(const Range& range) const {
28   RangeF range_f(range.start(), range.end());
29   return Intersect(range_f);
30 }
31 
Floor() const32 Range RangeF::Floor() const {
33   uint32_t start = start_ > 0 ? static_cast<uint32_t>(std::floor(start_)) : 0;
34   uint32_t end = end_ > 0 ? static_cast<uint32_t>(std::floor(end_)) : 0;
35   return Range(start, end);
36 }
37 
Ceil() const38 Range RangeF::Ceil() const {
39   uint32_t start = start_ > 0 ? static_cast<uint32_t>(std::ceil(start_)) : 0;
40   uint32_t end = end_ > 0 ? static_cast<uint32_t>(std::ceil(end_)) : 0;
41   return Range(start, end);
42 }
43 
Round() const44 Range RangeF::Round() const {
45   uint32_t start = start_ > 0 ? static_cast<uint32_t>(std::round(start_)) : 0;
46   uint32_t end = end_ > 0 ? static_cast<uint32_t>(std::round(end_)) : 0;
47   return Range(start, end);
48 }
49 
ToString() const50 std::string RangeF::ToString() const {
51   return base::StringPrintf("{%f,%f}", start(), end());
52 }
53 
operator <<(std::ostream & os,const RangeF & range)54 std::ostream& operator<<(std::ostream& os, const RangeF& range) {
55   return os << range.ToString();
56 }
57 
58 }  // namespace gfx
59