Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / geometry / point_f.h
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_F_H_
6 #define UI_GFX_GEOMETRY_POINT_F_H_
7
8 #include <iosfwd>
9 #include <string>
10
11 #include "ui/gfx/geometry/vector2d_f.h"
12 #include "ui/gfx/gfx_export.h"
13
14 namespace gfx {
15
16 // A floating version of gfx::Point.
17 class GFX_EXPORT PointF {
18  public:
19   PointF() : x_(0.f), y_(0.f) {}
20   PointF(float x, float y) : x_(x), y_(y) {}
21   ~PointF() {}
22
23   float x() const { return x_; }
24   float y() const { return y_; }
25   void set_x(float x) { x_ = x; }
26   void set_y(float y) { y_ = y; }
27
28   void SetPoint(float x, float y) {
29     x_ = x;
30     y_ = y;
31   }
32
33   void Offset(float delta_x, float delta_y) {
34     x_ += delta_x;
35     y_ += delta_y;
36   }
37
38   void operator+=(const Vector2dF& vector) {
39     x_ += vector.x();
40     y_ += vector.y();
41   }
42
43   void operator-=(const Vector2dF& vector) {
44     x_ -= vector.x();
45     y_ -= vector.y();
46   }
47
48   void SetToMin(const PointF& other);
49   void SetToMax(const PointF& other);
50
51   bool IsOrigin() const { return x_ == 0 && y_ == 0; }
52
53   Vector2dF OffsetFromOrigin() const { return Vector2dF(x_, y_); }
54
55   // A point is less than another point if its y-value is closer
56   // to the origin. If the y-values are the same, then point with
57   // the x-value closer to the origin is considered less than the
58   // other.
59   // This comparison is required to use PointF in sets, or sorted
60   // vectors.
61   bool operator<(const PointF& rhs) const {
62     return (y_ == rhs.y_) ? (x_ < rhs.x_) : (y_ < rhs.y_);
63   }
64
65   void Scale(float scale) {
66     Scale(scale, scale);
67   }
68
69   void Scale(float x_scale, float y_scale) {
70     SetPoint(x() * x_scale, y() * y_scale);
71   }
72
73   // Returns a string representation of point.
74   std::string ToString() const;
75
76  private:
77   float x_;
78   float y_;
79 };
80
81 inline bool operator==(const PointF& lhs, const PointF& rhs) {
82   return lhs.x() == rhs.x() && lhs.y() == rhs.y();
83 }
84
85 inline bool operator!=(const PointF& lhs, const PointF& rhs) {
86   return !(lhs == rhs);
87 }
88
89 inline PointF operator+(const PointF& lhs, const Vector2dF& rhs) {
90   PointF result(lhs);
91   result += rhs;
92   return result;
93 }
94
95 inline PointF operator-(const PointF& lhs, const Vector2dF& rhs) {
96   PointF result(lhs);
97   result -= rhs;
98   return result;
99 }
100
101 inline Vector2dF operator-(const PointF& lhs, const PointF& rhs) {
102   return Vector2dF(lhs.x() - rhs.x(), lhs.y() - rhs.y());
103 }
104
105 inline PointF PointAtOffsetFromOrigin(const Vector2dF& offset_from_origin) {
106   return PointF(offset_from_origin.x(), offset_from_origin.y());
107 }
108
109 GFX_EXPORT PointF ScalePoint(const PointF& p, float x_scale, float y_scale);
110
111 inline PointF ScalePoint(const PointF& p, float scale) {
112   return ScalePoint(p, scale, scale);
113 }
114
115 // This is declared here for use in gtest-based unit tests but is defined in
116 // the gfx_test_support target. Depend on that to use this in your unit test.
117 // This should not be used in production code - call ToString() instead.
118 void PrintTo(const PointF& point, ::std::ostream* os);
119
120 }  // namespace gfx
121
122 #endif  // UI_GFX_GEOMETRY_POINT_F_H_