Chromium Code Reviews| 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 integer 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_H_ | |
| 11 #define UI_GFX_VECTOR2D_H_ | |
| 12 | |
| 13 #include <string> | |
| 14 | |
| 15 #include "base/basictypes.h" | |
|
Peter Kasting
2012/10/30 20:24:37
How come this file includes this but vector2d_f.h
danakj
2012/10/30 21:14:53
For LengthSquared to return int64.
| |
| 16 #include "ui/base/ui_export.h" | |
| 17 #include "ui/gfx/vector2d_f.h" | |
| 18 | |
| 19 namespace gfx { | |
| 20 | |
| 21 class UI_EXPORT Vector2d { | |
| 22 public: | |
| 23 Vector2d(); | |
| 24 Vector2d(int x, int y); | |
| 25 | |
| 26 int x() const { return x_; } | |
| 27 void set_x(int x) { x_ = x; } | |
| 28 | |
| 29 int y() const { return y_; } | |
| 30 void set_y(int y) { y_ = y; } | |
| 31 | |
| 32 // True if both components of the vector are 0. | |
| 33 bool IsZero() const; | |
| 34 | |
| 35 // Adds |x| and |y| to the x-axis and y-axis components respectively. | |
| 36 void Grow(int x, int y); | |
| 37 | |
| 38 // Add the components of the |other| vector to the current vector. | |
| 39 void Add(const Vector2d& other); | |
| 40 // Subtract the components of the |other| vector from the current vector. | |
| 41 void Subtract(const Vector2d& other); | |
| 42 | |
| 43 void operator+=(const Vector2d& other) { Add(other); } | |
| 44 void operator-=(const Vector2d& other) { Subtract(other); } | |
| 45 | |
| 46 // Gives the square of the diagonal length of the vector. Since this is | |
| 47 // cheaper to compute than Length(), it is useful when you want to compare | |
| 48 // relative lengths of different vectors without needing the actual lengths. | |
| 49 int64 LengthSquared() const; | |
| 50 // Gives the diagonal length of the vector. | |
| 51 float Length() const; | |
| 52 | |
| 53 std::string ToString() const; | |
| 54 | |
| 55 operator Vector2dF() const { return Vector2dF(x_, y_); } | |
| 56 | |
| 57 private: | |
| 58 int x_; | |
| 59 int y_; | |
| 60 }; | |
| 61 | |
| 62 inline bool operator==(const Vector2d& lhs, const Vector2d& rhs) { | |
| 63 return lhs.x() == rhs.x() && lhs.y() == rhs.y(); | |
| 64 } | |
| 65 | |
| 66 inline Vector2d operator-(const Vector2d& v) { | |
| 67 return Vector2d(-v.x(), -v.y()); | |
| 68 } | |
| 69 | |
| 70 inline Vector2d operator+(const Vector2d& lhs, const Vector2d& rhs) { | |
| 71 Vector2d result = lhs; | |
| 72 result.Add(rhs); | |
| 73 return result; | |
| 74 } | |
| 75 | |
| 76 inline Vector2d operator-(const Vector2d& lhs, const Vector2d& rhs) { | |
| 77 Vector2d result = lhs; | |
| 78 result.Add(-rhs); | |
| 79 return result; | |
| 80 } | |
| 81 | |
| 82 } // namespace gfx | |
| 83 | |
| 84 #endif // UI_GFX_VECTOR2D_H_ | |
| OLD | NEW |