Home | History | Annotate | Download | only in gfx
      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 float 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_F_H_
     11 #define UI_GFX_VECTOR2D_F_H_
     12 
     13 #include <string>
     14 
     15 #include "ui/base/ui_export.h"
     16 
     17 namespace gfx {
     18 
     19 class UI_EXPORT Vector2dF {
     20  public:
     21   Vector2dF() : x_(0), y_(0) {}
     22   Vector2dF(float x, float y) : x_(x), y_(y) {}
     23 
     24   float x() const { return x_; }
     25   void set_x(float x) { x_ = x; }
     26 
     27   float y() const { return y_; }
     28   void set_y(float y) { y_ = y; }
     29 
     30   // True if both components of the vector are 0.
     31   bool IsZero() const;
     32 
     33   // Add the components of the |other| vector to the current vector.
     34   void Add(const Vector2dF& other);
     35   // Subtract the components of the |other| vector from the current vector.
     36   void Subtract(const Vector2dF& other);
     37 
     38   void operator+=(const Vector2dF& other) { Add(other); }
     39   void operator-=(const Vector2dF& other) { Subtract(other); }
     40 
     41   void SetToMin(const Vector2dF& other) {
     42     x_ = x_ <= other.x_ ? x_ : other.x_;
     43     y_ = y_ <= other.y_ ? y_ : other.y_;
     44   }
     45 
     46   void SetToMax(const Vector2dF& other) {
     47     x_ = x_ >= other.x_ ? x_ : other.x_;
     48     y_ = y_ >= other.y_ ? y_ : other.y_;
     49   }
     50 
     51   // Gives the square of the diagonal length of the vector.
     52   double LengthSquared() const;
     53   // Gives the diagonal length of the vector.
     54   float Length() const;
     55 
     56   // Scale the x and y components of the vector by |scale|.
     57   void Scale(float scale) { Scale(scale, scale); }
     58   // Scale the x and y components of the vector by |x_scale| and |y_scale|
     59   // respectively.
     60   void Scale(float x_scale, float y_scale);
     61 
     62   std::string ToString() const;
     63 
     64  private:
     65   float x_;
     66   float y_;
     67 };
     68 
     69 inline bool operator==(const Vector2dF& lhs, const Vector2dF& rhs) {
     70   return lhs.x() == rhs.x() && lhs.y() == rhs.y();
     71 }
     72 
     73 inline bool operator!=(const Vector2dF& lhs, const Vector2dF& rhs) {
     74   return !(lhs == rhs);
     75 }
     76 
     77 inline Vector2dF operator-(const Vector2dF& v) {
     78   return Vector2dF(-v.x(), -v.y());
     79 }
     80 
     81 inline Vector2dF operator+(const Vector2dF& lhs, const Vector2dF& rhs) {
     82   Vector2dF result = lhs;
     83   result.Add(rhs);
     84   return result;
     85 }
     86 
     87 inline Vector2dF operator-(const Vector2dF& lhs, const Vector2dF& rhs) {
     88   Vector2dF result = lhs;
     89   result.Add(-rhs);
     90   return result;
     91 }
     92 
     93 // Return the cross product of two vectors.
     94 UI_EXPORT double CrossProduct(const Vector2dF& lhs, const Vector2dF& rhs);
     95 
     96 // Return the dot product of two vectors.
     97 UI_EXPORT double DotProduct(const Vector2dF& lhs, const Vector2dF& rhs);
     98 
     99 // Return a vector that is |v| scaled by the given scale factors along each
    100 // axis.
    101 UI_EXPORT Vector2dF ScaleVector2d(const Vector2dF& v,
    102                                   float x_scale,
    103                                   float y_scale);
    104 
    105 // Return a vector that is |v| scaled by the given scale factor.
    106 inline Vector2dF ScaleVector2d(const Vector2dF& v, float scale) {
    107   return ScaleVector2d(v, scale, scale);
    108 }
    109 
    110 }  // namespace gfx
    111 
    112 #endif // UI_GFX_VECTOR2D_F_H_
    113