Home | History | Annotate | Download | only in geometry
      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:
     25   constexpr Vector2d() : x_(0), y_(0) {}
     26   constexpr Vector2d(int x, int y) : x_(x), y_(y) {}
     27 
     28   constexpr int x() const { return x_; }
     29   void set_x(int x) { x_ = x; }
     30 
     31   constexpr int y() const { return 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   void operator+=(const Vector2d& other) { Add(other); }
     43   void operator-=(const Vector2d& other) { Subtract(other); }
     44 
     45   void SetToMin(const Vector2d& other) {
     46     x_ = x_ <= other.x_ ? x_ : other.x_;
     47     y_ = y_ <= other.y_ ? y_ : other.y_;
     48   }
     49 
     50   void SetToMax(const Vector2d& other) {
     51     x_ = x_ >= other.x_ ? x_ : other.x_;
     52     y_ = y_ >= other.y_ ? y_ : other.y_;
     53   }
     54 
     55   // Gives the square of the diagonal length of the vector. Since this is
     56   // cheaper to compute than Length(), it is useful when you want to compare
     57   // relative lengths of different vectors without needing the actual lengths.
     58   int64_t LengthSquared() const;
     59   // Gives the diagonal length of the vector.
     60   float Length() const;
     61 
     62   std::string ToString() const;
     63 
     64   operator Vector2dF() const {
     65     return Vector2dF(static_cast<float>(x()), static_cast<float>(y()));
     66   }
     67 
     68  private:
     69   int x_;
     70   int y_;
     71 };
     72 
     73 inline bool operator==(const Vector2d& lhs, const Vector2d& rhs) {
     74   return lhs.x() == rhs.x() && lhs.y() == rhs.y();
     75 }
     76 
     77 inline Vector2d operator-(const Vector2d& v) {
     78   return Vector2d(-v.x(), -v.y());
     79 }
     80 
     81 inline Vector2d operator+(const Vector2d& lhs, const Vector2d& rhs) {
     82   Vector2d result = lhs;
     83   result.Add(rhs);
     84   return result;
     85 }
     86 
     87 inline Vector2d operator-(const Vector2d& lhs, const Vector2d& rhs) {
     88   Vector2d result = lhs;
     89   result.Add(-rhs);
     90   return result;
     91 }
     92 
     93 // This is declared here for use in gtest-based unit tests but is defined in
     94 // the //ui/gfx:test_support target. Depend on that to use this in your unit
     95 // test. This should not be used in production code - call ToString() instead.
     96 void PrintTo(const Vector2d& vector, ::std::ostream* os);
     97 
     98 }  // namespace gfx
     99 
    100 #endif // UI_GFX_GEOMETRY_VECTOR2D_H_
    101