Home > matGeom > geom2d > isPointInTriangle.m

isPointInTriangle

PURPOSE ^

ISPOINTINTRIANGLE Test if a point is located inside a triangle.

SYNOPSIS ^

function b = isPointInTriangle(point, p1, p2, p3)

DESCRIPTION ^

ISPOINTINTRIANGLE Test if a point is located inside a triangle.

   B = isPointInTriangle(POINT, V1, V2, V3)
   POINT is a 1-by-2 row vector containing coordinates of the test point,
   V1, V2 and V3 are 1-by-2 row vectors containing coordinates of triangle
   vertices. The function returns 1 is the point is inside or on the
   boundary of the triangle, and 0 otherwise.

   B = isPointInTriangle(POINT, VERTICES)
   Specifiy the coordinates of vertices as a 3-by-2 array.

   If POINT contains more than one row, the result B has as many rows as
   the input POINT.


   Example
     % vertices of the triangle
     p1 = [0 0];
     p2 = [10 0];
     p3 = [5 10];
     tri = [p1;p2;p3];
     % check if points are inside
     isPointInTriangle([0 0], tri)
     ans =
         1
     isPointInTriangle([5 5], tri)
     ans =
         1
     isPointInTriangle([10 5], tri)
     ans =
         0
     % check for an array of points
     isPointInTriangle([0 0;1 0;0 1], tri)
     ans =
         1
         1
         0

   See also
   polygons2d, isPointInPolygon, isCounterClockwise


 ------
 Author: David Legland
 e-mail: david.legland@grignon.inra.fr
 Created: 2011-05-16,    using Matlab 7.9.0.529 (R2009b)
 Copyright 2011 INRA - Cepia Software Platform.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function b = isPointInTriangle(point, p1, p2, p3)
0002 %ISPOINTINTRIANGLE Test if a point is located inside a triangle.
0003 %
0004 %   B = isPointInTriangle(POINT, V1, V2, V3)
0005 %   POINT is a 1-by-2 row vector containing coordinates of the test point,
0006 %   V1, V2 and V3 are 1-by-2 row vectors containing coordinates of triangle
0007 %   vertices. The function returns 1 is the point is inside or on the
0008 %   boundary of the triangle, and 0 otherwise.
0009 %
0010 %   B = isPointInTriangle(POINT, VERTICES)
0011 %   Specifiy the coordinates of vertices as a 3-by-2 array.
0012 %
0013 %   If POINT contains more than one row, the result B has as many rows as
0014 %   the input POINT.
0015 %
0016 %
0017 %   Example
0018 %     % vertices of the triangle
0019 %     p1 = [0 0];
0020 %     p2 = [10 0];
0021 %     p3 = [5 10];
0022 %     tri = [p1;p2;p3];
0023 %     % check if points are inside
0024 %     isPointInTriangle([0 0], tri)
0025 %     ans =
0026 %         1
0027 %     isPointInTriangle([5 5], tri)
0028 %     ans =
0029 %         1
0030 %     isPointInTriangle([10 5], tri)
0031 %     ans =
0032 %         0
0033 %     % check for an array of points
0034 %     isPointInTriangle([0 0;1 0;0 1], tri)
0035 %     ans =
0036 %         1
0037 %         1
0038 %         0
0039 %
0040 %   See also
0041 %   polygons2d, isPointInPolygon, isCounterClockwise
0042 %
0043 %
0044 % ------
0045 % Author: David Legland
0046 % e-mail: david.legland@grignon.inra.fr
0047 % Created: 2011-05-16,    using Matlab 7.9.0.529 (R2009b)
0048 % Copyright 2011 INRA - Cepia Software Platform.
0049 
0050 % if triangle vertices are given as a single array, extract vertices
0051 if nargin == 2
0052     p2 = p1(2, :);
0053     p3 = p1(3, :);
0054     p1 = p1(1, :);
0055 end
0056 
0057 % check triangle orientation
0058 isDirect = isCounterClockwise(p1, p2, p3);
0059 
0060 % check location of point with respect to each side
0061 if isDirect
0062     b12 = isCounterClockwise(p1, p2, point) >= 0;
0063     b23 = isCounterClockwise(p2, p3, point) >= 0;
0064     b31 = isCounterClockwise(p3, p1, point) >= 0;
0065 else
0066     b12 = isCounterClockwise(p1, p2, point) <= 0;
0067     b23 = isCounterClockwise(p2, p3, point) <= 0;
0068     b31 = isCounterClockwise(p3, p1, point) <= 0;
0069 end
0070 
0071 % combines the 3 results
0072 b = b12 & b23 & b31;
0073

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019