Skip to content

Commit 895f7b0

Browse files
committed
Updated ContainsArray to include the returnFirst argument.
1 parent b7c895c commit 895f7b0

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

v3/src/geom/triangle/ContainsArray.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// http://www.blackpawn.com/texts/pointinpoly/
22

33
// points is an array of Point-like objects with public x/y properties
4-
// returns an array with the same number of elements as the points array
5-
// each element contains either true or false depending if the point lay in the Triangle or not.
4+
// returns an array containing all points that are within the triangle, or an empty array if none
5+
// if 'returnFirst' is true it will return after the first point within the triangle is found
66

7-
var ContainsArray = function (triangle, points, out)
7+
var ContainsArray = function (triangle, points, returnFirst, out)
88
{
9+
if (returnFirst === undefined) { returnFirst = false; }
910
if (out === undefined) { out = []; }
1011

1112
var v0x = triangle.x3 - triangle.x1;
@@ -43,7 +44,15 @@ var ContainsArray = function (triangle, points, out)
4344
u = ((dot11 * dot02) - (dot01 * dot12)) * inv;
4445
v = ((dot00 * dot12) - (dot01 * dot02)) * inv;
4546

46-
out.push(u >= 0 && v >= 0 && (u + v < 1));
47+
if (u >= 0 && v >= 0 && (u + v < 1))
48+
{
49+
out.push({ x: points[i].x, y: points[i].y });
50+
51+
if (returnFirst)
52+
{
53+
break;
54+
}
55+
}
4756
}
4857

4958
return out;

0 commit comments

Comments
 (0)