point_f.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #ifndef UI_GFX_GEOMETRY_POINT_F_H_
  5. #define UI_GFX_GEOMETRY_POINT_F_H_
  6. #include <iosfwd>
  7. #include <string>
  8. #include <tuple>
  9. #include "build/build_config.h"
  10. #include "ui/gfx/geometry/geometry_export.h"
  11. #include "ui/gfx/geometry/point.h"
  12. #include "ui/gfx/geometry/vector2d_f.h"
  13. #if BUILDFLAG(IS_APPLE)
  14. struct CGPoint;
  15. #endif
  16. namespace gfx {
  17. // A floating version of gfx::Point.
  18. class GEOMETRY_EXPORT PointF {
  19. public:
  20. constexpr PointF() : x_(0.f), y_(0.f) {}
  21. constexpr PointF(float x, float y) : x_(x), y_(y) {}
  22. constexpr explicit PointF(const Point& p)
  23. : PointF(static_cast<float>(p.x()), static_cast<float>(p.y())) {}
  24. #if BUILDFLAG(IS_APPLE)
  25. explicit PointF(const CGPoint&);
  26. CGPoint ToCGPoint() const;
  27. #endif
  28. constexpr float x() const { return x_; }
  29. constexpr float y() const { return y_; }
  30. void set_x(float x) { x_ = x; }
  31. void set_y(float y) { y_ = y; }
  32. void SetPoint(float x, float y) {
  33. x_ = x;
  34. y_ = y;
  35. }
  36. void Offset(float delta_x, float delta_y) {
  37. x_ += delta_x;
  38. y_ += delta_y;
  39. }
  40. constexpr void operator+=(const Vector2dF& vector) {
  41. x_ += vector.x();
  42. y_ += vector.y();
  43. }
  44. constexpr void operator-=(const Vector2dF& vector) {
  45. x_ -= vector.x();
  46. y_ -= vector.y();
  47. }
  48. void SetToMin(const PointF& other);
  49. void SetToMax(const PointF& other);
  50. bool IsOrigin() const { return x_ == 0 && y_ == 0; }
  51. constexpr Vector2dF OffsetFromOrigin() const { return Vector2dF(x_, y_); }
  52. // A point is less than another point if its y-value is closer
  53. // to the origin. If the y-values are the same, then point with
  54. // the x-value closer to the origin is considered less than the
  55. // other.
  56. // This comparison is required to use PointF in sets, or sorted
  57. // vectors.
  58. bool operator<(const PointF& rhs) const {
  59. return std::tie(y_, x_) < std::tie(rhs.y_, rhs.x_);
  60. }
  61. void Scale(float scale) {
  62. Scale(scale, scale);
  63. }
  64. void Scale(float x_scale, float y_scale) {
  65. SetPoint(x() * x_scale, y() * y_scale);
  66. }
  67. void Transpose() {
  68. using std::swap;
  69. swap(x_, y_);
  70. }
  71. // Uses the Pythagorean theorem to determine the straight line distance
  72. // between the two points, and returns true if it is less than
  73. // |allowed_distance|.
  74. bool IsWithinDistance(const PointF& rhs, const float allowed_distance) const;
  75. // Returns a string representation of point.
  76. std::string ToString() const;
  77. private:
  78. float x_;
  79. float y_;
  80. };
  81. constexpr bool operator==(const PointF& lhs, const PointF& rhs) {
  82. return lhs.x() == rhs.x() && lhs.y() == rhs.y();
  83. }
  84. constexpr bool operator!=(const PointF& lhs, const PointF& rhs) {
  85. return !(lhs == rhs);
  86. }
  87. constexpr PointF operator+(const PointF& lhs, const Vector2dF& rhs) {
  88. PointF result(lhs);
  89. result += rhs;
  90. return result;
  91. }
  92. constexpr PointF operator-(const PointF& lhs, const Vector2dF& rhs) {
  93. PointF result(lhs);
  94. result -= rhs;
  95. return result;
  96. }
  97. inline Vector2dF operator-(const PointF& lhs, const PointF& rhs) {
  98. return Vector2dF(lhs.x() - rhs.x(), lhs.y() - rhs.y());
  99. }
  100. inline PointF PointAtOffsetFromOrigin(const Vector2dF& offset_from_origin) {
  101. return PointF(offset_from_origin.x(), offset_from_origin.y());
  102. }
  103. GEOMETRY_EXPORT PointF ScalePoint(const PointF& p,
  104. float x_scale,
  105. float y_scale);
  106. inline PointF ScalePoint(const PointF& p, float scale) {
  107. return ScalePoint(p, scale, scale);
  108. }
  109. inline PointF TransposePoint(const PointF& p) {
  110. return PointF(p.y(), p.x());
  111. }
  112. // This is declared here for use in gtest-based unit tests but is defined in
  113. // the //ui/gfx:test_support target. Depend on that to use this in your unit
  114. // test. This should not be used in production code - call ToString() instead.
  115. void PrintTo(const PointF& point, ::std::ostream* os);
  116. } // namespace gfx
  117. #endif // UI_GFX_GEOMETRY_POINT_F_H_