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