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

Side by Side Diff: ui/gfx/vector2d_f.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 float 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_F_H_
11 #define UI_GFX_VECTOR2D_F_H_
12
13 #include <string>
14
15 #include "ui/base/ui_export.h"
16
17 namespace gfx {
18
19 class UI_EXPORT Vector2dF {
20 public:
21 Vector2dF();
22 Vector2dF(float x, float y);
23
24 float x() const { return x_; }
25 void set_x(float x) { x_ = x; }
26
27 float y() const { return y_; }
28 void set_y(float y) { y_ = y; }
29
30 // True if both components of the vector are 0.
31 bool IsZero() const;
32
33 // Add the components of the |other| vector to the current vector.
34 void Add(const Vector2dF& other);
35 // Subtract the components of the |other| vector from the current vector.
36 void Subtract(const Vector2dF& other);
37
38 void operator+=(const Vector2dF& other) { Add(other); }
39 void operator-=(const Vector2dF& other) { Subtract(other); }
40
41 // Gives the square of the diagonal length of the vector.
42 double LengthSquared() const;
43 // Gives the diagonal length of the vector.
44 float Length() const;
45
46 // Scale the x and y components of the vector by |scale|.
47 void Scale(float scale) { Scale(scale, scale); }
48 // Scale the x and y components of the vector by |x_scale| and |y_scale|
49 // respectively.
50 void Scale(float x_scale, float y_scale);
51
52 std::string ToString() const;
53
54 private:
55 float x_;
56 float y_;
57 };
58
59 inline bool operator==(const Vector2dF& lhs, const Vector2dF& rhs) {
60 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
61 }
62
63 inline Vector2dF operator-(const Vector2dF& v) {
64 return Vector2dF(-v.x(), -v.y());
65 }
66
67 inline Vector2dF operator+(const Vector2dF& lhs, const Vector2dF& rhs) {
68 Vector2dF result = lhs;
69 result.Add(rhs);
70 return result;
71 }
72
73 inline Vector2dF operator-(const Vector2dF& lhs, const Vector2dF& rhs) {
74 Vector2dF result = lhs;
75 result.Add(-rhs);
76 return result;
77 }
78
79 } // namespace gfx
80
81 #endif // UI_GFX_VECTOR2D_F_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698