quad_f.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_QUAD_F_H_
  5. #define UI_GFX_GEOMETRY_QUAD_F_H_
  6. #include <stddef.h>
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <iosfwd>
  10. #include <string>
  11. #include "base/check_op.h"
  12. #include "ui/gfx/geometry/geometry_export.h"
  13. #include "ui/gfx/geometry/point_f.h"
  14. #include "ui/gfx/geometry/rect_f.h"
  15. namespace gfx {
  16. // A Quad is defined by four corners, allowing it to have edges that are not
  17. // axis-aligned, unlike a Rect.
  18. class GEOMETRY_EXPORT QuadF {
  19. public:
  20. constexpr QuadF() = default;
  21. constexpr QuadF(const PointF& p1,
  22. const PointF& p2,
  23. const PointF& p3,
  24. const PointF& p4)
  25. : p1_(p1), p2_(p2), p3_(p3), p4_(p4) {}
  26. constexpr explicit QuadF(const RectF& rect)
  27. : p1_(rect.x(), rect.y()),
  28. p2_(rect.right(), rect.y()),
  29. p3_(rect.right(), rect.bottom()),
  30. p4_(rect.x(), rect.bottom()) {}
  31. void operator=(const RectF& rect);
  32. void set_p1(const PointF& p) { p1_ = p; }
  33. void set_p2(const PointF& p) { p2_ = p; }
  34. void set_p3(const PointF& p) { p3_ = p; }
  35. void set_p4(const PointF& p) { p4_ = p; }
  36. constexpr const PointF& p1() const { return p1_; }
  37. constexpr const PointF& p2() const { return p2_; }
  38. constexpr const PointF& p3() const { return p3_; }
  39. constexpr const PointF& p4() const { return p4_; }
  40. // Returns true if the quad is an axis-aligned rectangle.
  41. bool IsRectilinear() const;
  42. // Returns true if the points of the quad are in counter-clockwise order. This
  43. // assumes that the quad is convex, and that no three points are collinear.
  44. bool IsCounterClockwise() const;
  45. // Returns true if the |point| is contained within the quad, or lies on on
  46. // edge of the quad. This assumes that the quad is convex.
  47. bool Contains(const PointF& point) const;
  48. // Returns true if the |quad| parameter is contained within |this| quad.
  49. // This method assumes |this| quad is convex. The |quad| parameter has no
  50. // restrictions.
  51. bool ContainsQuad(const QuadF& quad) const;
  52. // Returns a rectangle that bounds the four points of the quad. The points of
  53. // the quad may lie on the right/bottom edge of the resulting rectangle,
  54. // rather than being strictly inside it.
  55. RectF BoundingBox() const {
  56. float rl = std::min({p1_.x(), p2_.x(), p3_.x(), p4_.x()});
  57. float rr = std::max({p1_.x(), p2_.x(), p3_.x(), p4_.x()});
  58. float rt = std::min({p1_.y(), p2_.y(), p3_.y(), p4_.y()});
  59. float rb = std::max({p1_.y(), p2_.y(), p3_.y(), p4_.y()});
  60. return RectF(rl, rt, rr - rl, rb - rt);
  61. }
  62. // Realigns the corners in the quad by rotating them n corners to the right.
  63. void Realign(size_t times) {
  64. DCHECK_LE(times, 4u);
  65. for (size_t i = 0; i < times; ++i) {
  66. PointF temp = p1_;
  67. p1_ = p2_;
  68. p2_ = p3_;
  69. p3_ = p4_;
  70. p4_ = temp;
  71. }
  72. }
  73. // Add a vector to the quad, offseting each point in the quad by the vector.
  74. void operator+=(const Vector2dF& rhs);
  75. // Subtract a vector from the quad, offseting each point in the quad by the
  76. // inverse of the vector.
  77. void operator-=(const Vector2dF& rhs);
  78. // Scale each point in the quad by the |scale| factor.
  79. void Scale(float scale) { Scale(scale, scale); }
  80. // Scale each point in the quad by the scale factors along each axis.
  81. void Scale(float x_scale, float y_scale);
  82. // Tests whether any part of the rectangle intersects with this quad.
  83. // This only works for convex quads.
  84. // This intersection is edge-inclusive and will return true even if the
  85. // intersecting area is empty (i.e., the intersection is a line or a point).
  86. bool IntersectsRect(const RectF&) const;
  87. // Test whether any part of the circle/ellipse intersects with this quad.
  88. // Note that these two functions only work for convex quads.
  89. // These intersections are edge-inclusive and will return true even if the
  90. // intersecting area is empty (i.e., the intersection is a line or a point).
  91. bool IntersectsCircle(const PointF& center, float radius) const;
  92. bool IntersectsEllipse(const PointF& center, const SizeF& radii) const;
  93. // The center of the quad. If the quad is the result of a affine-transformed
  94. // rectangle this is the same as the original center transformed.
  95. PointF CenterPoint() const {
  96. return PointF((p1_.x() + p2_.x() + p3_.x() + p4_.x()) / 4.0,
  97. (p1_.y() + p2_.y() + p3_.y() + p4_.y()) / 4.0);
  98. }
  99. // Returns a string representation of quad.
  100. std::string ToString() const;
  101. private:
  102. PointF p1_;
  103. PointF p2_;
  104. PointF p3_;
  105. PointF p4_;
  106. };
  107. inline bool operator==(const QuadF& lhs, const QuadF& rhs) {
  108. return
  109. lhs.p1() == rhs.p1() && lhs.p2() == rhs.p2() &&
  110. lhs.p3() == rhs.p3() && lhs.p4() == rhs.p4();
  111. }
  112. inline bool operator!=(const QuadF& lhs, const QuadF& rhs) {
  113. return !(lhs == rhs);
  114. }
  115. // Add a vector to a quad, offseting each point in the quad by the vector.
  116. GEOMETRY_EXPORT QuadF operator+(const QuadF& lhs, const Vector2dF& rhs);
  117. // Subtract a vector from a quad, offseting each point in the quad by the
  118. // inverse of the vector.
  119. GEOMETRY_EXPORT QuadF operator-(const QuadF& lhs, const Vector2dF& rhs);
  120. // This is declared here for use in gtest-based unit tests but is defined in
  121. // the //ui/gfx:test_support target. Depend on that to use this in your unit
  122. // test. This should not be used in production code - call ToString() instead.
  123. void PrintTo(const QuadF& quad, ::std::ostream* os);
  124. } // namespace gfx
  125. #endif // UI_GFX_GEOMETRY_QUAD_F_H_