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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 of |point| relative to the triangle |
66 // (r1, r2, r3). This algorithm comes from Christer Ericson's Real-Time | 66 // (r1, r2, r3). This algorithm comes from Christer Ericson's Real-Time |
danakj
2014/05/16 15:43:44
Can you update this comment with a citation on the
jdduke (slow)
2014/05/16 16:49:47
Hmm, the algorithm is the same (barycentric coords
danakj
2014/05/16 17:01:48
Ahhh yeh ok, showing the equation we're solving so
jdduke (slow)
2014/05/16 17:30:32
Done.
| |
67 // Collision Detection. | 67 // Collision Detection. |
68 Vector2dF v0 = r2 - r1; | 68 Vector2dF r31 = r1 - r3; |
69 Vector2dF v1 = r3 - r1; | 69 Vector2dF r32 = r2 - r3; |
70 Vector2dF v2 = point - r1; | 70 Vector2dF r3p = point - r3; |
71 | 71 |
72 double dot00 = DotProduct(v0, v0); | 72 float denom = r32.y() * r31.x() - r32.x() * r31.y(); |
73 double dot01 = DotProduct(v0, v1); | 73 float v = (r32.y() * r3p.x() - r32.x() * r3p.y()) / denom; |
74 double dot11 = DotProduct(v1, v1); | 74 float w = (r31.x() * r3p.y() - r31.y() * r3p.x()) / denom; |
75 double dot20 = DotProduct(v2, v0); | 75 float u = 1.f - v - w; |
76 double dot21 = DotProduct(v2, v1); | |
77 | |
78 double denom = dot00 * dot11 - dot01 * dot01; | |
79 | |
80 double v = (dot11 * dot20 - dot01 * dot21) / denom; | |
81 double w = (dot00 * dot21 - dot01 * dot20) / denom; | |
82 double u = 1 - v - w; | |
83 | 76 |
84 // Use the barycentric coordinates to test if |point| is inside the | 77 // Use the barycentric coordinates to test if |point| is inside the |
85 // triangle (r1, r2, r2). | 78 // triangle (r1, r2, r2). |
86 return (v >= 0) && (w >= 0) && (u >= 0); | 79 return (v >= 0) && (w >= 0) && (u >= 0); |
87 } | 80 } |
88 | 81 |
89 bool QuadF::Contains(const PointF& point) const { | 82 bool QuadF::Contains(const PointF& point) const { |
90 return PointIsInTriangle(point, p1_, p2_, p3_) | 83 return PointIsInTriangle(point, p1_, p2_, p3_) |
91 || PointIsInTriangle(point, p1_, p3_, p4_); | 84 || PointIsInTriangle(point, p1_, p3_, p4_); |
92 } | 85 } |
(...skipping 25 matching lines...) Expand all Loading... | |
118 return result; | 111 return result; |
119 } | 112 } |
120 | 113 |
121 QuadF operator-(const QuadF& lhs, const Vector2dF& rhs) { | 114 QuadF operator-(const QuadF& lhs, const Vector2dF& rhs) { |
122 QuadF result = lhs; | 115 QuadF result = lhs; |
123 result -= rhs; | 116 result -= rhs; |
124 return result; | 117 return result; |
125 } | 118 } |
126 | 119 |
127 } // namespace gfx | 120 } // namespace gfx |
OLD | NEW |