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 #ifndef UI_GFX_RANGE_RANGE_F_H_
6 #define UI_GFX_RANGE_RANGE_F_H_
11 #include "base/basictypes.h"
12 #include "ui/gfx/gfx_export.h"
16 // A float version of Range. RangeF is made of a start and end position; when
17 // they are the same, the range is empty. Note that |start_| can be greater
18 // than |end_| to respect the directionality of the range.
19 class GFX_EXPORT RangeF
{
21 // Creates an empty range {0,0}.
24 // Initializes the range with a start and end.
25 RangeF(float start
, float end
);
27 // Initializes the range with the same start and end positions.
28 explicit RangeF(float position
);
30 // Returns a range that is invalid, which is {float_max,float_max}.
31 static const RangeF
InvalidRange();
33 // Checks if the range is valid through comparison to InvalidRange().
36 // Getters and setters.
37 float start() const { return start_
; }
38 void set_start(float start
) { start_
= start
; }
40 float end() const { return end_
; }
41 void set_end(float end
) { end_
= end
; }
43 // Returns the absolute value of the length.
44 float length() const {
45 const float length
= end() - start();
46 return length
>= 0 ? length
: -length
;
49 bool is_reversed() const { return start() > end(); }
50 bool is_empty() const { return start() == end(); }
52 // Returns the minimum and maximum values.
56 bool operator==(const RangeF
& other
) const;
57 bool operator!=(const RangeF
& other
) const;
58 bool EqualsIgnoringDirection(const RangeF
& other
) const;
60 // Returns true if this range intersects the specified |range|.
61 bool Intersects(const RangeF
& range
) const;
63 // Returns true if this range contains the specified |range|.
64 bool Contains(const RangeF
& range
) const;
66 // Computes the intersection of this range with the given |range|.
67 // If they don't intersect, it returns an InvalidRange().
68 // The returned range is always empty or forward (never reversed).
69 RangeF
Intersect(const RangeF
& range
) const;
71 std::string
ToString() const;
78 GFX_EXPORT
std::ostream
& operator<<(std::ostream
& os
, const RangeF
& range
);
82 #endif // UI_GFX_RANGE_RANGE_F_H_