vector2d_f.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 float 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_F_H_
  9. #define UI_GFX_GEOMETRY_VECTOR2D_F_H_
  10. #include <iosfwd>
  11. #include <string>
  12. #include "ui/gfx/geometry/geometry_export.h"
  13. namespace gfx {
  14. class GEOMETRY_EXPORT Vector2dF {
  15. public:
  16. constexpr Vector2dF() : x_(0), y_(0) {}
  17. constexpr Vector2dF(float x, float y) : x_(x), y_(y) {}
  18. constexpr float x() const { return x_; }
  19. void set_x(float x) { x_ = x; }
  20. constexpr float y() const { return y_; }
  21. void set_y(float y) { y_ = y; }
  22. // True if both components of the vector are 0.
  23. bool IsZero() const;
  24. // Add the components of the |other| vector to the current vector.
  25. void Add(const Vector2dF& other);
  26. // Subtract the components of the |other| vector from the current vector.
  27. void Subtract(const Vector2dF& other);
  28. void operator+=(const Vector2dF& other) { Add(other); }
  29. void operator-=(const Vector2dF& other) { Subtract(other); }
  30. void SetToMin(const Vector2dF& other) {
  31. x_ = std::min(x_, other.x_);
  32. y_ = std::min(y_, other.y_);
  33. }
  34. void SetToMax(const Vector2dF& other) {
  35. x_ = std::max(x_, other.x_);
  36. y_ = std::max(y_, other.y_);
  37. }
  38. // Gives the square of the diagonal length, i.e. the square of magnitude, of
  39. // the vector.
  40. double LengthSquared() const;
  41. // Gives the diagonal length (i.e. the magnitude) of the vector.
  42. float Length() const;
  43. float AspectRatio() const { return x_ / y_; }
  44. // Gives the slope angle in radians of the vector from the positive x axis,
  45. // in the range of (-pi, pi]. The sign of the result is the same as the sign
  46. // of y(), except that the result is pi for Vector2dF(negative-x, zero-y).
  47. float SlopeAngleRadians() const;
  48. // Scale the x and y components of the vector by |scale|.
  49. void Scale(float scale) { Scale(scale, scale); }
  50. // Scale the x and y components of the vector by |x_scale| and |y_scale|
  51. // respectively.
  52. void Scale(float x_scale, float y_scale);
  53. void Transpose() {
  54. using std::swap;
  55. swap(x_, y_);
  56. }
  57. std::string ToString() const;
  58. private:
  59. float x_;
  60. float y_;
  61. };
  62. inline constexpr bool operator==(const Vector2dF& lhs, const Vector2dF& rhs) {
  63. return lhs.x() == rhs.x() && lhs.y() == rhs.y();
  64. }
  65. inline constexpr bool operator!=(const Vector2dF& lhs, const Vector2dF& rhs) {
  66. return !(lhs == rhs);
  67. }
  68. inline constexpr Vector2dF operator-(const Vector2dF& v) {
  69. return Vector2dF(-v.x(), -v.y());
  70. }
  71. inline Vector2dF operator+(const Vector2dF& lhs, const Vector2dF& rhs) {
  72. Vector2dF result = lhs;
  73. result.Add(rhs);
  74. return result;
  75. }
  76. inline Vector2dF operator-(const Vector2dF& lhs, const Vector2dF& rhs) {
  77. Vector2dF result = lhs;
  78. result.Add(-rhs);
  79. return result;
  80. }
  81. // Return the cross product of two vectors, i.e. the determinant.
  82. GEOMETRY_EXPORT double CrossProduct(const Vector2dF& lhs, const Vector2dF& rhs);
  83. // Return the dot product of two vectors.
  84. GEOMETRY_EXPORT double DotProduct(const Vector2dF& lhs, const Vector2dF& rhs);
  85. // Return a vector that is |v| scaled by the given scale factors along each
  86. // axis.
  87. GEOMETRY_EXPORT Vector2dF ScaleVector2d(const Vector2dF& v,
  88. float x_scale,
  89. float y_scale);
  90. // Return a vector that is |v| scaled by the given scale factor.
  91. inline Vector2dF ScaleVector2d(const Vector2dF& v, float scale) {
  92. return ScaleVector2d(v, scale, scale);
  93. }
  94. inline Vector2dF TransposeVector2d(const Vector2dF& v) {
  95. return Vector2dF(v.y(), v.x());
  96. }
  97. // This is declared here for use in gtest-based unit tests but is defined in
  98. // the //ui/gfx:test_support target. Depend on that to use this in your unit
  99. // test. This should not be used in production code - call ToString() instead.
  100. void PrintTo(const Vector2dF& vector, ::std::ostream* os);
  101. } // namespace gfx
  102. #endif // UI_GFX_GEOMETRY_VECTOR2D_F_H_