point.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_H_
  5. #define UI_GFX_GEOMETRY_POINT_H_
  6. #include <iosfwd>
  7. #include <string>
  8. #include <tuple>
  9. #include "base/numerics/clamped_math.h"
  10. #include "build/build_config.h"
  11. #include "ui/gfx/geometry/geometry_export.h"
  12. #include "ui/gfx/geometry/vector2d.h"
  13. #if BUILDFLAG(IS_WIN)
  14. typedef unsigned long DWORD;
  15. typedef struct tagPOINT POINT;
  16. #elif BUILDFLAG(IS_APPLE)
  17. typedef struct CGPoint CGPoint;
  18. #endif
  19. namespace gfx {
  20. // A point has an x and y coordinate.
  21. class GEOMETRY_EXPORT Point {
  22. public:
  23. constexpr Point() : x_(0), y_(0) {}
  24. constexpr Point(int x, int y) : x_(x), y_(y) {}
  25. #if BUILDFLAG(IS_WIN)
  26. // |point| is a DWORD value that contains a coordinate. The x-coordinate is
  27. // the low-order short and the y-coordinate is the high-order short. This
  28. // value is commonly acquired from GetMessagePos/GetCursorPos.
  29. explicit Point(DWORD point);
  30. explicit Point(const POINT& point);
  31. Point& operator=(const POINT& point);
  32. #elif BUILDFLAG(IS_APPLE)
  33. explicit Point(const CGPoint& point);
  34. #endif
  35. #if BUILDFLAG(IS_WIN)
  36. POINT ToPOINT() const;
  37. #elif BUILDFLAG(IS_APPLE)
  38. CGPoint ToCGPoint() const;
  39. #endif
  40. constexpr int x() const { return x_; }
  41. constexpr int y() const { return y_; }
  42. void set_x(int x) { x_ = x; }
  43. void set_y(int y) { y_ = y; }
  44. void SetPoint(int x, int y) {
  45. x_ = x;
  46. y_ = y;
  47. }
  48. void Offset(int delta_x, int delta_y) {
  49. x_ = base::ClampAdd(x_, delta_x);
  50. y_ = base::ClampAdd(y_, delta_y);
  51. }
  52. void operator+=(const Vector2d& vector) {
  53. x_ = base::ClampAdd(x_, vector.x());
  54. y_ = base::ClampAdd(y_, vector.y());
  55. }
  56. void operator-=(const Vector2d& vector) {
  57. x_ = base::ClampSub(x_, vector.x());
  58. y_ = base::ClampSub(y_, vector.y());
  59. }
  60. void SetToMin(const Point& other);
  61. void SetToMax(const Point& other);
  62. bool IsOrigin() const { return x_ == 0 && y_ == 0; }
  63. Vector2d OffsetFromOrigin() const { return Vector2d(x_, y_); }
  64. void Transpose() {
  65. using std::swap;
  66. swap(x_, y_);
  67. }
  68. // A point is less than another point if its y-value is closer
  69. // to the origin. If the y-values are the same, then point with
  70. // the x-value closer to the origin is considered less than the
  71. // other.
  72. // This comparison is required to use Point in sets, or sorted
  73. // vectors.
  74. bool operator<(const Point& rhs) const {
  75. return std::tie(y_, x_) < std::tie(rhs.y_, rhs.x_);
  76. }
  77. // Returns a string representation of point.
  78. std::string ToString() const;
  79. private:
  80. int x_;
  81. int y_;
  82. };
  83. constexpr bool operator==(const Point& lhs, const Point& rhs) {
  84. return lhs.x() == rhs.x() && lhs.y() == rhs.y();
  85. }
  86. inline bool operator!=(const Point& lhs, const Point& rhs) {
  87. return !(lhs == rhs);
  88. }
  89. inline Point operator+(const Point& lhs, const Vector2d& rhs) {
  90. Point result(lhs);
  91. result += rhs;
  92. return result;
  93. }
  94. inline Point operator-(const Point& lhs, const Vector2d& rhs) {
  95. Point result(lhs);
  96. result -= rhs;
  97. return result;
  98. }
  99. inline Vector2d operator-(const Point& lhs, const Point& rhs) {
  100. return Vector2d(base::ClampSub(lhs.x(), rhs.x()),
  101. base::ClampSub(lhs.y(), rhs.y()));
  102. }
  103. inline Point PointAtOffsetFromOrigin(const Vector2d& offset_from_origin) {
  104. return Point(offset_from_origin.x(), offset_from_origin.y());
  105. }
  106. inline Point TransposePoint(const gfx::Point& p) {
  107. return Point(p.y(), p.x());
  108. }
  109. // This is declared here for use in gtest-based unit tests but is defined in
  110. // the //ui/gfx:test_support target. Depend on that to use this in your unit
  111. // test. This should not be used in production code - call ToString() instead.
  112. void PrintTo(const Point& point, ::std::ostream* os);
  113. // Helper methods to scale a gfx::Point to a new gfx::Point.
  114. GEOMETRY_EXPORT Point ScaleToCeiledPoint(const Point& point,
  115. float x_scale,
  116. float y_scale);
  117. GEOMETRY_EXPORT Point ScaleToCeiledPoint(const Point& point, float x_scale);
  118. GEOMETRY_EXPORT Point ScaleToFlooredPoint(const Point& point,
  119. float x_scale,
  120. float y_scale);
  121. GEOMETRY_EXPORT Point ScaleToFlooredPoint(const Point& point, float x_scale);
  122. GEOMETRY_EXPORT Point ScaleToRoundedPoint(const Point& point,
  123. float x_scale,
  124. float y_scale);
  125. GEOMETRY_EXPORT Point ScaleToRoundedPoint(const Point& point, float x_scale);
  126. } // namespace gfx
  127. #endif // UI_GFX_GEOMETRY_POINT_H_