• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ui/gfx/geometry/quad_f.h"
6 
7 #include <limits>
8 
9 #include "base/strings/stringprintf.h"
10 
11 namespace gfx {
12 
operator =(const RectF & rect)13 void QuadF::operator=(const RectF& rect) {
14   p1_ = PointF(rect.x(), rect.y());
15   p2_ = PointF(rect.right(), rect.y());
16   p3_ = PointF(rect.right(), rect.bottom());
17   p4_ = PointF(rect.x(), rect.bottom());
18 }
19 
ToString() const20 std::string QuadF::ToString() const {
21   return base::StringPrintf("%s;%s;%s;%s",
22                             p1_.ToString().c_str(),
23                             p2_.ToString().c_str(),
24                             p3_.ToString().c_str(),
25                             p4_.ToString().c_str());
26 }
27 
WithinEpsilon(float a,float b)28 static inline bool WithinEpsilon(float a, float b) {
29   return std::abs(a - b) < std::numeric_limits<float>::epsilon();
30 }
31 
IsRectilinear() const32 bool QuadF::IsRectilinear() const {
33   return
34       (WithinEpsilon(p1_.x(), p2_.x()) && WithinEpsilon(p2_.y(), p3_.y()) &&
35        WithinEpsilon(p3_.x(), p4_.x()) && WithinEpsilon(p4_.y(), p1_.y())) ||
36       (WithinEpsilon(p1_.y(), p2_.y()) && WithinEpsilon(p2_.x(), p3_.x()) &&
37        WithinEpsilon(p3_.y(), p4_.y()) && WithinEpsilon(p4_.x(), p1_.x()));
38 }
39 
IsCounterClockwise() const40 bool QuadF::IsCounterClockwise() const {
41   // This math computes the signed area of the quad. Positive area
42   // indicates the quad is clockwise; negative area indicates the quad is
43   // counter-clockwise. Note carefully: this is backwards from conventional
44   // math because our geometric space uses screen coordiantes with y-axis
45   // pointing downards.
46   // Reference: http://mathworld.wolfram.com/PolygonArea.html.
47   // The equation can be written:
48   // Signed area = determinant1 + determinant2 + determinant3 + determinant4
49   // In practise, Refactoring the computation of adding determinants so that
50   // reducing the number of operations. The equation is:
51   // Signed area = element1 + element2 - element3 - element4
52 
53   float p24 = p2_.y() - p4_.y();
54   float p31 = p3_.y() - p1_.y();
55 
56   // Up-cast to double so this cannot overflow.
57   double element1 = static_cast<double>(p1_.x()) * p24;
58   double element2 = static_cast<double>(p2_.x()) * p31;
59   double element3 = static_cast<double>(p3_.x()) * p24;
60   double element4 = static_cast<double>(p4_.x()) * p31;
61 
62   return element1 + element2 < element3 + element4;
63 }
64 
PointIsInTriangle(const PointF & point,const PointF & r1,const PointF & r2,const PointF & r3)65 static inline bool PointIsInTriangle(const PointF& point,
66                                      const PointF& r1,
67                                      const PointF& r2,
68                                      const PointF& r3) {
69   // Translate point and triangle so that point lies at origin.
70   // Then checking if the origin is contained in the translated triangle.
71   // The origin O lies inside ABC if and only if the triangles OAB, OBC,
72   // and OCA are all either clockwise or counterclockwise.
73   // This algorithm is from Real-Time Collision Detection (Chaper 5.4.2).
74 
75   Vector2dF a = r1 - point;
76   Vector2dF b = r2 - point;
77   Vector2dF c = r3 - point;
78 
79   double u = CrossProduct(b, c);
80   double v = CrossProduct(c, a);
81   double w = CrossProduct(a, b);
82   return ((u * v < 0) || ((u * w) < 0) || ((v * w) < 0)) ? false : true;
83 }
84 
Contains(const PointF & point) const85 bool QuadF::Contains(const PointF& point) const {
86   return PointIsInTriangle(point, p1_, p2_, p3_)
87       || PointIsInTriangle(point, p1_, p3_, p4_);
88 }
89 
Scale(float x_scale,float y_scale)90 void QuadF::Scale(float x_scale, float y_scale) {
91   p1_.Scale(x_scale, y_scale);
92   p2_.Scale(x_scale, y_scale);
93   p3_.Scale(x_scale, y_scale);
94   p4_.Scale(x_scale, y_scale);
95 }
96 
operator +=(const Vector2dF & rhs)97 void QuadF::operator+=(const Vector2dF& rhs) {
98   p1_ += rhs;
99   p2_ += rhs;
100   p3_ += rhs;
101   p4_ += rhs;
102 }
103 
operator -=(const Vector2dF & rhs)104 void QuadF::operator-=(const Vector2dF& rhs) {
105   p1_ -= rhs;
106   p2_ -= rhs;
107   p3_ -= rhs;
108   p4_ -= rhs;
109 }
110 
operator +(const QuadF & lhs,const Vector2dF & rhs)111 QuadF operator+(const QuadF& lhs, const Vector2dF& rhs) {
112   QuadF result = lhs;
113   result += rhs;
114   return result;
115 }
116 
operator -(const QuadF & lhs,const Vector2dF & rhs)117 QuadF operator-(const QuadF& lhs, const Vector2dF& rhs) {
118   QuadF result = lhs;
119   result -= rhs;
120   return result;
121 }
122 
123 }  // namespace gfx
124