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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 double determinant4 = static_cast<double>(p4_.x()) * p1_.y() | 55 double determinant4 = static_cast<double>(p4_.x()) * p1_.y() |
56 - static_cast<double>(p1_.x()) * p4_.y(); | 56 - static_cast<double>(p1_.x()) * p4_.y(); |
57 | 57 |
58 return determinant1 + determinant2 + determinant3 + determinant4 < 0; | 58 return determinant1 + determinant2 + determinant3 + determinant4 < 0; |
59 } | 59 } |
60 | 60 |
61 static inline bool PointIsInTriangle(const PointF& point, | 61 static inline bool PointIsInTriangle(const PointF& point, |
62 const PointF& r1, | 62 const PointF& r1, |
63 const PointF& r2, | 63 const PointF& r2, |
64 const PointF& r3) { | 64 const PointF& r3) { |
65 // Compute the barycentric coordinates of |point| relative to the triangle | 65 // Compute the barycentric coordinates (u, v, w) of |point| relative to the |
66 // (r1, r2, r3). This algorithm comes from Christer Ericson's Real-Time | 66 // triangle (r1, r2, r3) by the solving the system of equations: |
67 // Collision Detection. | 67 // 1) point = u * r1 + v * r2 + w * r3 |
68 Vector2dF v0 = r2 - r1; | 68 // 2) u + v + w = 1 |
69 Vector2dF v1 = r3 - r1; | 69 // This algorithm comes from Christer Ericson's Real-Time Collision Detection. |
70 Vector2dF v2 = point - r1; | |
71 | 70 |
72 double dot00 = DotProduct(v0, v0); | 71 Vector2dF r31 = r1 - r3; |
73 double dot01 = DotProduct(v0, v1); | 72 Vector2dF r32 = r2 - r3; |
74 double dot11 = DotProduct(v1, v1); | 73 Vector2dF r3p = point - r3; |
75 double dot20 = DotProduct(v2, v0); | |
76 double dot21 = DotProduct(v2, v1); | |
77 | 74 |
78 double denom = dot00 * dot11 - dot01 * dot01; | 75 float denom = r32.y() * r31.x() - r32.x() * r31.y(); |
79 | 76 float u = (r32.y() * r3p.x() - r32.x() * r3p.y()) / denom; |
80 double v = (dot11 * dot20 - dot01 * dot21) / denom; | 77 float v = (r31.x() * r3p.y() - r31.y() * r3p.x()) / denom; |
81 double w = (dot00 * dot21 - dot01 * dot20) / denom; | 78 float w = 1.f - u - v; |
82 double u = 1 - v - w; | |
83 | 79 |
84 // Use the barycentric coordinates to test if |point| is inside the | 80 // Use the barycentric coordinates to test if |point| is inside the |
85 // triangle (r1, r2, r2). | 81 // triangle (r1, r2, r2). |
86 return (v >= 0) && (w >= 0) && (u >= 0); | 82 return (u >= 0) && (v >= 0) && (w >= 0); |
87 } | 83 } |
88 | 84 |
89 bool QuadF::Contains(const PointF& point) const { | 85 bool QuadF::Contains(const PointF& point) const { |
90 return PointIsInTriangle(point, p1_, p2_, p3_) | 86 return PointIsInTriangle(point, p1_, p2_, p3_) |
91 || PointIsInTriangle(point, p1_, p3_, p4_); | 87 || PointIsInTriangle(point, p1_, p3_, p4_); |
92 } | 88 } |
93 | 89 |
94 void QuadF::Scale(float x_scale, float y_scale) { | 90 void QuadF::Scale(float x_scale, float y_scale) { |
95 p1_.Scale(x_scale, y_scale); | 91 p1_.Scale(x_scale, y_scale); |
96 p2_.Scale(x_scale, y_scale); | 92 p2_.Scale(x_scale, y_scale); |
(...skipping 21 matching lines...) Expand all Loading... |
118 return result; | 114 return result; |
119 } | 115 } |
120 | 116 |
121 QuadF operator-(const QuadF& lhs, const Vector2dF& rhs) { | 117 QuadF operator-(const QuadF& lhs, const Vector2dF& rhs) { |
122 QuadF result = lhs; | 118 QuadF result = lhs; |
123 result -= rhs; | 119 result -= rhs; |
124 return result; | 120 return result; |
125 } | 121 } |
126 | 122 |
127 } // namespace gfx | 123 } // namespace gfx |
OLD | NEW |