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.
5 #ifndef UI_GFX_GEOMETRY_POINT_H_
6 #define UI_GFX_GEOMETRY_POINT_H_
11 #include "ui/gfx/geometry/point_f.h"
12 #include "ui/gfx/geometry/vector2d.h"
13 #include "ui/gfx/gfx_export.h"
16 typedef unsigned long DWORD
;
17 typedef struct tagPOINT POINT
;
18 #elif defined(OS_MACOSX)
19 typedef struct CGPoint CGPoint
;
24 // A point has an x and y coordinate.
25 class GFX_EXPORT Point
{
27 Point() : x_(0), y_(0) {}
28 Point(int x
, int y
) : x_(x
), y_(y
) {}
30 // |point| is a DWORD value that contains a coordinate. The x-coordinate is
31 // the low-order short and the y-coordinate is the high-order short. This
32 // value is commonly acquired from GetMessagePos/GetCursorPos.
33 explicit Point(DWORD point
);
34 explicit Point(const POINT
& point
);
35 Point
& operator=(const POINT
& point
);
36 #elif defined(OS_MACOSX)
37 explicit Point(const CGPoint
& point
);
43 POINT
ToPOINT() const;
44 #elif defined(OS_MACOSX)
45 CGPoint
ToCGPoint() const;
48 int x() const { return x_
; }
49 int y() const { return y_
; }
50 void set_x(int x
) { x_
= x
; }
51 void set_y(int y
) { y_
= y
; }
53 void SetPoint(int x
, int y
) {
58 void Offset(int delta_x
, int delta_y
) {
63 void operator+=(const Vector2d
& vector
) {
68 void operator-=(const Vector2d
& vector
) {
73 void SetToMin(const Point
& other
);
74 void SetToMax(const Point
& other
);
76 bool IsOrigin() const { return x_
== 0 && y_
== 0; }
78 Vector2d
OffsetFromOrigin() const { return Vector2d(x_
, y_
); }
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
84 // This comparison is required to use Point in sets, or sorted
86 bool operator<(const Point
& rhs
) const {
87 return (y_
== rhs
.y_
) ? (x_
< rhs
.x_
) : (y_
< rhs
.y_
);
90 operator PointF() const {
91 return PointF(static_cast<float>(x()), static_cast<float>(y()));
94 // Returns a string representation of point.
95 std::string
ToString() const;
102 inline bool operator==(const Point
& lhs
, const Point
& rhs
) {
103 return lhs
.x() == rhs
.x() && lhs
.y() == rhs
.y();
106 inline bool operator!=(const Point
& lhs
, const Point
& rhs
) {
107 return !(lhs
== rhs
);
110 inline Point
operator+(const Point
& lhs
, const Vector2d
& rhs
) {
116 inline Point
operator-(const Point
& lhs
, const Vector2d
& rhs
) {
122 inline Vector2d
operator-(const Point
& lhs
, const Point
& rhs
) {
123 return Vector2d(lhs
.x() - rhs
.x(), lhs
.y() - rhs
.y());
126 inline Point
PointAtOffsetFromOrigin(const Vector2d
& offset_from_origin
) {
127 return Point(offset_from_origin
.x(), offset_from_origin
.y());
130 // This is declared here for use in gtest-based unit tests but is defined in
131 // the gfx_test_support target. Depend on that to use this in your unit test.
132 // This should not be used in production code - call ToString() instead.
133 void PrintTo(const Point
& point
, ::std::ostream
* os
);
137 #endif // UI_GFX_GEOMETRY_POINT_H_