vector2d.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // Defines a simple integer vector class. This class is used to indicate a
  5. // distance in two dimensions between two points. Subtracting two points should
  6. // produce a vector, and adding a vector to a point produces the point at the
  7. // vector's distance from the original point.
  8. #ifndef UI_GFX_GEOMETRY_VECTOR2D_H_
  9. #define UI_GFX_GEOMETRY_VECTOR2D_H_
  10. #include <stdint.h>
  11. #include <iosfwd>
  12. #include <string>
  13. #include "ui/gfx/geometry/geometry_export.h"
  14. #include "ui/gfx/geometry/vector2d_f.h"
  15. namespace gfx {
  16. class GEOMETRY_EXPORT Vector2d {
  17. public:
  18. constexpr Vector2d() : x_(0), y_(0) {}
  19. constexpr Vector2d(int x, int y) : x_(x), y_(y) {}
  20. constexpr int x() const { return x_; }
  21. void set_x(int x) { x_ = x; }
  22. constexpr int y() const { return y_; }
  23. void set_y(int y) { y_ = y; }
  24. // True if both components of the vector are 0.
  25. bool IsZero() const;
  26. // Add the components of the |other| vector to the current vector.
  27. void Add(const Vector2d& other);
  28. // Subtract the components of the |other| vector from the current vector.
  29. void Subtract(const Vector2d& other);
  30. constexpr bool operator==(const Vector2d& other) const {
  31. return x_ == other.x_ && y_ == other.y_;
  32. }
  33. void operator+=(const Vector2d& other) { Add(other); }
  34. void operator-=(const Vector2d& other) { Subtract(other); }
  35. void SetToMin(const Vector2d& other) {
  36. x_ = std::min(x_, other.x_);
  37. y_ = std::min(y_, other.y_);
  38. }
  39. void SetToMax(const Vector2d& other) {
  40. x_ = std::max(x_, other.x_);
  41. y_ = std::max(y_, other.y_);
  42. }
  43. // Gives the square of the diagonal length of the vector. Since this is
  44. // cheaper to compute than Length(), it is useful when you want to compare
  45. // relative lengths of different vectors without needing the actual lengths.
  46. int64_t LengthSquared() const;
  47. // Gives the diagonal length of the vector.
  48. float Length() const;
  49. void Transpose() {
  50. using std::swap;
  51. swap(x_, y_);
  52. }
  53. std::string ToString() const;
  54. operator Vector2dF() const {
  55. return Vector2dF(static_cast<float>(x()), static_cast<float>(y()));
  56. }
  57. private:
  58. int x_;
  59. int y_;
  60. };
  61. GEOMETRY_EXPORT Vector2d operator-(const Vector2d&);
  62. inline Vector2d operator+(const Vector2d& lhs, const Vector2d& rhs) {
  63. Vector2d result = lhs;
  64. result.Add(rhs);
  65. return result;
  66. }
  67. inline Vector2d operator-(const Vector2d& lhs, const Vector2d& rhs) {
  68. Vector2d result = lhs;
  69. result.Subtract(rhs);
  70. return result;
  71. }
  72. inline Vector2d TransposeVector2d(const Vector2d& v) {
  73. return Vector2d(v.y(), v.x());
  74. }
  75. // This is declared here for use in gtest-based unit tests but is defined in
  76. // the //ui/gfx:test_support target. Depend on that to use this in your unit
  77. // test. This should not be used in production code - call ToString() instead.
  78. void PrintTo(const Vector2d& vector, ::std::ostream* os);
  79. } // namespace gfx
  80. #endif // UI_GFX_GEOMETRY_VECTOR2D_H_