• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_GEOMETRY_VECTOR2D_F_H_
11 #define UI_GFX_GEOMETRY_VECTOR2D_F_H_
12 
13 #include <iosfwd>
14 #include <string>
15 
16 #include "ui/gfx/gfx_export.h"
17 
18 namespace gfx {
19 
20 class GFX_EXPORT Vector2dF {
21  public:
Vector2dF()22   constexpr Vector2dF() : x_(0), y_(0) {}
Vector2dF(float x,float y)23   constexpr Vector2dF(float x, float y) : x_(x), y_(y) {}
24 
x()25   constexpr float x() const { return x_; }
set_x(float x)26   void set_x(float x) { x_ = x; }
27 
y()28   constexpr float y() const { return y_; }
set_y(float y)29   void set_y(float y) { y_ = y; }
30 
31   // True if both components of the vector are 0.
32   bool IsZero() const;
33 
34   // Add the components of the |other| vector to the current vector.
35   void Add(const Vector2dF& other);
36   // Subtract the components of the |other| vector from the current vector.
37   void Subtract(const Vector2dF& other);
38 
39   void operator+=(const Vector2dF& other) { Add(other); }
40   void operator-=(const Vector2dF& other) { Subtract(other); }
41 
SetToMin(const Vector2dF & other)42   void SetToMin(const Vector2dF& other) {
43     x_ = x_ <= other.x_ ? x_ : other.x_;
44     y_ = y_ <= other.y_ ? y_ : other.y_;
45   }
46 
SetToMax(const Vector2dF & other)47   void SetToMax(const Vector2dF& other) {
48     x_ = x_ >= other.x_ ? x_ : other.x_;
49     y_ = y_ >= other.y_ ? y_ : other.y_;
50   }
51 
52   // Gives the square of the diagonal length of the vector.
53   double LengthSquared() const;
54   // Gives the diagonal length of the vector.
55   float Length() const;
56 
57   // Scale the x and y components of the vector by |scale|.
Scale(float scale)58   void Scale(float scale) { Scale(scale, scale); }
59   // Scale the x and y components of the vector by |x_scale| and |y_scale|
60   // respectively.
61   void Scale(float x_scale, float y_scale);
62 
63   std::string ToString() const;
64 
65  private:
66   float x_;
67   float y_;
68 };
69 
70 inline bool operator==(const Vector2dF& lhs, const Vector2dF& rhs) {
71   return lhs.x() == rhs.x() && lhs.y() == rhs.y();
72 }
73 
74 inline bool operator!=(const Vector2dF& lhs, const Vector2dF& rhs) {
75   return !(lhs == rhs);
76 }
77 
78 inline Vector2dF operator-(const Vector2dF& v) {
79   return Vector2dF(-v.x(), -v.y());
80 }
81 
82 inline Vector2dF operator+(const Vector2dF& lhs, const Vector2dF& rhs) {
83   Vector2dF result = lhs;
84   result.Add(rhs);
85   return result;
86 }
87 
88 inline Vector2dF operator-(const Vector2dF& lhs, const Vector2dF& rhs) {
89   Vector2dF result = lhs;
90   result.Add(-rhs);
91   return result;
92 }
93 
94 // Return the cross product of two vectors.
95 GFX_EXPORT double CrossProduct(const Vector2dF& lhs, const Vector2dF& rhs);
96 
97 // Return the dot product of two vectors.
98 GFX_EXPORT double DotProduct(const Vector2dF& lhs, const Vector2dF& rhs);
99 
100 // Return a vector that is |v| scaled by the given scale factors along each
101 // axis.
102 GFX_EXPORT Vector2dF ScaleVector2d(const Vector2dF& v,
103                                    float x_scale,
104                                    float y_scale);
105 
106 // Return a vector that is |v| scaled by the given scale factor.
ScaleVector2d(const Vector2dF & v,float scale)107 inline Vector2dF ScaleVector2d(const Vector2dF& v, float scale) {
108   return ScaleVector2d(v, scale, scale);
109 }
110 
111 // This is declared here for use in gtest-based unit tests but is defined in
112 // the //ui/gfx:test_support target. Depend on that to use this in your unit
113 // test. This should not be used in production code - call ToString() instead.
114 void PrintTo(const Vector2dF& vector, ::std::ostream* os);
115 
116 }  // namespace gfx
117 
118 #endif // UI_GFX_GEOMETRY_VECTOR2D_F_H_
119