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/geometry/vector2d_f.h"
6
7 #include <cmath>
8
9 #include "base/strings/stringprintf.h"
10
11 namespace gfx {
12
ToString() const13 std::string Vector2dF::ToString() const {
14 return base::StringPrintf("[%f %f]", x_, y_);
15 }
16
IsZero() const17 bool Vector2dF::IsZero() const {
18 return x_ == 0 && y_ == 0;
19 }
20
Add(const Vector2dF & other)21 void Vector2dF::Add(const Vector2dF& other) {
22 x_ += other.x_;
23 y_ += other.y_;
24 }
25
Subtract(const Vector2dF & other)26 void Vector2dF::Subtract(const Vector2dF& other) {
27 x_ -= other.x_;
28 y_ -= other.y_;
29 }
30
LengthSquared() const31 double Vector2dF::LengthSquared() const {
32 return static_cast<double>(x_) * x_ + static_cast<double>(y_) * y_;
33 }
34
Length() const35 float Vector2dF::Length() const {
36 return static_cast<float>(std::sqrt(LengthSquared()));
37 }
38
Scale(float x_scale,float y_scale)39 void Vector2dF::Scale(float x_scale, float y_scale) {
40 x_ *= x_scale;
41 y_ *= y_scale;
42 }
43
CrossProduct(const Vector2dF & lhs,const Vector2dF & rhs)44 double CrossProduct(const Vector2dF& lhs, const Vector2dF& rhs) {
45 return static_cast<double>(lhs.x()) * rhs.y() -
46 static_cast<double>(lhs.y()) * rhs.x();
47 }
48
DotProduct(const Vector2dF & lhs,const Vector2dF & rhs)49 double DotProduct(const Vector2dF& lhs, const Vector2dF& rhs) {
50 return static_cast<double>(lhs.x()) * rhs.x() +
51 static_cast<double>(lhs.y()) * rhs.y();
52 }
53
ScaleVector2d(const Vector2dF & v,float x_scale,float y_scale)54 Vector2dF ScaleVector2d(const Vector2dF& v, float x_scale, float y_scale) {
55 Vector2dF scaled_v(v);
56 scaled_v.Scale(x_scale, y_scale);
57 return scaled_v;
58 }
59
60 } // namespace gfx
61