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

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: more vector use fixes 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; }
Peter Kasting 2012/10/30 01:14:14 Only ParamTraits uses set_x() and set_y(). I wond
danakj 2012/10/30 19:21:21 I made the code in render_text use set_x() for the
Peter Kasting 2012/10/30 20:24:37 Oh. I'm not sure I actually like that change. As
danakj 2012/10/30 21:14:52 Ok, I've undone that to not use set_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;
Peter Kasting 2012/10/30 01:14:14 Nit: Nowhere besides tests seem to use this, remov
danakj 2012/10/30 19:21:21 This is used in the cc/ code. I added it here when
Peter Kasting 2012/10/30 20:24:37 No, it's fine.
34
35 // Adds |x| and |y| to the x-axis and y-axis components respectively.
36 void Grow(int x, int y);
37
38 // Add the components of the |other| vector to the current vector.
39 void Add(const Vector2d& other);
40 // Subtract the components of the |other| vector from the current vector.
41 void Subtract(const Vector2d& other);
42
43 void operator+=(const Vector2d& other) { Add(other); }
44 void operator-=(const Vector2d& other) { Subtract(other); }
45
46 // Gives the square of the diagonal length of the vector.
Peter Kasting 2012/10/30 01:14:14 Nit: Perhaps add note that this is cheaper than Le
47 int64 LengthSquared() const;
48 // Gives the diagonal length of the vector.
49 float Length() const;
50
51 std::string ToString() const;
52
53 operator Vector2dF() const { return Vector2dF(x_, y_); }
54
55 private:
56 int x_;
57 int y_;
58 };
59
60 inline bool operator==(const Vector2d& lhs, const Vector2d& rhs) {
61 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
62 }
63
64 inline Vector2d operator-(const Vector2d& v) {
65 return Vector2d(-v.x(), -v.y());
66 }
67
68 inline Vector2d operator+(const Vector2d& lhs, const Vector2d& rhs) {
69 Vector2d result = lhs;
70 result.Add(rhs);
71 return result;
72 }
73
74 inline Vector2d operator-(const Vector2d& lhs, const Vector2d& rhs) {
75 Vector2d result = lhs;
76 result.Add(-rhs);
77 return result;
78 }
79
80 } // namespace gfx
81
82 #endif // UI_GFX_VECTOR2D_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698