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