OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/geometry/vector2d.h" | 5 #include "ui/gfx/geometry/vector2d.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
| 9 #include "base/numerics/saturated_arithmetic.h" |
9 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
10 #include "ui/gfx/geometry/safe_integer_conversions.h" | |
11 | 11 |
12 namespace gfx { | 12 namespace gfx { |
13 | 13 |
14 bool Vector2d::IsZero() const { | 14 bool Vector2d::IsZero() const { |
15 return x_ == 0 && y_ == 0; | 15 return x_ == 0 && y_ == 0; |
16 } | 16 } |
17 | 17 |
18 void Vector2d::Add(const Vector2d& other) { | 18 void Vector2d::Add(const Vector2d& other) { |
19 x_ = SafeAdd(other.x_, x_); | 19 x_ = base::SaturatedAddition(other.x_, x_); |
20 y_ = SafeAdd(other.y_, y_); | 20 y_ = base::SaturatedAddition(other.y_, y_); |
21 } | 21 } |
22 | 22 |
23 void Vector2d::Subtract(const Vector2d& other) { | 23 void Vector2d::Subtract(const Vector2d& other) { |
24 x_ = SafeSubtract(x_, other.x_); | 24 x_ = base::SaturatedSubtraction(x_, other.x_); |
25 y_ = SafeSubtract(y_, other.y_); | 25 y_ = base::SaturatedSubtraction(y_, other.y_); |
26 } | 26 } |
27 | 27 |
28 int64_t Vector2d::LengthSquared() const { | 28 int64_t Vector2d::LengthSquared() const { |
29 return static_cast<int64_t>(x_) * x_ + static_cast<int64_t>(y_) * y_; | 29 return static_cast<int64_t>(x_) * x_ + static_cast<int64_t>(y_) * y_; |
30 } | 30 } |
31 | 31 |
32 float Vector2d::Length() const { | 32 float Vector2d::Length() const { |
33 return static_cast<float>(std::sqrt(static_cast<double>(LengthSquared()))); | 33 return static_cast<float>(std::sqrt(static_cast<double>(LengthSquared()))); |
34 } | 34 } |
35 | 35 |
36 std::string Vector2d::ToString() const { | 36 std::string Vector2d::ToString() const { |
37 return base::StringPrintf("[%d %d]", x_, y_); | 37 return base::StringPrintf("[%d %d]", x_, y_); |
38 } | 38 } |
39 | 39 |
40 } // namespace gfx | 40 } // namespace gfx |
OLD | NEW |