vector2d.cc 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. #include "ui/gfx/geometry/vector2d.h"
  5. #include <cmath>
  6. #include "base/numerics/clamped_math.h"
  7. #include "base/strings/stringprintf.h"
  8. namespace gfx {
  9. bool Vector2d::IsZero() const {
  10. return x_ == 0 && y_ == 0;
  11. }
  12. void Vector2d::Add(const Vector2d& other) {
  13. x_ = base::ClampAdd(other.x_, x_);
  14. y_ = base::ClampAdd(other.y_, y_);
  15. }
  16. void Vector2d::Subtract(const Vector2d& other) {
  17. x_ = base::ClampSub(x_, other.x_);
  18. y_ = base::ClampSub(y_, other.y_);
  19. }
  20. Vector2d operator-(const Vector2d& v) {
  21. return Vector2d(-base::MakeClampedNum(v.x()), -base::MakeClampedNum(v.y()));
  22. }
  23. int64_t Vector2d::LengthSquared() const {
  24. return static_cast<int64_t>(x_) * x_ + static_cast<int64_t>(y_) * y_;
  25. }
  26. float Vector2d::Length() const {
  27. return static_cast<float>(std::sqrt(static_cast<double>(LengthSquared())));
  28. }
  29. std::string Vector2d::ToString() const {
  30. return base::StringPrintf("[%d %d]", x_, y_);
  31. }
  32. } // namespace gfx