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_GEOMETRY_VECTOR2D_H_ 11 #define UI_GFX_GEOMETRY_VECTOR2D_H_ 12 13 #include <stdint.h> 14 15 #include <iosfwd> 16 #include <string> 17 18 #include "ui/gfx/geometry/vector2d_f.h" 19 #include "ui/gfx/gfx_export.h" 20 21 namespace gfx { 22 23 class GFX_EXPORT Vector2d { 24 public: Vector2d()25 constexpr Vector2d() : x_(0), y_(0) {} Vector2d(int x,int y)26 constexpr Vector2d(int x, int y) : x_(x), y_(y) {} 27 x()28 constexpr int x() const { return x_; } set_x(int x)29 void set_x(int x) { x_ = x; } 30 y()31 constexpr int y() const { return y_; } set_y(int y)32 void set_y(int y) { y_ = y; } 33 34 // True if both components of the vector are 0. 35 bool IsZero() const; 36 37 // Add the components of the |other| vector to the current vector. 38 void Add(const Vector2d& other); 39 // Subtract the components of the |other| vector from the current vector. 40 void Subtract(const Vector2d& other); 41 42 constexpr bool operator==(const Vector2d& other) const { 43 return x_ == other.x_ && y_ == other.y_; 44 } 45 void operator+=(const Vector2d& other) { Add(other); } 46 void operator-=(const Vector2d& other) { Subtract(other); } 47 SetToMin(const Vector2d & other)48 void SetToMin(const Vector2d& other) { 49 x_ = x_ <= other.x_ ? x_ : other.x_; 50 y_ = y_ <= other.y_ ? y_ : other.y_; 51 } 52 SetToMax(const Vector2d & other)53 void SetToMax(const Vector2d& other) { 54 x_ = x_ >= other.x_ ? x_ : other.x_; 55 y_ = y_ >= other.y_ ? y_ : other.y_; 56 } 57 58 // Gives the square of the diagonal length of the vector. Since this is 59 // cheaper to compute than Length(), it is useful when you want to compare 60 // relative lengths of different vectors without needing the actual lengths. 61 int64_t LengthSquared() const; 62 // Gives the diagonal length of the vector. 63 float Length() const; 64 65 std::string ToString() const; 66 Vector2dF()67 operator Vector2dF() const { 68 return Vector2dF(static_cast<float>(x()), static_cast<float>(y())); 69 } 70 71 private: 72 int x_; 73 int y_; 74 }; 75 76 inline constexpr Vector2d operator-(const Vector2d& v) { 77 return Vector2d(-v.x(), -v.y()); 78 } 79 80 inline Vector2d operator+(const Vector2d& lhs, const Vector2d& rhs) { 81 Vector2d result = lhs; 82 result.Add(rhs); 83 return result; 84 } 85 86 inline Vector2d operator-(const Vector2d& lhs, const Vector2d& rhs) { 87 Vector2d result = lhs; 88 result.Add(-rhs); 89 return result; 90 } 91 92 // This is declared here for use in gtest-based unit tests but is defined in 93 // the //ui/gfx:test_support target. Depend on that to use this in your unit 94 // test. This should not be used in production code - call ToString() instead. 95 void PrintTo(const Vector2d& vector, ::std::ostream* os); 96 97 } // namespace gfx 98 99 #endif // UI_GFX_GEOMETRY_VECTOR2D_H_ 100