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.
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.
10 #ifndef UI_GFX_VECTOR2D_H_
11 #define UI_GFX_VECTOR2D_H_
15 #include "base/basictypes.h"
16 #include "ui/gfx/gfx_export.h"
17 #include "ui/gfx/vector2d_f.h"
21 class GFX_EXPORT Vector2d
{
23 Vector2d() : x_(0), y_(0) {}
24 Vector2d(int x
, int y
) : x_(x
), y_(y
) {}
26 int x() const { return x_
; }
27 void set_x(int x
) { x_
= x
; }
29 int y() const { return y_
; }
30 void set_y(int y
) { y_
= y
; }
32 // True if both components of the vector are 0.
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
);
40 void operator+=(const Vector2d
& other
) { Add(other
); }
41 void operator-=(const Vector2d
& other
) { Subtract(other
); }
43 void SetToMin(const Vector2d
& other
) {
44 x_
= x_
<= other
.x_
? x_
: other
.x_
;
45 y_
= y_
<= other
.y_
? y_
: other
.y_
;
48 void SetToMax(const Vector2d
& other
) {
49 x_
= x_
>= other
.x_
? x_
: other
.x_
;
50 y_
= y_
>= other
.y_
? y_
: other
.y_
;
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.
60 std::string
ToString() const;
62 operator Vector2dF() const { return Vector2dF(x_
, y_
); }
69 inline bool operator==(const Vector2d
& lhs
, const Vector2d
& rhs
) {
70 return lhs
.x() == rhs
.x() && lhs
.y() == rhs
.y();
73 inline Vector2d
operator-(const Vector2d
& v
) {
74 return Vector2d(-v
.x(), -v
.y());
77 inline Vector2d
operator+(const Vector2d
& lhs
, const Vector2d
& rhs
) {
78 Vector2d result
= lhs
;
83 inline Vector2d
operator-(const Vector2d
& lhs
, const Vector2d
& rhs
) {
84 Vector2d result
= lhs
;
91 #endif // UI_GFX_VECTOR2D_H_