[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / ui / gfx / geometry / vector2d.h
blob4e404be1a7e2007702ec5a11e1358922bfa05f8d
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 // 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.
10 #ifndef UI_GFX_GEOMETRY_VECTOR2D_H_
11 #define UI_GFX_GEOMETRY_VECTOR2D_H_
13 #include <iosfwd>
14 #include <string>
16 #include "base/basictypes.h"
17 #include "ui/gfx/geometry/vector2d_f.h"
18 #include "ui/gfx/gfx_export.h"
20 namespace gfx {
22 class GFX_EXPORT Vector2d {
23 public:
24 Vector2d() : x_(0), y_(0) {}
25 Vector2d(int x, int y) : x_(x), y_(y) {}
27 int x() const { return x_; }
28 void set_x(int x) { x_ = x; }
30 int y() const { return y_; }
31 void set_y(int y) { y_ = y; }
33 // True if both components of the vector are 0.
34 bool IsZero() const;
36 // Add the components of the |other| vector to the current vector.
37 void Add(const Vector2d& other);
38 // Subtract the components of the |other| vector from the current vector.
39 void Subtract(const Vector2d& other);
41 void operator+=(const Vector2d& other) { Add(other); }
42 void operator-=(const Vector2d& other) { Subtract(other); }
44 void SetToMin(const Vector2d& other) {
45 x_ = x_ <= other.x_ ? x_ : other.x_;
46 y_ = y_ <= other.y_ ? y_ : other.y_;
49 void SetToMax(const Vector2d& other) {
50 x_ = x_ >= other.x_ ? x_ : other.x_;
51 y_ = y_ >= other.y_ ? y_ : other.y_;
54 // Gives the square of the diagonal length of the vector. Since this is
55 // cheaper to compute than Length(), it is useful when you want to compare
56 // relative lengths of different vectors without needing the actual lengths.
57 int64 LengthSquared() const;
58 // Gives the diagonal length of the vector.
59 float Length() const;
61 std::string ToString() const;
63 operator Vector2dF() const { return Vector2dF(x_, y_); }
65 private:
66 int x_;
67 int y_;
70 inline bool operator==(const Vector2d& lhs, const Vector2d& rhs) {
71 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
74 inline Vector2d operator-(const Vector2d& v) {
75 return Vector2d(-v.x(), -v.y());
78 inline Vector2d operator+(const Vector2d& lhs, const Vector2d& rhs) {
79 Vector2d result = lhs;
80 result.Add(rhs);
81 return result;
84 inline Vector2d operator-(const Vector2d& lhs, const Vector2d& rhs) {
85 Vector2d result = lhs;
86 result.Add(-rhs);
87 return result;
90 // This is declared here for use in gtest-based unit tests but is defined in
91 // the gfx_test_support target. Depend on that to use this in your unit test.
92 // This should not be used in production code - call ToString() instead.
93 void PrintTo(const Vector2d& vector, ::std::ostream* os);
95 } // namespace gfx
97 #endif // UI_GFX_GEOMETRY_VECTOR2D_H_