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 dimentions 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 bool IsZero() const { return x_ == 0 && y_ == 0; } |
| 32 |
| 33 void Grow(float x, float y) { |
| 34 x_ += x; |
| 35 y_ += y; |
| 36 } |
| 37 |
| 38 void Add(const Vector2dF& other) { |
| 39 x_ += other.x_; |
| 40 y_ += other.y_; |
| 41 } |
| 42 |
| 43 void Scale(float scale) { |
| 44 Scale(scale, scale); |
| 45 } |
| 46 |
| 47 void Scale(float x_scale, float y_scale) { |
| 48 x_ *= x_scale; |
| 49 y_ *= y_scale; |
| 50 } |
| 51 |
| 52 float LengthSquared() const { |
| 53 return x_ * x_ + y_ * y_; |
| 54 } |
| 55 |
| 56 float Length() const { |
| 57 return static_cast<float>(std::sqrt( |
| 58 static_cast<double>(x_) * x_ + static_cast<double>(y_) * y_)); |
| 59 } |
| 60 |
| 61 std::string ToString() const; |
| 62 |
| 63 private: |
| 64 float x_; |
| 65 float y_; |
| 66 }; |
| 67 |
| 68 inline bool operator==(const Vector2dF& lhs, const Vector2dF& rhs) { |
| 69 return lhs.x() == rhs.x() && lhs.y() == rhs.y(); |
| 70 } |
| 71 |
| 72 inline Vector2dF operator-(const Vector2dF& v) { |
| 73 return Vector2dF(-v.x(), -v.y()); |
| 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 inline Vector2dF operator-(const Vector2dF& lhs, const Vector2dF& rhs) { |
| 83 Vector2dF result = lhs; |
| 84 result.Add(-rhs); |
| 85 return result; |
| 86 } |
| 87 |
| 88 } // namespace gfx |
| 89 |
| 90 #endif // UI_GFX_VECTOR2D_F_H_ |
OLD | NEW |