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