Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / gfx / geometry / point_f.h
blobf900c1ceae727cf408afff98b81e4d30ae3ae5ad
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_F_H_
6 #define UI_GFX_GEOMETRY_POINT_F_H_
8 #include <iosfwd>
9 #include <string>
11 #include "ui/gfx/geometry/vector2d_f.h"
12 #include "ui/gfx/gfx_export.h"
14 namespace gfx {
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() {}
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; }
28 void SetPoint(float x, float y) {
29 x_ = x;
30 y_ = y;
33 void Offset(float delta_x, float delta_y) {
34 x_ += delta_x;
35 y_ += delta_y;
38 void operator+=(const Vector2dF& vector) {
39 x_ += vector.x();
40 y_ += vector.y();
43 void operator-=(const Vector2dF& vector) {
44 x_ -= vector.x();
45 y_ -= vector.y();
48 void SetToMin(const PointF& other);
49 void SetToMax(const PointF& other);
51 bool IsOrigin() const { return x_ == 0 && y_ == 0; }
53 Vector2dF OffsetFromOrigin() const { return Vector2dF(x_, y_); }
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_);
65 void Scale(float scale) {
66 Scale(scale, scale);
69 void Scale(float x_scale, float y_scale) {
70 SetPoint(x() * x_scale, y() * y_scale);
73 // Returns a string representation of point.
74 std::string ToString() const;
76 private:
77 float x_;
78 float y_;
81 inline bool operator==(const PointF& lhs, const PointF& rhs) {
82 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
85 inline bool operator!=(const PointF& lhs, const PointF& rhs) {
86 return !(lhs == rhs);
89 inline PointF operator+(const PointF& lhs, const Vector2dF& rhs) {
90 PointF result(lhs);
91 result += rhs;
92 return result;
95 inline PointF operator-(const PointF& lhs, const Vector2dF& rhs) {
96 PointF result(lhs);
97 result -= rhs;
98 return result;
101 inline Vector2dF operator-(const PointF& lhs, const PointF& rhs) {
102 return Vector2dF(lhs.x() - rhs.x(), lhs.y() - rhs.y());
105 inline PointF PointAtOffsetFromOrigin(const Vector2dF& offset_from_origin) {
106 return PointF(offset_from_origin.x(), offset_from_origin.y());
109 GFX_EXPORT PointF ScalePoint(const PointF& p, float x_scale, float y_scale);
111 inline PointF ScalePoint(const PointF& p, float scale) {
112 return ScalePoint(p, scale, scale);
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);
120 } // namespace gfx
122 #endif // UI_GFX_GEOMETRY_POINT_F_H_