1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/pathops/SkPathOpsTriangle.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,51 @@ 1.4 +/* 1.5 + * Copyright 2012 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#include "SkPathOpsTriangle.h" 1.12 + 1.13 +// http://www.blackpawn.com/texts/pointinpoly/default.html 1.14 +// return true if pt is inside triangle; false if outside or on the line 1.15 +bool SkDTriangle::contains(const SkDPoint& pt) const { 1.16 +// Compute vectors 1.17 + SkDVector v0 = fPts[2] - fPts[0]; 1.18 + SkDVector v1 = fPts[1] - fPts[0]; 1.19 + SkDVector v2 = pt - fPts[0]; 1.20 + 1.21 +// Compute dot products 1.22 + double dot00 = v0.dot(v0); 1.23 + double dot01 = v0.dot(v1); 1.24 + double dot02 = v0.dot(v2); 1.25 + double dot11 = v1.dot(v1); 1.26 + double dot12 = v1.dot(v2); 1.27 + 1.28 +// original code doesn't handle degenerate input; isn't symmetric with inclusion of corner pts; 1.29 +// introduces necessary error with divide; doesn't short circuit on early answer 1.30 +#if 0 1.31 +// Compute barycentric coordinates 1.32 + double invDenom = 1 / (dot00 * dot11 - dot01 * dot01); 1.33 + double u = (dot11 * dot02 - dot01 * dot12) * invDenom; 1.34 + double v = (dot00 * dot12 - dot01 * dot02) * invDenom; 1.35 + 1.36 +// Check if point is in triangle 1.37 + return (u >= 0) && (v >= 0) && (u + v <= 1); 1.38 +#else 1.39 + double w = dot00 * dot11 - dot01 * dot01; 1.40 + if (w == 0) { 1.41 + return false; 1.42 + } 1.43 + double wSign = w < 0 ? -1 : 1; 1.44 + double u = (dot11 * dot02 - dot01 * dot12) * wSign; 1.45 + if (u <= 0) { 1.46 + return false; 1.47 + } 1.48 + double v = (dot00 * dot12 - dot01 * dot02) * wSign; 1.49 + if (v <= 0) { 1.50 + return false; 1.51 + } 1.52 + return u + v < w * wSign; 1.53 +#endif 1.54 +}