OLD | NEW |
---|---|
(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 #include "ui/gfx/vector2d.h" | |
6 | |
7 #include <cmath> | |
8 | |
9 #include "base/stringprintf.h" | |
10 | |
11 namespace gfx { | |
12 | |
13 Vector2d::Vector2d() | |
14 : x_(0), | |
15 y_(0) { | |
Peter Kasting
2012/10/30 01:14:14
Nit: Could also put these first three lines all on
| |
16 } | |
17 | |
18 Vector2d::Vector2d(int x, int y) | |
19 : x_(x), | |
20 y_(y) { | |
21 } | |
22 | |
23 bool Vector2d::IsZero() const { | |
24 return x_ == 0 && y_ == 0; | |
25 } | |
26 | |
27 void Vector2d::Grow(int x, int y) { | |
28 x_ += x; | |
29 y_ += y; | |
30 } | |
31 | |
32 void Vector2d::Add(const Vector2d& other) { | |
33 x_ += other.x_; | |
34 y_ += other.y_; | |
35 } | |
36 | |
37 void Vector2d::Subtract(const Vector2d& other) { | |
38 x_ -= other.x_; | |
39 y_ -= other.y_; | |
40 } | |
41 | |
42 int64 Vector2d::LengthSquared() const { | |
43 return x_ * x_ + y_ * y_; | |
44 } | |
45 | |
46 float Vector2d::Length() const { | |
47 return static_cast<float>(std::sqrt( | |
48 static_cast<double>(x_) * x_ + static_cast<double>(y_) * y_)); | |
Peter Kasting
2012/10/30 01:14:14
Nit: Would this be more efficient as
static
danakj
2012/10/30 19:21:21
But we'd also lose some range, as x*x doesn't fit
Peter Kasting
2012/10/30 20:24:37
What about this then:
return static_cast<float>
danakj
2012/10/30 21:14:52
Ya, I like that!
| |
49 } | |
50 | |
51 std::string Vector2d::ToString() const { | |
52 return base::StringPrintf("[%d %d]", x_, y_); | |
53 } | |
54 | |
55 } // namespace gfx | |
OLD | NEW |