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_H_
6*635a8641SAndroid Build Coastguard Worker #define UI_GFX_GEOMETRY_POINT_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 "base/numerics/clamped_math.h"
13*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
14*635a8641SAndroid Build Coastguard Worker #include "ui/gfx/geometry/vector2d.h"
15*635a8641SAndroid Build Coastguard Worker #include "ui/gfx/gfx_export.h"
16*635a8641SAndroid Build Coastguard Worker
17*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
18*635a8641SAndroid Build Coastguard Worker typedef unsigned long DWORD;
19*635a8641SAndroid Build Coastguard Worker typedef struct tagPOINT POINT;
20*635a8641SAndroid Build Coastguard Worker #elif defined(OS_MACOSX)
21*635a8641SAndroid Build Coastguard Worker typedef struct CGPoint CGPoint;
22*635a8641SAndroid Build Coastguard Worker #endif
23*635a8641SAndroid Build Coastguard Worker
24*635a8641SAndroid Build Coastguard Worker namespace gfx {
25*635a8641SAndroid Build Coastguard Worker
26*635a8641SAndroid Build Coastguard Worker // A point has an x and y coordinate.
27*635a8641SAndroid Build Coastguard Worker class GFX_EXPORT Point {
28*635a8641SAndroid Build Coastguard Worker public:
Point()29*635a8641SAndroid Build Coastguard Worker constexpr Point() : x_(0), y_(0) {}
Point(int x,int y)30*635a8641SAndroid Build Coastguard Worker constexpr Point(int x, int y) : x_(x), y_(y) {}
31*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
32*635a8641SAndroid Build Coastguard Worker // |point| is a DWORD value that contains a coordinate. The x-coordinate is
33*635a8641SAndroid Build Coastguard Worker // the low-order short and the y-coordinate is the high-order short. This
34*635a8641SAndroid Build Coastguard Worker // value is commonly acquired from GetMessagePos/GetCursorPos.
35*635a8641SAndroid Build Coastguard Worker explicit Point(DWORD point);
36*635a8641SAndroid Build Coastguard Worker explicit Point(const POINT& point);
37*635a8641SAndroid Build Coastguard Worker Point& operator=(const POINT& point);
38*635a8641SAndroid Build Coastguard Worker #elif defined(OS_MACOSX)
39*635a8641SAndroid Build Coastguard Worker explicit Point(const CGPoint& point);
40*635a8641SAndroid Build Coastguard Worker #endif
41*635a8641SAndroid Build Coastguard Worker
42*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
43*635a8641SAndroid Build Coastguard Worker POINT ToPOINT() const;
44*635a8641SAndroid Build Coastguard Worker #elif defined(OS_MACOSX)
45*635a8641SAndroid Build Coastguard Worker CGPoint ToCGPoint() const;
46*635a8641SAndroid Build Coastguard Worker #endif
47*635a8641SAndroid Build Coastguard Worker
x()48*635a8641SAndroid Build Coastguard Worker constexpr int x() const { return x_; }
y()49*635a8641SAndroid Build Coastguard Worker constexpr int y() const { return y_; }
set_x(int x)50*635a8641SAndroid Build Coastguard Worker void set_x(int x) { x_ = x; }
set_y(int y)51*635a8641SAndroid Build Coastguard Worker void set_y(int y) { y_ = y; }
52*635a8641SAndroid Build Coastguard Worker
SetPoint(int x,int y)53*635a8641SAndroid Build Coastguard Worker void SetPoint(int x, int y) {
54*635a8641SAndroid Build Coastguard Worker x_ = x;
55*635a8641SAndroid Build Coastguard Worker y_ = y;
56*635a8641SAndroid Build Coastguard Worker }
57*635a8641SAndroid Build Coastguard Worker
Offset(int delta_x,int delta_y)58*635a8641SAndroid Build Coastguard Worker void Offset(int delta_x, int delta_y) {
59*635a8641SAndroid Build Coastguard Worker x_ = base::ClampAdd(x_, delta_x);
60*635a8641SAndroid Build Coastguard Worker y_ = base::ClampAdd(y_, delta_y);
61*635a8641SAndroid Build Coastguard Worker }
62*635a8641SAndroid Build Coastguard Worker
63*635a8641SAndroid Build Coastguard Worker void operator+=(const Vector2d& vector) {
64*635a8641SAndroid Build Coastguard Worker x_ = base::ClampAdd(x_, vector.x());
65*635a8641SAndroid Build Coastguard Worker y_ = base::ClampAdd(y_, vector.y());
66*635a8641SAndroid Build Coastguard Worker }
67*635a8641SAndroid Build Coastguard Worker
68*635a8641SAndroid Build Coastguard Worker void operator-=(const Vector2d& vector) {
69*635a8641SAndroid Build Coastguard Worker x_ = base::ClampSub(x_, vector.x());
70*635a8641SAndroid Build Coastguard Worker y_ = base::ClampSub(y_, vector.y());
71*635a8641SAndroid Build Coastguard Worker }
72*635a8641SAndroid Build Coastguard Worker
73*635a8641SAndroid Build Coastguard Worker void SetToMin(const Point& other);
74*635a8641SAndroid Build Coastguard Worker void SetToMax(const Point& other);
75*635a8641SAndroid Build Coastguard Worker
IsOrigin()76*635a8641SAndroid Build Coastguard Worker bool IsOrigin() const { return x_ == 0 && y_ == 0; }
77*635a8641SAndroid Build Coastguard Worker
OffsetFromOrigin()78*635a8641SAndroid Build Coastguard Worker Vector2d OffsetFromOrigin() const { return Vector2d(x_, y_); }
79*635a8641SAndroid Build Coastguard Worker
80*635a8641SAndroid Build Coastguard Worker // A point is less than another point if its y-value is closer
81*635a8641SAndroid Build Coastguard Worker // to the origin. If the y-values are the same, then point with
82*635a8641SAndroid Build Coastguard Worker // the x-value closer to the origin is considered less than the
83*635a8641SAndroid Build Coastguard Worker // other.
84*635a8641SAndroid Build Coastguard Worker // This comparison is required to use Point in sets, or sorted
85*635a8641SAndroid Build Coastguard Worker // vectors.
86*635a8641SAndroid Build Coastguard Worker bool operator<(const Point& rhs) const {
87*635a8641SAndroid Build Coastguard Worker return std::tie(y_, x_) < std::tie(rhs.y_, rhs.x_);
88*635a8641SAndroid Build Coastguard Worker }
89*635a8641SAndroid Build Coastguard Worker
90*635a8641SAndroid Build Coastguard Worker // Returns a string representation of point.
91*635a8641SAndroid Build Coastguard Worker std::string ToString() const;
92*635a8641SAndroid Build Coastguard Worker
93*635a8641SAndroid Build Coastguard Worker private:
94*635a8641SAndroid Build Coastguard Worker int x_;
95*635a8641SAndroid Build Coastguard Worker int y_;
96*635a8641SAndroid Build Coastguard Worker };
97*635a8641SAndroid Build Coastguard Worker
98*635a8641SAndroid Build Coastguard Worker inline bool operator==(const Point& lhs, const Point& rhs) {
99*635a8641SAndroid Build Coastguard Worker return lhs.x() == rhs.x() && lhs.y() == rhs.y();
100*635a8641SAndroid Build Coastguard Worker }
101*635a8641SAndroid Build Coastguard Worker
102*635a8641SAndroid Build Coastguard Worker inline bool operator!=(const Point& lhs, const Point& rhs) {
103*635a8641SAndroid Build Coastguard Worker return !(lhs == rhs);
104*635a8641SAndroid Build Coastguard Worker }
105*635a8641SAndroid Build Coastguard Worker
106*635a8641SAndroid Build Coastguard Worker inline Point operator+(const Point& lhs, const Vector2d& rhs) {
107*635a8641SAndroid Build Coastguard Worker Point result(lhs);
108*635a8641SAndroid Build Coastguard Worker result += rhs;
109*635a8641SAndroid Build Coastguard Worker return result;
110*635a8641SAndroid Build Coastguard Worker }
111*635a8641SAndroid Build Coastguard Worker
112*635a8641SAndroid Build Coastguard Worker inline Point operator-(const Point& lhs, const Vector2d& rhs) {
113*635a8641SAndroid Build Coastguard Worker Point result(lhs);
114*635a8641SAndroid Build Coastguard Worker result -= rhs;
115*635a8641SAndroid Build Coastguard Worker return result;
116*635a8641SAndroid Build Coastguard Worker }
117*635a8641SAndroid Build Coastguard Worker
118*635a8641SAndroid Build Coastguard Worker inline Vector2d operator-(const Point& lhs, const Point& rhs) {
119*635a8641SAndroid Build Coastguard Worker return Vector2d(base::ClampSub(lhs.x(), rhs.x()),
120*635a8641SAndroid Build Coastguard Worker base::ClampSub(lhs.y(), rhs.y()));
121*635a8641SAndroid Build Coastguard Worker }
122*635a8641SAndroid Build Coastguard Worker
PointAtOffsetFromOrigin(const Vector2d & offset_from_origin)123*635a8641SAndroid Build Coastguard Worker inline Point PointAtOffsetFromOrigin(const Vector2d& offset_from_origin) {
124*635a8641SAndroid Build Coastguard Worker return Point(offset_from_origin.x(), offset_from_origin.y());
125*635a8641SAndroid Build Coastguard Worker }
126*635a8641SAndroid Build Coastguard Worker
127*635a8641SAndroid Build Coastguard Worker // This is declared here for use in gtest-based unit tests but is defined in
128*635a8641SAndroid Build Coastguard Worker // the //ui/gfx:test_support target. Depend on that to use this in your unit
129*635a8641SAndroid Build Coastguard Worker // test. This should not be used in production code - call ToString() instead.
130*635a8641SAndroid Build Coastguard Worker void PrintTo(const Point& point, ::std::ostream* os);
131*635a8641SAndroid Build Coastguard Worker
132*635a8641SAndroid Build Coastguard Worker // Helper methods to scale a gfx::Point to a new gfx::Point.
133*635a8641SAndroid Build Coastguard Worker GFX_EXPORT Point ScaleToCeiledPoint(const Point& point,
134*635a8641SAndroid Build Coastguard Worker float x_scale,
135*635a8641SAndroid Build Coastguard Worker float y_scale);
136*635a8641SAndroid Build Coastguard Worker GFX_EXPORT Point ScaleToCeiledPoint(const Point& point, float x_scale);
137*635a8641SAndroid Build Coastguard Worker GFX_EXPORT Point ScaleToFlooredPoint(const Point& point,
138*635a8641SAndroid Build Coastguard Worker float x_scale,
139*635a8641SAndroid Build Coastguard Worker float y_scale);
140*635a8641SAndroid Build Coastguard Worker GFX_EXPORT Point ScaleToFlooredPoint(const Point& point, float x_scale);
141*635a8641SAndroid Build Coastguard Worker GFX_EXPORT Point ScaleToRoundedPoint(const Point& point,
142*635a8641SAndroid Build Coastguard Worker float x_scale,
143*635a8641SAndroid Build Coastguard Worker float y_scale);
144*635a8641SAndroid Build Coastguard Worker GFX_EXPORT Point ScaleToRoundedPoint(const Point& point, float x_scale);
145*635a8641SAndroid Build Coastguard Worker
146*635a8641SAndroid Build Coastguard Worker } // namespace gfx
147*635a8641SAndroid Build Coastguard Worker
148*635a8641SAndroid Build Coastguard Worker #endif // UI_GFX_GEOMETRY_POINT_H_
149