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