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.
5 #include "ui/gfx/range/range_f.h"
10 #include "base/float_util.h"
11 #include "base/format_macros.h"
12 #include "base/strings/stringprintf.h"
21 RangeF::RangeF(float start
, float end
)
26 RangeF::RangeF(float position
)
32 const RangeF
RangeF::InvalidRange() {
33 return RangeF(std::numeric_limits
<float>::max());
36 bool RangeF::IsValid() const {
37 return *this != InvalidRange();
40 float RangeF::GetMin() const {
41 return std::min(start(), end());
44 float RangeF::GetMax() const {
45 return std::max(start(), end());
48 bool RangeF::operator==(const RangeF
& other
) const {
49 return start() == other
.start() && end() == other
.end();
52 bool RangeF::operator!=(const RangeF
& other
) const {
53 return !(*this == other
);
56 bool RangeF::EqualsIgnoringDirection(const RangeF
& other
) const {
57 return GetMin() == other
.GetMin() && GetMax() == other
.GetMax();
60 bool RangeF::Intersects(const RangeF
& range
) const {
61 return IsValid() && range
.IsValid() &&
62 !(range
.GetMax() < GetMin() || range
.GetMin() >= GetMax());
65 bool RangeF::Contains(const RangeF
& range
) const {
66 return IsValid() && range
.IsValid() &&
67 GetMin() <= range
.GetMin() && range
.GetMax() <= GetMax();
70 RangeF
RangeF::Intersect(const RangeF
& range
) const {
71 float min
= std::max(GetMin(), range
.GetMin());
72 float max
= std::min(GetMax(), range
.GetMax());
74 if (min
>= max
) // No intersection.
75 return InvalidRange();
77 return RangeF(min
, max
);
80 std::string
RangeF::ToString() const {
81 return base::StringPrintf("{%f,%f}", start(), end());
84 std::ostream
& operator<<(std::ostream
& os
, const RangeF
& range
) {
85 return os
<< range
.ToString();