Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(849)

Side by Side Diff: ui/gfx/vector2d.h

Issue 11269022: Add Vector2d classes that represent offsets, instead of using Point. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 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.
9
10 #ifndef UI_GFX_VECTOR2D_H_
11 #define UI_GFX_VECTOR2D_H_
12
13 #include <cmath>
14 #include <string>
15
16 #include "base/basictypes.h"
17 #include "ui/base/ui_export.h"
18 #include "ui/gfx/vector2d_f.h"
19
20 namespace gfx {
21
22 class UI_EXPORT Vector2d {
23 public:
24 Vector2d();
25 Vector2d(int x, int y);
26
27 int x() const { return x_; }
28 void set_x(int x) { x_ = x; }
29
30 int y() const { return y_; }
31 void set_y(int y) { y_ = y; }
32
33 // True if both components of the vector are 0.
34 bool IsZero() const;
35
36 // Adds |x| and |y| to the x-axis and y-axis components respectively.
37 void Grow(int x, int y);
38
39 // Add the components of the |other| vector to the current vector.
40 void Add(const Vector2d& other);
41 // Subtract the components of the |other| vector from the current vector.
42 void Subtract(const Vector2d& other);
43
44 void operator+=(const Vector2d& other) { Add(other); }
45 void operator-=(const Vector2d& other) { Subtract(other); }
46
47 // Gives the square of the diagonal length of the vector.
48 int64 LengthSquared() const;
49 // Gives the diagonal length of the vector.
50 float Length() const;
51
52 std::string ToString() const;
53
54 operator Vector2dF() const { return Vector2dF(x_, y_); }
55
56 private:
57 int x_;
58 int y_;
59 };
60
61 inline bool operator==(const Vector2d& lhs, const Vector2d& rhs) {
62 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
63 }
64
65 inline Vector2d operator-(const Vector2d& v) {
66 return Vector2d(-v.x(), -v.y());
67 }
68
69 inline Vector2d operator+(const Vector2d& lhs, const Vector2d& rhs) {
70 Vector2d result = lhs;
71 result.Add(rhs);
72 return result;
73 }
74
75 inline Vector2d operator-(const Vector2d& lhs, const Vector2d& rhs) {
76 Vector2d result = lhs;
77 result.Add(-rhs);
78 return result;
79 }
80
81 } // namespace gfx
82
83 #endif // UI_GFX_VECTOR2D_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698