Flutter Engine
The Flutter Engine
point.h
Go to the documentation of this file.
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
14#include "gfx/gfx_export.h"
15#include "vector2d.h"
16
17#if defined(OS_WIN)
18typedef unsigned long DWORD;
19typedef struct tagPOINT POINT;
20#elif defined(OS_APPLE)
21typedef struct CGPoint CGPoint;
22#endif
23
24namespace gfx {
25
26// A point has an x and y coordinate.
28 public:
29 constexpr Point() : x_(0), y_(0) {}
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_APPLE)
39 explicit Point(const CGPoint& point);
40#endif
41
42#if defined(OS_WIN)
43 POINT ToPOINT() const;
44#elif defined(OS_APPLE)
45 CGPoint ToCGPoint() const;
46#endif
47
48 constexpr int x() const { return x_; }
49 constexpr int y() const { return y_; }
50 void set_x(int x) { x_ = x; }
51 void set_y(int y) { y_ = y; }
52
53 void SetPoint(int x, int y) {
54 x_ = x;
55 y_ = y;
56 }
57
58 void Offset(int delta_x, int delta_y) {
59 x_ = base::ClampAdd(x_, delta_x);
60 y_ = base::ClampAdd(y_, delta_y);
61 }
62
63 void operator+=(const Vector2d& vector) {
64 x_ = base::ClampAdd(x_, vector.x());
65 y_ = base::ClampAdd(y_, vector.y());
66 }
67
68 void operator-=(const Vector2d& vector) {
69 x_ = base::ClampSub(x_, vector.x());
70 y_ = base::ClampSub(y_, vector.y());
71 }
72
73 void SetToMin(const Point& other);
74 void SetToMax(const Point& other);
75
76 bool IsOrigin() const { return x_ == 0 && y_ == 0; }
77
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 { return std::tie(y_, x_) < std::tie(rhs.y_, rhs.x_); }
87
88 // Returns a string representation of point.
89 std::string ToString() const;
90
91 private:
92 int x_;
93 int y_;
94};
95
96inline bool operator==(const Point& lhs, const Point& rhs) {
97 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
98}
99
100inline bool operator!=(const Point& lhs, const Point& rhs) {
101 return !(lhs == rhs);
102}
103
104inline Point operator+(const Point& lhs, const Vector2d& rhs) {
105 Point result(lhs);
106 result += rhs;
107 return result;
108}
109
110inline Point operator-(const Point& lhs, const Vector2d& rhs) {
111 Point result(lhs);
112 result -= rhs;
113 return result;
114}
115
116inline Vector2d operator-(const Point& lhs, const Point& rhs) {
117 return Vector2d(base::ClampSub(lhs.x(), rhs.x()), base::ClampSub(lhs.y(), rhs.y()));
118}
119
120inline Point PointAtOffsetFromOrigin(const Vector2d& offset_from_origin) {
121 return Point(offset_from_origin.x(), offset_from_origin.y());
122}
123
124// This is declared here for use in gtest-based unit tests but is defined in
125// the //ui/gfx:test_support target. Depend on that to use this in your unit
126// test. This should not be used in production code - call ToString() instead.
127void PrintTo(const Point& point, ::std::ostream* os);
128
129// Helper methods to scale a gfx::Point to a new gfx::Point.
130GFX_EXPORT Point ScaleToCeiledPoint(const Point& point, float x_scale, float y_scale);
131GFX_EXPORT Point ScaleToCeiledPoint(const Point& point, float x_scale);
132GFX_EXPORT Point ScaleToFlooredPoint(const Point& point, float x_scale, float y_scale);
133GFX_EXPORT Point ScaleToFlooredPoint(const Point& point, float x_scale);
134GFX_EXPORT Point ScaleToRoundedPoint(const Point& point, float x_scale, float y_scale);
135GFX_EXPORT Point ScaleToRoundedPoint(const Point& point, float x_scale);
136
137} // namespace gfx
138
139#endif // UI_GFX_GEOMETRY_POINT_H_
void set_y(int y)
Definition: point.h:51
constexpr int y() const
Definition: point.h:49
bool operator<(const Point &rhs) const
Definition: point.h:86
void Offset(int delta_x, int delta_y)
Definition: point.h:58
void operator+=(const Vector2d &vector)
Definition: point.h:63
bool IsOrigin() const
Definition: point.h:76
void SetPoint(int x, int y)
Definition: point.h:53
void operator-=(const Vector2d &vector)
Definition: point.h:68
Vector2d OffsetFromOrigin() const
Definition: point.h:78
constexpr Point()
Definition: point.h:29
void set_x(int x)
Definition: point.h:50
constexpr Point(int x, int y)
Definition: point.h:30
constexpr int x() const
Definition: point.h:48
constexpr int x() const
Definition: vector2d.h:27
constexpr int y() const
Definition: vector2d.h:30
GAsyncResult * result
#define GFX_EXPORT
Definition: gfx_export.h:26
double y
double x
Definition: insets.cc:10
Point ScaleToCeiledPoint(const Point &point, float x_scale, float y_scale)
Definition: point.cc:67
Point PointAtOffsetFromOrigin(const Vector2d &offset_from_origin)
Definition: point.h:120
Insets operator+(Insets lhs, const Insets &rhs)
Definition: insets.h:175
void PrintTo(const Point &point, ::std::ostream *os)
Definition: gfx_util.cc:74
Point ScaleToFlooredPoint(const Point &point, float x_scale, float y_scale)
Definition: point.cc:79
Insets operator-(Insets lhs, const Insets &rhs)
Definition: insets.h:180
bool operator==(const Point &lhs, const Point &rhs)
Definition: point.h:96
Point ScaleToRoundedPoint(const Point &point, float x_scale, float y_scale)
Definition: point.cc:91
bool operator!=(const Point &lhs, const Point &rhs)
Definition: point.h:100
static std::string ToString(CompilerBackend::Type type)
Definition: reflector.cc:559
TPoint< Scalar > Point
Definition: point.h:322
unsigned long DWORD
Definition: windows_types.h:22