OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/geometry/quad_f.h" | 5 #include "ui/gfx/geometry/quad_f.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 | 10 |
(...skipping 25 matching lines...) Expand all Loading... |
36 (WithinEpsilon(p1_.y(), p2_.y()) && WithinEpsilon(p2_.x(), p3_.x()) && | 36 (WithinEpsilon(p1_.y(), p2_.y()) && WithinEpsilon(p2_.x(), p3_.x()) && |
37 WithinEpsilon(p3_.y(), p4_.y()) && WithinEpsilon(p4_.x(), p1_.x())); | 37 WithinEpsilon(p3_.y(), p4_.y()) && WithinEpsilon(p4_.x(), p1_.x())); |
38 } | 38 } |
39 | 39 |
40 bool QuadF::IsCounterClockwise() const { | 40 bool QuadF::IsCounterClockwise() const { |
41 // This math computes the signed area of the quad. Positive area | 41 // This math computes the signed area of the quad. Positive area |
42 // indicates the quad is clockwise; negative area indicates the quad is | 42 // indicates the quad is clockwise; negative area indicates the quad is |
43 // counter-clockwise. Note carefully: this is backwards from conventional | 43 // counter-clockwise. Note carefully: this is backwards from conventional |
44 // math because our geometric space uses screen coordiantes with y-axis | 44 // math because our geometric space uses screen coordiantes with y-axis |
45 // pointing downards. | 45 // pointing downards. |
46 // Reference: http://mathworld.wolfram.com/PolygonArea.html | 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(); |
47 | 55 |
48 // Up-cast to double so this cannot overflow. | 56 // Up-cast to double so this cannot overflow. |
49 double determinant1 = static_cast<double>(p1_.x()) * p2_.y() | 57 double element1 = static_cast<double>(p1_.x()) * p24; |
50 - static_cast<double>(p2_.x()) * p1_.y(); | 58 double element2 = static_cast<double>(p2_.x()) * p31; |
51 double determinant2 = static_cast<double>(p2_.x()) * p3_.y() | 59 double element3 = static_cast<double>(p3_.x()) * p24; |
52 - static_cast<double>(p3_.x()) * p2_.y(); | 60 double element4 = static_cast<double>(p4_.x()) * p31; |
53 double determinant3 = static_cast<double>(p3_.x()) * p4_.y() | |
54 - static_cast<double>(p4_.x()) * p3_.y(); | |
55 double determinant4 = static_cast<double>(p4_.x()) * p1_.y() | |
56 - static_cast<double>(p1_.x()) * p4_.y(); | |
57 | 61 |
58 return determinant1 + determinant2 + determinant3 + determinant4 < 0; | 62 return element1 + element2 < element3 + element4; |
59 } | 63 } |
60 | 64 |
61 static inline bool PointIsInTriangle(const PointF& point, | 65 static inline bool PointIsInTriangle(const PointF& point, |
62 const PointF& r1, | 66 const PointF& r1, |
63 const PointF& r2, | 67 const PointF& r2, |
64 const PointF& r3) { | 68 const PointF& r3) { |
65 // Compute the barycentric coordinates (u, v, w) of |point| relative to the | 69 // Compute the barycentric coordinates (u, v, w) of |point| relative to the |
66 // triangle (r1, r2, r3) by the solving the system of equations: | 70 // triangle (r1, r2, r3) by the solving the system of equations: |
67 // 1) point = u * r1 + v * r2 + w * r3 | 71 // 1) point = u * r1 + v * r2 + w * r3 |
68 // 2) u + v + w = 1 | 72 // 2) u + v + w = 1 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 return result; | 118 return result; |
115 } | 119 } |
116 | 120 |
117 QuadF operator-(const QuadF& lhs, const Vector2dF& rhs) { | 121 QuadF operator-(const QuadF& lhs, const Vector2dF& rhs) { |
118 QuadF result = lhs; | 122 QuadF result = lhs; |
119 result -= rhs; | 123 result -= rhs; |
120 return result; | 124 return result; |
121 } | 125 } |
122 | 126 |
123 } // namespace gfx | 127 } // namespace gfx |
OLD | NEW |