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