OLD | NEW |
(Empty) | |
| 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 // Defines a simple float vector class. This class is used to indicate a |
| 6 // distance in two dimensions between two points. Subtracting two points should |
| 7 // produce a vector, and adding a vector to a point produces the point at the |
| 8 // vector's distance from the original point. |
| 9 |
| 10 #ifndef UI_GFX_VECTOR2D_F_H_ |
| 11 #define UI_GFX_VECTOR2D_F_H_ |
| 12 |
| 13 #include <string> |
| 14 |
| 15 #include "ui/base/ui_export.h" |
| 16 |
| 17 namespace gfx { |
| 18 |
| 19 class UI_EXPORT Vector2dF { |
| 20 public: |
| 21 Vector2dF(); |
| 22 Vector2dF(float x, float y); |
| 23 |
| 24 float x() const { return x_; } |
| 25 void set_x(float x) { x_ = x; } |
| 26 |
| 27 float y() const { return y_; } |
| 28 void set_y(float y) { y_ = y; } |
| 29 |
| 30 // True if both components of the vector are 0. |
| 31 bool IsZero() const; |
| 32 |
| 33 // Adds |x| and |y| to the x-axis and y-axis components respectively. |
| 34 void Grow(float x, float y); |
| 35 |
| 36 // Add the components of the |other| vector to the current vector. |
| 37 void Add(const Vector2dF& other); |
| 38 // Subtract the components of the |other| vector from the current vector. |
| 39 void Subtract(const Vector2dF& other); |
| 40 |
| 41 void operator+=(const Vector2dF& other) { Add(other); } |
| 42 void operator-=(const Vector2dF& other) { Subtract(other); } |
| 43 |
| 44 // Gives the square of the diagonal length of the vector. |
| 45 float LengthSquared() const; |
| 46 // Gives the diagonal length of the vector. |
| 47 float Length() const; |
| 48 |
| 49 // Scale the x and y components of the vector by |scale|. |
| 50 void Scale(float scale) { Scale(scale, scale); } |
| 51 // Scale the x and y components of the vector by |x_scale| and |y_scale| |
| 52 // respectively. |
| 53 void Scale(float x_scale, float y_scale); |
| 54 |
| 55 std::string ToString() const; |
| 56 |
| 57 private: |
| 58 float x_; |
| 59 float y_; |
| 60 }; |
| 61 |
| 62 inline bool operator==(const Vector2dF& lhs, const Vector2dF& rhs) { |
| 63 return lhs.x() == rhs.x() && lhs.y() == rhs.y(); |
| 64 } |
| 65 |
| 66 inline Vector2dF operator-(const Vector2dF& v) { |
| 67 return Vector2dF(-v.x(), -v.y()); |
| 68 } |
| 69 |
| 70 inline Vector2dF operator+(const Vector2dF& lhs, const Vector2dF& rhs) { |
| 71 Vector2dF result = lhs; |
| 72 result.Add(rhs); |
| 73 return result; |
| 74 } |
| 75 |
| 76 inline Vector2dF operator-(const Vector2dF& lhs, const Vector2dF& rhs) { |
| 77 Vector2dF result = lhs; |
| 78 result.Add(-rhs); |
| 79 return result; |
| 80 } |
| 81 |
| 82 } // namespace gfx |
| 83 |
| 84 #endif // UI_GFX_VECTOR2D_F_H_ |
OLD | NEW |