gfx/skia/trunk/src/pathops/SkPathOpsTriangle.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright 2012 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include "SkPathOpsTriangle.h"
michael@0 9
michael@0 10 // http://www.blackpawn.com/texts/pointinpoly/default.html
michael@0 11 // return true if pt is inside triangle; false if outside or on the line
michael@0 12 bool SkDTriangle::contains(const SkDPoint& pt) const {
michael@0 13 // Compute vectors
michael@0 14 SkDVector v0 = fPts[2] - fPts[0];
michael@0 15 SkDVector v1 = fPts[1] - fPts[0];
michael@0 16 SkDVector v2 = pt - fPts[0];
michael@0 17
michael@0 18 // Compute dot products
michael@0 19 double dot00 = v0.dot(v0);
michael@0 20 double dot01 = v0.dot(v1);
michael@0 21 double dot02 = v0.dot(v2);
michael@0 22 double dot11 = v1.dot(v1);
michael@0 23 double dot12 = v1.dot(v2);
michael@0 24
michael@0 25 // original code doesn't handle degenerate input; isn't symmetric with inclusion of corner pts;
michael@0 26 // introduces necessary error with divide; doesn't short circuit on early answer
michael@0 27 #if 0
michael@0 28 // Compute barycentric coordinates
michael@0 29 double invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
michael@0 30 double u = (dot11 * dot02 - dot01 * dot12) * invDenom;
michael@0 31 double v = (dot00 * dot12 - dot01 * dot02) * invDenom;
michael@0 32
michael@0 33 // Check if point is in triangle
michael@0 34 return (u >= 0) && (v >= 0) && (u + v <= 1);
michael@0 35 #else
michael@0 36 double w = dot00 * dot11 - dot01 * dot01;
michael@0 37 if (w == 0) {
michael@0 38 return false;
michael@0 39 }
michael@0 40 double wSign = w < 0 ? -1 : 1;
michael@0 41 double u = (dot11 * dot02 - dot01 * dot12) * wSign;
michael@0 42 if (u <= 0) {
michael@0 43 return false;
michael@0 44 }
michael@0 45 double v = (dot00 * dot12 - dot01 * dot02) * wSign;
michael@0 46 if (v <= 0) {
michael@0 47 return false;
michael@0 48 }
michael@0 49 return u + v < w * wSign;
michael@0 50 #endif
michael@0 51 }

mercurial