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_
11 #include "ui/gfx/geometry/vector2d_f.h"
12 #include "ui/gfx/gfx_export.h"
16 // A floating version of gfx::Point.
17 class GFX_EXPORT PointF {
19 PointF() : x_(0.f), y_(0.f) {}
20 PointF(float x, float y) : x_(x), y_(y) {}
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) {
33 void Offset(float delta_x, float delta_y) {
38 void operator+=(const Vector2dF& vector) {
43 void operator-=(const Vector2dF& vector) {
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
59 // This comparison is required to use PointF in sets, or sorted
61 bool operator<(const PointF& rhs) const {
62 return (y_ == rhs.y_) ? (x_ < rhs.x_) : (y_ < rhs.y_);
65 void Scale(float 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;
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) {
89 inline PointF operator+(const PointF& lhs, const Vector2dF& rhs) {
95 inline PointF operator-(const PointF& lhs, const Vector2dF& rhs) {
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);
122 #endif // UI_GFX_GEOMETRY_POINT_F_H_