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_QUAD_F_H_ 6 #define UI_GFX_QUAD_F_H_ 7 8 #include <cmath> 9 #include <string> 10 11 #include "ui/base/ui_export.h" 12 #include "ui/gfx/point_f.h" 13 #include "ui/gfx/rect_f.h" 14 15 namespace gfx { 16 17 // A Quad is defined by four corners, allowing it to have edges that are not 18 // axis-aligned, unlike a Rect. 19 class UI_EXPORT QuadF { 20 public: 21 QuadF() {} 22 QuadF(const PointF& p1, const PointF& p2, const PointF& p3, const PointF& p4) 23 : p1_(p1), 24 p2_(p2), 25 p3_(p3), 26 p4_(p4) {} 27 28 explicit QuadF(const RectF& rect) 29 : p1_(rect.x(), rect.y()), 30 p2_(rect.right(), rect.y()), 31 p3_(rect.right(), rect.bottom()), 32 p4_(rect.x(), rect.bottom()) {} 33 34 void operator=(const RectF& rect); 35 36 void set_p1(const PointF& p) { p1_ = p; } 37 void set_p2(const PointF& p) { p2_ = p; } 38 void set_p3(const PointF& p) { p3_ = p; } 39 void set_p4(const PointF& p) { p4_ = p; } 40 41 const PointF& p1() const { return p1_; } 42 const PointF& p2() const { return p2_; } 43 const PointF& p3() const { return p3_; } 44 const PointF& p4() const { return p4_; } 45 46 // Returns true if the quad is an axis-aligned rectangle. 47 bool IsRectilinear() const; 48 49 // Returns true if the points of the quad are in counter-clockwise order. This 50 // assumes that the quad is convex, and that no three points are collinear. 51 bool IsCounterClockwise() const; 52 53 // Returns true if the |point| is contained within the quad, or lies on on 54 // edge of the quad. 55 bool Contains(const gfx::PointF& point) const; 56 57 // Returns a rectangle that bounds the four points of the quad. The points of 58 // the quad may lie on the right/bottom edge of the resulting rectangle, 59 // rather than being strictly inside it. 60 RectF BoundingBox() const { 61 float rl = std::min(std::min(p1_.x(), p2_.x()), std::min(p3_.x(), p4_.x())); 62 float rr = std::max(std::max(p1_.x(), p2_.x()), std::max(p3_.x(), p4_.x())); 63 float rt = std::min(std::min(p1_.y(), p2_.y()), std::min(p3_.y(), p4_.y())); 64 float rb = std::max(std::max(p1_.y(), p2_.y()), std::max(p3_.y(), p4_.y())); 65 return RectF(rl, rt, rr - rl, rb - rt); 66 } 67 68 // Add a vector to the quad, offseting each point in the quad by the vector. 69 void operator+=(const Vector2dF& rhs); 70 // Subtract a vector from the quad, offseting each point in the quad by the 71 // inverse of the vector. 72 void operator-=(const Vector2dF& rhs); 73 74 // Scale each point in the quad by the |scale| factor. 75 void Scale(float scale) { Scale(scale, scale); } 76 77 // Scale each point in the quad by the scale factors along each axis. 78 void Scale(float x_scale, float y_scale); 79 80 // Returns a string representation of quad. 81 std::string ToString() const; 82 83 private: 84 PointF p1_; 85 PointF p2_; 86 PointF p3_; 87 PointF p4_; 88 }; 89 90 inline bool operator==(const QuadF& lhs, const QuadF& rhs) { 91 return 92 lhs.p1() == rhs.p1() && lhs.p2() == rhs.p2() && 93 lhs.p3() == rhs.p3() && lhs.p4() == rhs.p4(); 94 } 95 96 inline bool operator!=(const QuadF& lhs, const QuadF& rhs) { 97 return !(lhs == rhs); 98 } 99 100 // Add a vector to a quad, offseting each point in the quad by the vector. 101 UI_EXPORT QuadF operator+(const QuadF& lhs, const Vector2dF& rhs); 102 // Subtract a vector from a quad, offseting each point in the quad by the 103 // inverse of the vector. 104 UI_EXPORT QuadF operator-(const QuadF& lhs, const Vector2dF& rhs); 105 106 } // namespace gfx 107 108 #endif // UI_GFX_QUAD_F_H_ 109