aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/Intersection/TriangleUtilities.cpp
diff options
context:
space:
mode:
authorGravatar caryclark@google.com <caryclark@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-02-07 13:13:41 +0000
committerGravatar caryclark@google.com <caryclark@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-02-07 13:13:41 +0000
commitbeda389e646d6be3cfef853584a78ca8ba39d0fc (patch)
tree2233d325407b886bfaf69ad94efed94a4b30f9fb /experimental/Intersection/TriangleUtilities.cpp
parent709906b74dc0179609e9f1455dc6e9e13675c0fa (diff)
shape ops work in progress
git-svn-id: http://skia.googlecode.com/svn/trunk@7637 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'experimental/Intersection/TriangleUtilities.cpp')
-rw-r--r--experimental/Intersection/TriangleUtilities.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/experimental/Intersection/TriangleUtilities.cpp b/experimental/Intersection/TriangleUtilities.cpp
new file mode 100644
index 0000000000..38a691646c
--- /dev/null
+++ b/experimental/Intersection/TriangleUtilities.cpp
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "TriangleUtilities.h"
+
+// http://www.blackpawn.com/texts/pointinpoly/default.html
+bool pointInTriangle(const Triangle& triangle, const _Point& pt) {
+// Compute vectors
+ _Point v0 = triangle[2] - triangle[0];
+ _Point v1 = triangle[1] - triangle[0];
+ _Point v2 = pt - triangle[0];
+
+// Compute dot products
+ double dot00 = v0.dot(v0);
+ double dot01 = v0.dot(v1);
+ double dot02 = v0.dot(v2);
+ double dot11 = v1.dot(v1);
+ double dot12 = v1.dot(v2);
+
+// Compute barycentric coordinates
+ double invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
+ double u = (dot11 * dot02 - dot01 * dot12) * invDenom;
+ double v = (dot00 * dot12 - dot01 * dot02) * invDenom;
+
+// Check if point is in triangle
+ return (u >= 0) && (v >= 0) && (u + v < 1);
+}
+