Disable ExtensionIconSourceTest.IconLoaded* on Mac and Win
[chromium-blink-merge.git] / ui / gfx / vector2d.h
blobe22a7512c39cb402f7165eb268fdf805bed69fe0
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_
13 #include <string>
15 #include "base/basictypes.h"
16 #include "ui/base/ui_export.h"
17 #include "ui/gfx/vector2d_f.h"
19 namespace gfx {
21 class UI_EXPORT Vector2d {
22 public:
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.
33 bool IsZero() const;
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 ClampToMax(const Vector2d& max) {
44 x_ = x_ <= max.x_ ? x_ : max.x_;
45 y_ = y_ <= max.y_ ? y_ : max.y_;
48 void ClampToMin(const Vector2d& min) {
49 x_ = x_ >= min.x_ ? x_ : min.x_;
50 y_ = y_ >= min.y_ ? y_ : min.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.
58 float Length() const;
60 std::string ToString() const;
62 operator Vector2dF() const { return Vector2dF(x_, y_); }
64 private:
65 int x_;
66 int 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;
79 result.Add(rhs);
80 return result;
83 inline Vector2d operator-(const Vector2d& lhs, const Vector2d& rhs) {
84 Vector2d result = lhs;
85 result.Add(-rhs);
86 return result;
89 } // namespace gfx
91 #endif // UI_GFX_VECTOR2D_H_