Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 <algorithm> | |
| 8 #include <limits> | |
| 9 | |
| 10 #include "base/float_util.h" | |
| 11 #include "base/format_macros.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 | |
| 14 namespace gfx { | |
| 15 | |
| 16 RangeF::RangeF() | |
| 17 : start_(0.0f), | |
| 18 end_(0.0f) { | |
| 19 } | |
| 20 | |
| 21 RangeF::RangeF(float start, float end) | |
| 22 : start_(start), | |
| 23 end_(end) { | |
| 24 } | |
| 25 | |
| 26 RangeF::RangeF(float position) | |
| 27 : start_(position), | |
| 28 end_(position) { | |
| 29 } | |
| 30 | |
| 31 // static | |
| 32 const RangeF RangeF::InvalidRange() { | |
| 33 return RangeF(std::numeric_limits<double>::quiet_NaN()); | |
| 34 } | |
| 35 | |
| 36 bool RangeF::IsValid() const { | |
| 37 return !base::IsNaN(start_) && !base::IsNaN(end_); | |
|
Robert Sesek
2015/02/12 16:52:37
Use start() and end() like the rest of the impleme
msw
2015/02/12 21:01:04
Can this just "return *this != InvalidRange();" li
ckocagil
2015/02/13 18:47:47
No. NaN isn't equal to any other value, including
ckocagil
2015/02/13 18:47:47
Done.
| |
| 38 } | |
| 39 | |
| 40 float RangeF::GetMin() const { | |
| 41 return std::min(start(), end()); | |
| 42 } | |
| 43 | |
| 44 float RangeF::GetMax() const { | |
| 45 return std::max(start(), end()); | |
| 46 } | |
| 47 | |
| 48 bool RangeF::operator==(const RangeF& other) const { | |
| 49 return start() == other.start() && end() == other.end(); | |
| 50 } | |
| 51 | |
| 52 bool RangeF::operator!=(const RangeF& other) const { | |
| 53 return !(*this == other); | |
| 54 } | |
| 55 | |
| 56 bool RangeF::EqualsIgnoringDirection(const RangeF& other) const { | |
| 57 return GetMin() == other.GetMin() && GetMax() == other.GetMax(); | |
| 58 } | |
| 59 | |
| 60 bool RangeF::Intersects(const RangeF& range) const { | |
| 61 return IsValid() && range.IsValid() && | |
| 62 !(range.GetMax() < GetMin() || range.GetMin() >= GetMax()); | |
| 63 } | |
| 64 | |
| 65 bool RangeF::Contains(const RangeF& range) const { | |
| 66 return IsValid() && range.IsValid() && | |
| 67 GetMin() <= range.GetMin() && range.GetMax() <= GetMax(); | |
| 68 } | |
| 69 | |
| 70 RangeF RangeF::Intersect(const RangeF& range) const { | |
| 71 float min = std::max(GetMin(), range.GetMin()); | |
| 72 float max = std::min(GetMax(), range.GetMax()); | |
| 73 | |
| 74 if (min >= max) // No intersection. | |
| 75 return InvalidRange(); | |
| 76 | |
| 77 return RangeF(min, max); | |
| 78 } | |
| 79 | |
| 80 std::string RangeF::ToString() const { | |
| 81 return base::StringPrintf("{%f,%f}", start(), end()); | |
| 82 } | |
| 83 | |
| 84 std::ostream& operator<<(std::ostream& os, const RangeF& range) { | |
| 85 return os << range.ToString(); | |
| 86 } | |
| 87 | |
| 88 } // namespace gfx | |
| OLD | NEW |