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
;
19 #include <CoreGraphics/CoreGraphics.h>
20 #elif defined(OS_MACOSX)
21 #include <ApplicationServices/ApplicationServices.h>
26 // A point has an x and y coordinate.
27 class GFX_EXPORT Point
{
29 Point() : x_(0), y_(0) {}
30 Point(int x
, int y
) : x_(x
), y_(y
) {}
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
) : x_(point
.x
), y_(point
.y
) {}
45 POINT
ToPOINT() const;
46 #elif defined(OS_MACOSX)
47 CGPoint
ToCGPoint() const { return CGPointMake(x(), y()); }
50 int x() const { return x_
; }
51 int y() const { return y_
; }
52 void set_x(int x
) { x_
= x
; }
53 void set_y(int y
) { y_
= y
; }
55 void SetPoint(int x
, int y
) {
60 void Offset(int delta_x
, int delta_y
) {
65 void operator+=(const Vector2d
& vector
) {
70 void operator-=(const Vector2d
& vector
) {
75 void SetToMin(const Point
& other
);
76 void SetToMax(const Point
& other
);
78 bool IsOrigin() const { return x_
== 0 && y_
== 0; }
80 Vector2d
OffsetFromOrigin() const { return Vector2d(x_
, y_
); }
82 // A point is less than another point if its y-value is closer
83 // to the origin. If the y-values are the same, then point with
84 // the x-value closer to the origin is considered less than the
86 // This comparison is required to use Point in sets, or sorted
88 bool operator<(const Point
& rhs
) const {
89 return (y_
== rhs
.y_
) ? (x_
< rhs
.x_
) : (y_
< rhs
.y_
);
92 operator PointF() const {
93 return PointF(static_cast<float>(x()), static_cast<float>(y()));
96 // Returns a string representation of point.
97 std::string
ToString() const;
104 inline bool operator==(const Point
& lhs
, const Point
& rhs
) {
105 return lhs
.x() == rhs
.x() && lhs
.y() == rhs
.y();
108 inline bool operator!=(const Point
& lhs
, const Point
& rhs
) {
109 return !(lhs
== rhs
);
112 inline Point
operator+(const Point
& lhs
, const Vector2d
& rhs
) {
118 inline Point
operator-(const Point
& lhs
, const Vector2d
& rhs
) {
124 inline Vector2d
operator-(const Point
& lhs
, const Point
& rhs
) {
125 return Vector2d(lhs
.x() - rhs
.x(), lhs
.y() - rhs
.y());
128 inline Point
PointAtOffsetFromOrigin(const Vector2d
& offset_from_origin
) {
129 return Point(offset_from_origin
.x(), offset_from_origin
.y());
132 // This is declared here for use in gtest-based unit tests but is defined in
133 // the gfx_test_support target. Depend on that to use this in your unit test.
134 // This should not be used in production code - call ToString() instead.
135 void PrintTo(const Point
& point
, ::std::ostream
* os
);
139 #endif // UI_GFX_GEOMETRY_POINT_H_