xref: /aosp_15_r20/external/libchrome/ui/gfx/geometry/vector2d.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker // Defines a simple integer vector class.  This class is used to indicate a
6*635a8641SAndroid Build Coastguard Worker // distance in two dimensions between two points. Subtracting two points should
7*635a8641SAndroid Build Coastguard Worker // produce a vector, and adding a vector to a point produces the point at the
8*635a8641SAndroid Build Coastguard Worker // vector's distance from the original point.
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #ifndef UI_GFX_GEOMETRY_VECTOR2D_H_
11*635a8641SAndroid Build Coastguard Worker #define UI_GFX_GEOMETRY_VECTOR2D_H_
12*635a8641SAndroid Build Coastguard Worker 
13*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
14*635a8641SAndroid Build Coastguard Worker 
15*635a8641SAndroid Build Coastguard Worker #include <iosfwd>
16*635a8641SAndroid Build Coastguard Worker #include <string>
17*635a8641SAndroid Build Coastguard Worker 
18*635a8641SAndroid Build Coastguard Worker #include "ui/gfx/geometry/vector2d_f.h"
19*635a8641SAndroid Build Coastguard Worker #include "ui/gfx/gfx_export.h"
20*635a8641SAndroid Build Coastguard Worker 
21*635a8641SAndroid Build Coastguard Worker namespace gfx {
22*635a8641SAndroid Build Coastguard Worker 
23*635a8641SAndroid Build Coastguard Worker class GFX_EXPORT Vector2d {
24*635a8641SAndroid Build Coastguard Worker  public:
Vector2d()25*635a8641SAndroid Build Coastguard Worker   constexpr Vector2d() : x_(0), y_(0) {}
Vector2d(int x,int y)26*635a8641SAndroid Build Coastguard Worker   constexpr Vector2d(int x, int y) : x_(x), y_(y) {}
27*635a8641SAndroid Build Coastguard Worker 
x()28*635a8641SAndroid Build Coastguard Worker   constexpr int x() const { return x_; }
set_x(int x)29*635a8641SAndroid Build Coastguard Worker   void set_x(int x) { x_ = x; }
30*635a8641SAndroid Build Coastguard Worker 
y()31*635a8641SAndroid Build Coastguard Worker   constexpr int y() const { return y_; }
set_y(int y)32*635a8641SAndroid Build Coastguard Worker   void set_y(int y) { y_ = y; }
33*635a8641SAndroid Build Coastguard Worker 
34*635a8641SAndroid Build Coastguard Worker   // True if both components of the vector are 0.
35*635a8641SAndroid Build Coastguard Worker   bool IsZero() const;
36*635a8641SAndroid Build Coastguard Worker 
37*635a8641SAndroid Build Coastguard Worker   // Add the components of the |other| vector to the current vector.
38*635a8641SAndroid Build Coastguard Worker   void Add(const Vector2d& other);
39*635a8641SAndroid Build Coastguard Worker   // Subtract the components of the |other| vector from the current vector.
40*635a8641SAndroid Build Coastguard Worker   void Subtract(const Vector2d& other);
41*635a8641SAndroid Build Coastguard Worker 
42*635a8641SAndroid Build Coastguard Worker   constexpr bool operator==(const Vector2d& other) const {
43*635a8641SAndroid Build Coastguard Worker     return x_ == other.x_ && y_ == other.y_;
44*635a8641SAndroid Build Coastguard Worker   }
45*635a8641SAndroid Build Coastguard Worker   void operator+=(const Vector2d& other) { Add(other); }
46*635a8641SAndroid Build Coastguard Worker   void operator-=(const Vector2d& other) { Subtract(other); }
47*635a8641SAndroid Build Coastguard Worker 
SetToMin(const Vector2d & other)48*635a8641SAndroid Build Coastguard Worker   void SetToMin(const Vector2d& other) {
49*635a8641SAndroid Build Coastguard Worker     x_ = x_ <= other.x_ ? x_ : other.x_;
50*635a8641SAndroid Build Coastguard Worker     y_ = y_ <= other.y_ ? y_ : other.y_;
51*635a8641SAndroid Build Coastguard Worker   }
52*635a8641SAndroid Build Coastguard Worker 
SetToMax(const Vector2d & other)53*635a8641SAndroid Build Coastguard Worker   void SetToMax(const Vector2d& other) {
54*635a8641SAndroid Build Coastguard Worker     x_ = x_ >= other.x_ ? x_ : other.x_;
55*635a8641SAndroid Build Coastguard Worker     y_ = y_ >= other.y_ ? y_ : other.y_;
56*635a8641SAndroid Build Coastguard Worker   }
57*635a8641SAndroid Build Coastguard Worker 
58*635a8641SAndroid Build Coastguard Worker   // Gives the square of the diagonal length of the vector. Since this is
59*635a8641SAndroid Build Coastguard Worker   // cheaper to compute than Length(), it is useful when you want to compare
60*635a8641SAndroid Build Coastguard Worker   // relative lengths of different vectors without needing the actual lengths.
61*635a8641SAndroid Build Coastguard Worker   int64_t LengthSquared() const;
62*635a8641SAndroid Build Coastguard Worker   // Gives the diagonal length of the vector.
63*635a8641SAndroid Build Coastguard Worker   float Length() const;
64*635a8641SAndroid Build Coastguard Worker 
65*635a8641SAndroid Build Coastguard Worker   std::string ToString() const;
66*635a8641SAndroid Build Coastguard Worker 
Vector2dF()67*635a8641SAndroid Build Coastguard Worker   operator Vector2dF() const {
68*635a8641SAndroid Build Coastguard Worker     return Vector2dF(static_cast<float>(x()), static_cast<float>(y()));
69*635a8641SAndroid Build Coastguard Worker   }
70*635a8641SAndroid Build Coastguard Worker 
71*635a8641SAndroid Build Coastguard Worker  private:
72*635a8641SAndroid Build Coastguard Worker   int x_;
73*635a8641SAndroid Build Coastguard Worker   int y_;
74*635a8641SAndroid Build Coastguard Worker };
75*635a8641SAndroid Build Coastguard Worker 
76*635a8641SAndroid Build Coastguard Worker inline constexpr Vector2d operator-(const Vector2d& v) {
77*635a8641SAndroid Build Coastguard Worker   return Vector2d(-v.x(), -v.y());
78*635a8641SAndroid Build Coastguard Worker }
79*635a8641SAndroid Build Coastguard Worker 
80*635a8641SAndroid Build Coastguard Worker inline Vector2d operator+(const Vector2d& lhs, const Vector2d& rhs) {
81*635a8641SAndroid Build Coastguard Worker   Vector2d result = lhs;
82*635a8641SAndroid Build Coastguard Worker   result.Add(rhs);
83*635a8641SAndroid Build Coastguard Worker   return result;
84*635a8641SAndroid Build Coastguard Worker }
85*635a8641SAndroid Build Coastguard Worker 
86*635a8641SAndroid Build Coastguard Worker inline Vector2d operator-(const Vector2d& lhs, const Vector2d& rhs) {
87*635a8641SAndroid Build Coastguard Worker   Vector2d result = lhs;
88*635a8641SAndroid Build Coastguard Worker   result.Add(-rhs);
89*635a8641SAndroid Build Coastguard Worker   return result;
90*635a8641SAndroid Build Coastguard Worker }
91*635a8641SAndroid Build Coastguard Worker 
92*635a8641SAndroid Build Coastguard Worker // This is declared here for use in gtest-based unit tests but is defined in
93*635a8641SAndroid Build Coastguard Worker // the //ui/gfx:test_support target. Depend on that to use this in your unit
94*635a8641SAndroid Build Coastguard Worker // test. This should not be used in production code - call ToString() instead.
95*635a8641SAndroid Build Coastguard Worker void PrintTo(const Vector2d& vector, ::std::ostream* os);
96*635a8641SAndroid Build Coastguard Worker 
97*635a8641SAndroid Build Coastguard Worker }  // namespace gfx
98*635a8641SAndroid Build Coastguard Worker 
99*635a8641SAndroid Build Coastguard Worker #endif // UI_GFX_GEOMETRY_VECTOR2D_H_
100