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

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: RenderText fixup 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 <string>
14
15 #include "base/basictypes.h"
16 #include "ui/base/ui_export.h"
17 #include "ui/gfx/vector2d_f.h"
18
19 namespace gfx {
20
21 class UI_EXPORT Vector2d {
22 public:
23 Vector2d();
24 Vector2d(int x, int y);
25
26 int x() const { return x_; }
27 void set_x(int x) { x_ = x; }
28
29 int y() const { return y_; }
30 void set_y(int y) { y_ = y; }
31
32 // True if both components of the vector are 0.
33 bool IsZero() const;
34
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);
39
40 void operator+=(const Vector2d& other) { Add(other); }
41 void operator-=(const Vector2d& other) { Subtract(other); }
42
43 // Gives the square of the diagonal length of the vector. Since this is
44 // cheaper to compute than Length(), it is useful when you want to compare
45 // relative lengths of different vectors without needing the actual lengths.
46 int64 LengthSquared() const;
47 // Gives the diagonal length of the vector.
48 float Length() const;
49
50 std::string ToString() const;
51
52 operator Vector2dF() const { return Vector2dF(x_, y_); }
53
54 private:
55 int x_;
56 int y_;
57 };
58
59 inline bool operator==(const Vector2d& lhs, const Vector2d& rhs) {
60 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
61 }
62
63 inline Vector2d operator-(const Vector2d& v) {
64 return Vector2d(-v.x(), -v.y());
65 }
66
67 inline Vector2d operator+(const Vector2d& lhs, const Vector2d& rhs) {
68 Vector2d result = lhs;
69 result.Add(rhs);
70 return result;
71 }
72
73 inline Vector2d operator-(const Vector2d& lhs, const Vector2d& rhs) {
74 Vector2d result = lhs;
75 result.Add(-rhs);
76 return result;
77 }
78
79 } // namespace gfx
80
81 #endif // UI_GFX_VECTOR2D_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698