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 <string>
     14 
     15 #include "base/basictypes.h"
     16 #include "ui/gfx/geometry/vector2d_f.h"
     17 #include "ui/gfx/gfx_export.h"
     18 
     19 namespace gfx {
     20 
     21 class GFX_EXPORT Vector2d {
     22  public:
     23   Vector2d() : x_(0), y_(0) {}
     24   Vector2d(int x, int y) : x_(x), y_(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   // Add the components of the |other| vector to the current vector.
     36   void Add(const Vector2d& other);
     37   // Subtract the components of the |other| vector from the current vector.
     38   void Subtract(const Vector2d& other);
     39 
     40   void operator+=(const Vector2d& other) { Add(other); }
     41   void operator-=(const Vector2d& other) { Subtract(other); }
     42 
     43   void SetToMin(const Vector2d& other) {
     44     x_ = x_ <= other.x_ ? x_ : other.x_;
     45     y_ = y_ <= other.y_ ? y_ : other.y_;
     46   }
     47 
     48   void SetToMax(const Vector2d& other) {
     49     x_ = x_ >= other.x_ ? x_ : other.x_;
     50     y_ = y_ >= other.y_ ? y_ : other.y_;
     51   }
     52 
     53   // Gives the square of the diagonal length of the vector. Since this is
     54   // cheaper to compute than Length(), it is useful when you want to compare
     55   // relative lengths of different vectors without needing the actual lengths.
     56   int64 LengthSquared() const;
     57   // Gives the diagonal length of the vector.
     58   float Length() const;
     59 
     60   std::string ToString() const;
     61 
     62   operator Vector2dF() const { return Vector2dF(x_, y_); }
     63 
     64  private:
     65   int x_;
     66   int y_;
     67 };
     68 
     69 inline bool operator==(const Vector2d& lhs, const Vector2d& rhs) {
     70   return lhs.x() == rhs.x() && lhs.y() == rhs.y();
     71 }
     72 
     73 inline Vector2d operator-(const Vector2d& v) {
     74   return Vector2d(-v.x(), -v.y());
     75 }
     76 
     77 inline Vector2d operator+(const Vector2d& lhs, const Vector2d& rhs) {
     78   Vector2d result = lhs;
     79   result.Add(rhs);
     80   return result;
     81 }
     82 
     83 inline Vector2d operator-(const Vector2d& lhs, const Vector2d& rhs) {
     84   Vector2d result = lhs;
     85   result.Add(-rhs);
     86   return result;
     87 }
     88 
     89 }  // namespace gfx
     90 
     91 #endif // UI_GFX_GEOMETRY_VECTOR2D_H_
     92