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