summaryrefslogtreecommitdiff
path: root/chromium/ui/gfx/range/range_f.h
blob: 0e4185a52d2dc9503ece30a57de7b0385ae11c81 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 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.

#ifndef UI_GFX_RANGE_RANGE_F_H_
#define UI_GFX_RANGE_RANGE_F_H_

#include <ostream>
#include <string>

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

namespace gfx {

// A float version of Range. RangeF is made of a start and end position; when
// they are the same, the range is empty. Note that |start_| can be greater
// than |end_| to respect the directionality of the range.
class GFX_EXPORT RangeF {
 public:
  // Creates an empty range {0,0}.
  RangeF();

  // Initializes the range with a start and end.
  RangeF(float start, float end);

  // Initializes the range with the same start and end positions.
  explicit RangeF(float position);

  // Returns a range that is invalid, which is {float_max,float_max}.
  static const RangeF InvalidRange();

  // Checks if the range is valid through comparison to InvalidRange().
  bool IsValid() const;

  // Getters and setters.
  float start() const { return start_; }
  void set_start(float start) { start_ = start; }

  float end() const { return end_; }
  void set_end(float end) { end_ = end; }

  // Returns the absolute value of the length.
  float length() const {
    const float length = end() - start();
    return length >= 0 ? length : -length;
  }

  bool is_reversed() const { return start() > end(); }
  bool is_empty() const { return start() == end(); }

  // Returns the minimum and maximum values.
  float GetMin() const;
  float GetMax() const;

  bool operator==(const RangeF& other) const;
  bool operator!=(const RangeF& other) const;
  bool EqualsIgnoringDirection(const RangeF& other) const;

  // Returns true if this range intersects the specified |range|.
  bool Intersects(const RangeF& range) const;

  // Returns true if this range contains the specified |range|.
  bool Contains(const RangeF& range) const;

  // Computes the intersection of this range with the given |range|.
  // If they don't intersect, it returns an InvalidRange().
  // The returned range is always empty or forward (never reversed).
  RangeF Intersect(const RangeF& range) const;
  RangeF Intersect(const Range& range) const;

  // Floor/Ceil/Round the start and end values of the given RangeF.
  Range Floor() const;
  Range Ceil() const;
  Range Round() const;

  std::string ToString() const;

 private:
  float start_;
  float end_;
};

GFX_EXPORT std::ostream& operator<<(std::ostream& os, const RangeF& range);

}  // namespace gfx

#endif  // UI_GFX_RANGE_RANGE_F_H_