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