1 // Copyright (c) 2012 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 #ifndef UI_GFX_GEOMETRY_POINT_F_H_
6 #define UI_GFX_GEOMETRY_POINT_F_H_
7
8 #include <iosfwd>
9 #include <string>
10 #include <tuple>
11
12 #include "ui/gfx/geometry/point.h"
13 #include "ui/gfx/geometry/vector2d_f.h"
14 #include "ui/gfx/gfx_export.h"
15
16 namespace gfx {
17
18 // A floating version of gfx::Point.
19 class GFX_EXPORT PointF {
20 public:
PointF()21 constexpr PointF() : x_(0.f), y_(0.f) {}
PointF(float x,float y)22 constexpr PointF(float x, float y) : x_(x), y_(y) {}
23
PointF(const Point & p)24 constexpr explicit PointF(const Point& p)
25 : PointF(static_cast<float>(p.x()), static_cast<float>(p.y())) {}
26
x()27 constexpr float x() const { return x_; }
y()28 constexpr float y() const { return y_; }
set_x(float x)29 void set_x(float x) { x_ = x; }
set_y(float y)30 void set_y(float y) { y_ = y; }
31
SetPoint(float x,float y)32 void SetPoint(float x, float y) {
33 x_ = x;
34 y_ = y;
35 }
36
Offset(float delta_x,float delta_y)37 void Offset(float delta_x, float delta_y) {
38 x_ += delta_x;
39 y_ += delta_y;
40 }
41
42 void operator+=(const Vector2dF& vector) {
43 x_ += vector.x();
44 y_ += vector.y();
45 }
46
47 void operator-=(const Vector2dF& vector) {
48 x_ -= vector.x();
49 y_ -= vector.y();
50 }
51
52 void SetToMin(const PointF& other);
53 void SetToMax(const PointF& other);
54
IsOrigin()55 bool IsOrigin() const { return x_ == 0 && y_ == 0; }
56
OffsetFromOrigin()57 Vector2dF OffsetFromOrigin() const { return Vector2dF(x_, y_); }
58
59 // A point is less than another point if its y-value is closer
60 // to the origin. If the y-values are the same, then point with
61 // the x-value closer to the origin is considered less than the
62 // other.
63 // This comparison is required to use PointF in sets, or sorted
64 // vectors.
65 bool operator<(const PointF& rhs) const {
66 return std::tie(y_, x_) < std::tie(rhs.y_, rhs.x_);
67 }
68
Scale(float scale)69 void Scale(float scale) {
70 Scale(scale, scale);
71 }
72
Scale(float x_scale,float y_scale)73 void Scale(float x_scale, float y_scale) {
74 SetPoint(x() * x_scale, y() * y_scale);
75 }
76
77 // Returns a string representation of point.
78 std::string ToString() const;
79
80 private:
81 float x_;
82 float y_;
83 };
84
85 inline bool operator==(const PointF& lhs, const PointF& rhs) {
86 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
87 }
88
89 inline bool operator!=(const PointF& lhs, const PointF& rhs) {
90 return !(lhs == rhs);
91 }
92
93 inline PointF operator+(const PointF& lhs, const Vector2dF& rhs) {
94 PointF result(lhs);
95 result += rhs;
96 return result;
97 }
98
99 inline PointF operator-(const PointF& lhs, const Vector2dF& rhs) {
100 PointF result(lhs);
101 result -= rhs;
102 return result;
103 }
104
105 inline Vector2dF operator-(const PointF& lhs, const PointF& rhs) {
106 return Vector2dF(lhs.x() - rhs.x(), lhs.y() - rhs.y());
107 }
108
PointAtOffsetFromOrigin(const Vector2dF & offset_from_origin)109 inline PointF PointAtOffsetFromOrigin(const Vector2dF& offset_from_origin) {
110 return PointF(offset_from_origin.x(), offset_from_origin.y());
111 }
112
113 GFX_EXPORT PointF ScalePoint(const PointF& p, float x_scale, float y_scale);
114
ScalePoint(const PointF & p,float scale)115 inline PointF ScalePoint(const PointF& p, float scale) {
116 return ScalePoint(p, scale, scale);
117 }
118
119 // This is declared here for use in gtest-based unit tests but is defined in
120 // the //ui/gfx:test_support target. Depend on that to use this in your unit
121 // test. This should not be used in production code - call ToString() instead.
122 void PrintTo(const PointF& point, ::std::ostream* os);
123
124 } // namespace gfx
125
126 #endif // UI_GFX_GEOMETRY_POINT_F_H_
127