aboutsummaryrefslogtreecommitdiff
path: root/ui/gfx/range/range_f.cc
blob: b3bfc6956927b8cf0110b9376506b168585162ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/range/range_f.h"

#include <stddef.h>

#include <algorithm>
#include <cmath>

#include "base/format_macros.h"
#include "base/strings/stringprintf.h"

namespace gfx {

RangeF RangeF::Intersect(const RangeF& range) const {
  float min = std::max(GetMin(), range.GetMin());
  float max = std::min(GetMax(), range.GetMax());

  if (min >= max)  // No intersection.
    return InvalidRange();

  return RangeF(min, max);
}

RangeF RangeF::Intersect(const Range& range) const {
  RangeF range_f(range.start(), range.end());
  return Intersect(range_f);
}

Range RangeF::Floor() const {
  uint32_t start = start_ > 0 ? static_cast<uint32_t>(std::floor(start_)) : 0;
  uint32_t end = end_ > 0 ? static_cast<uint32_t>(std::floor(end_)) : 0;
  return Range(start, end);
}

Range RangeF::Ceil() const {
  uint32_t start = start_ > 0 ? static_cast<uint32_t>(std::ceil(start_)) : 0;
  uint32_t end = end_ > 0 ? static_cast<uint32_t>(std::ceil(end_)) : 0;
  return Range(start, end);
}

Range RangeF::Round() const {
  uint32_t start = start_ > 0 ? static_cast<uint32_t>(std::round(start_)) : 0;
  uint32_t end = end_ > 0 ? static_cast<uint32_t>(std::round(end_)) : 0;
  return Range(start, end);
}

std::string RangeF::ToString() const {
  return base::StringPrintf("{%f,%f}", start(), end());
}

std::ostream& operator<<(std::ostream& os, const RangeF& range) {
  return os << range.ToString();
}

}  // namespace gfx