Home > matGeom > geom2d > isPointOnEdge.m

isPointOnEdge

PURPOSE ^

ISPOINTONEDGE Test if a point belongs to an edge.

SYNOPSIS ^

function b = isPointOnEdge(point, edge, varargin)

DESCRIPTION ^

ISPOINTONEDGE Test if a point belongs to an edge.

   Usage
   B = isPointOnEdge(POINT, EDGE)
   B = isPointOnEdge(POINT, EDGE, TOL)

   Description
   B = isPointOnEdge(POINT, EDGE)
   with POINT being [xp yp], and EDGE being [x1 y1 x2 y2], returns TRUE if
   the point is located on the edge, and FALSE otherwise.

   B = isPointOnEdge(POINT, EDGE, TOL)
   Specify an optilonal tolerance value TOL. The tolerance is given as a
   fraction of the norm of the edge direction vector. Default is 1e-14. 

   B = isPointOnEdge(POINTARRAY, EDGE)
   B = isPointOnEdge(POINT, EDGEARRAY)
   When one of the inputs has several rows, return the result of the test
   for each element of the array tested against the single parameter.

   B = isPointOnEdge(POINTARRAY, EDGEARRAY)
   When both POINTARRAY and EDGEARRAY have the same number of rows,
   returns a column vector with the same number of rows.
   When the number of rows are different and both greater than 1, returns
   a Np-by-Ne matrix of booleans, containing the result for each couple of
   point and edge.

   Examples
   % create a point array
   points = [10 10;15 10; 30 10];
   % create an edge array
   vertices = [10 10;20 10;20 20;10 20];
   edges = [vertices vertices([2:end 1], :)];

   % Test one point and one edge
   isPointOnEdge(points(1,:), edges(1,:))
   ans = 
       1
   isPointOnEdge(points(3,:), edges(1,:))
   ans = 
       0

   % Test one point and several edges
   isPointOnEdge(points(1,:), edges)'
   ans =
        1     0     0     1

   % Test several points and one edge
   isPointOnEdge(points, edges(1,:))'
   ans =
        1     1     0

   % Test N points and N edges
   isPointOnEdge(points, edges(1:3,:))'
   ans =
        1     0     0

   % Test NP points and NE edges
   isPointOnEdge(points, edges)
   ans =
        1     0     0     1
        1     0     0     0
        0     0     0     0


   See also
   edges2d, points2d, isPointOnLine

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function b = isPointOnEdge(point, edge, varargin)
0002 %ISPOINTONEDGE Test if a point belongs to an edge.
0003 %
0004 %   Usage
0005 %   B = isPointOnEdge(POINT, EDGE)
0006 %   B = isPointOnEdge(POINT, EDGE, TOL)
0007 %
0008 %   Description
0009 %   B = isPointOnEdge(POINT, EDGE)
0010 %   with POINT being [xp yp], and EDGE being [x1 y1 x2 y2], returns TRUE if
0011 %   the point is located on the edge, and FALSE otherwise.
0012 %
0013 %   B = isPointOnEdge(POINT, EDGE, TOL)
0014 %   Specify an optilonal tolerance value TOL. The tolerance is given as a
0015 %   fraction of the norm of the edge direction vector. Default is 1e-14.
0016 %
0017 %   B = isPointOnEdge(POINTARRAY, EDGE)
0018 %   B = isPointOnEdge(POINT, EDGEARRAY)
0019 %   When one of the inputs has several rows, return the result of the test
0020 %   for each element of the array tested against the single parameter.
0021 %
0022 %   B = isPointOnEdge(POINTARRAY, EDGEARRAY)
0023 %   When both POINTARRAY and EDGEARRAY have the same number of rows,
0024 %   returns a column vector with the same number of rows.
0025 %   When the number of rows are different and both greater than 1, returns
0026 %   a Np-by-Ne matrix of booleans, containing the result for each couple of
0027 %   point and edge.
0028 %
0029 %   Examples
0030 %   % create a point array
0031 %   points = [10 10;15 10; 30 10];
0032 %   % create an edge array
0033 %   vertices = [10 10;20 10;20 20;10 20];
0034 %   edges = [vertices vertices([2:end 1], :)];
0035 %
0036 %   % Test one point and one edge
0037 %   isPointOnEdge(points(1,:), edges(1,:))
0038 %   ans =
0039 %       1
0040 %   isPointOnEdge(points(3,:), edges(1,:))
0041 %   ans =
0042 %       0
0043 %
0044 %   % Test one point and several edges
0045 %   isPointOnEdge(points(1,:), edges)'
0046 %   ans =
0047 %        1     0     0     1
0048 %
0049 %   % Test several points and one edge
0050 %   isPointOnEdge(points, edges(1,:))'
0051 %   ans =
0052 %        1     1     0
0053 %
0054 %   % Test N points and N edges
0055 %   isPointOnEdge(points, edges(1:3,:))'
0056 %   ans =
0057 %        1     0     0
0058 %
0059 %   % Test NP points and NE edges
0060 %   isPointOnEdge(points, edges)
0061 %   ans =
0062 %        1     0     0     1
0063 %        1     0     0     0
0064 %        0     0     0     0
0065 %
0066 %
0067 %   See also
0068 %   edges2d, points2d, isPointOnLine
0069 %
0070 
0071 %   ---------
0072 %   author : David Legland
0073 %   INRA - TPV URPOI - BIA IMASTE
0074 %   created the 31/10/2003.
0075 %
0076 
0077 %   HISTORY
0078 %   11/03/2004 change input format: edge is [x1 y1 x2 y2].
0079 %   17/01/2005 if test N edges with N points, return N boolean.
0080 %   21/01/2005 normalize test for colinearity, so enhance precision
0081 %   22/05/2009 rename to isPointOnEdge, add psb to specify tolerance
0082 %   26/01/2010 fix bug in precision computation
0083 %   04/10/2010 fix a bug, and clean up code
0084 %   28/10/2010 fix bug to have N results when input is N points and N
0085 %       edges, add support for arrays with different numbers of rows, and
0086 %       update doc.
0087 %   2011-06-15 rewrites by using less memory, and avoiding repmat when psb
0088 
0089 
0090 % extract computation tolerance
0091 tol = 1e-14;
0092 if ~isempty(varargin)
0093     tol = varargin{1};
0094 end
0095 
0096 % number of edges and of points
0097 nPoints = size(point, 1);
0098 nEdges = size(edge, 1);
0099 
0100 % adapt size of inputs if needed, and extract elements for computation
0101 if nPoints == nEdges
0102     % When the number of points and edges is the same, the one-to-one test
0103     % will be computed, so there is no need to repeat matrices
0104     dx = edge(:,3) - edge(:,1);
0105     dy = edge(:,4) - edge(:,2);
0106     lx = point(:,1) - edge(:,1);
0107     ly = point(:,2) - edge(:,2);
0108     
0109 elseif nPoints == 1
0110     % one point, several edges
0111     dx = edge(:, 3) - edge(:, 1);
0112     dy = edge(:, 4) - edge(:, 2);
0113     lx = point(ones(nEdges, 1), 1) - edge(:, 1);
0114     ly = point(ones(nEdges, 1), 2) - edge(:, 2);
0115     
0116 elseif nEdges == 1
0117     % several points, one edge
0118     dx = (edge(3) - edge(1)) * ones(nPoints, 1);
0119     dy = (edge(4) - edge(2)) * ones(nPoints, 1);
0120     lx = point(:, 1) - edge(1);
0121     ly = point(:, 2) - edge(2);
0122 
0123 else
0124     % Np points and Ne edges:
0125     % Create an array for each parameter, so that the result will be a
0126     % Np-by-Ne matrix of booleans (requires more memory, and uses repmat)
0127 
0128     x0 = repmat(edge(:, 1)', nPoints, 1);
0129     y0 = repmat(edge(:, 2)', nPoints, 1);
0130     dx = repmat(edge(:, 3)', nPoints,  1) - x0;
0131     dy = repmat(edge(:, 4)', nPoints,  1) - y0;
0132     
0133     lx = repmat(point(:, 1), 1, nEdges) - x0;
0134     ly = repmat(point(:, 2), 1, nEdges) - y0;
0135 end
0136 
0137 % test if point is located on supporting line
0138 b1 = abs(lx.*dy - ly.*dx) ./ (dx.*dx + dy.*dy) < tol;
0139 
0140 % compute position of point with respect to edge bounds
0141 % use different tests depending on line angle
0142 ind     = abs(dx) > abs(dy);
0143 t       = zeros(size(dx));
0144 t(ind)  = lx( ind) ./ dx( ind);
0145 t(~ind) = ly(~ind) ./ dy(~ind);
0146 
0147 % check if point is located between edge bounds
0148 b = t >- tol & t-1 < tol & b1;

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