blob: d7950645ce28f6366e5d147ba92e78852c831dd8 [file] [log] [blame]
ben@chromium.org8e7c15d2013-12-19 06:01:15 +09001// 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>
jamesr@chromium.org16cdf9c2014-07-10 16:41:43 +090010#include <iosfwd>
ben@chromium.org8e7c15d2013-12-19 06:01:15 +090011#include <string>
12
awoloszynde634522015-03-12 23:38:32 +090013#include "base/logging.h"
ben@chromium.org8e7c15d2013-12-19 06:01:15 +090014#include "ui/gfx/geometry/point_f.h"
15#include "ui/gfx/geometry/rect_f.h"
16#include "ui/gfx/gfx_export.h"
17
18namespace gfx {
19
20// A Quad is defined by four corners, allowing it to have edges that are not
21// axis-aligned, unlike a Rect.
22class 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) {}
30
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()) {}
36
37 void operator=(const RectF& rect);
38
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; }
43
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_; }
48
49 // Returns true if the quad is an axis-aligned rectangle.
50 bool IsRectilinear() const;
51
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;
55
56 // Returns true if the |point| is contained within the quad, or lies on on
jdduke@chromium.orga55f8c02014-05-17 05:13:54 +090057 // edge of the quad. This assumes that the quad is convex.
ben@chromium.org8e7c15d2013-12-19 06:01:15 +090058 bool Contains(const gfx::PointF& point) const;
59
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);
69 }
70
awoloszynde634522015-03-12 23:38:32 +090071 // 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;
80 }
81 }
82
ben@chromium.org8e7c15d2013-12-19 06:01:15 +090083 // 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);
88
89 // Scale each point in the quad by the |scale| factor.
90 void Scale(float scale) { Scale(scale, scale); }
91
92 // Scale each point in the quad by the scale factors along each axis.
93 void Scale(float x_scale, float y_scale);
94
95 // Returns a string representation of quad.
96 std::string ToString() const;
97
98 private:
99 PointF p1_;
100 PointF p2_;
101 PointF p3_;
102 PointF p4_;
103};
104
105inline 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();
109}
110
111inline bool operator!=(const QuadF& lhs, const QuadF& rhs) {
112 return !(lhs == rhs);
113}
114
115// Add a vector to a quad, offseting each point in the quad by the vector.
116GFX_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.
119GFX_EXPORT QuadF operator-(const QuadF& lhs, const Vector2dF& rhs);
120
jamesr@chromium.org16cdf9c2014-07-10 16:41:43 +0900121// 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.
124void PrintTo(const QuadF& quad, ::std::ostream* os);
125
ben@chromium.org8e7c15d2013-12-19 06:01:15 +0900126} // namespace gfx
127
128#endif // UI_GFX_GEOMETRY_QUAD_F_H_