aboutsummaryrefslogtreecommitdiff
path: root/ui/gfx/geometry/vector2d.cc
blob: 2b4875c39cb4a9905f2ed0699fcb9164ee8fe2d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/geometry/vector2d.h"

#include <cmath>

#include "base/numerics/saturated_arithmetic.h"
#include "base/strings/stringprintf.h"

namespace gfx {

bool Vector2d::IsZero() const {
  return x_ == 0 && y_ == 0;
}

void Vector2d::Add(const Vector2d& other) {
  x_ = base::SaturatedAddition(other.x_, x_);
  y_ = base::SaturatedAddition(other.y_, y_);
}

void Vector2d::Subtract(const Vector2d& other) {
  x_ = base::SaturatedSubtraction(x_, other.x_);
  y_ = base::SaturatedSubtraction(y_, other.y_);
}

int64_t Vector2d::LengthSquared() const {
  return static_cast<int64_t>(x_) * x_ + static_cast<int64_t>(y_) * y_;
}

float Vector2d::Length() const {
  return static_cast<float>(std::sqrt(static_cast<double>(LengthSquared())));
}

std::string Vector2d::ToString() const {
  return base::StringPrintf("[%d %d]", x_, y_);
}

}  // namespace gfx