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 % E-mail: david.legland@inrae.fr
0074 % Created: 2003-10-31
0075 % Copyright 2003-2024 INRA - TPV URPOI - BIA IMASTE
0076 
0077 % extract computation tolerance
0078 tol = 1e-14;
0079 if ~isempty(varargin)
0080     tol = varargin{1};
0081 end
0082 
0083 % number of edges and of points
0084 nPoints = size(point, 1);
0085 nEdges = size(edge, 1);
0086 
0087 % adapt size of inputs if needed, and extract elements for computation
0088 if nPoints == nEdges
0089     % When the number of points and edges is the same, the one-to-one test
0090     % will be computed, so there is no need to repeat matrices
0091     dx = edge(:,3) - edge(:,1);
0092     dy = edge(:,4) - edge(:,2);
0093     lx = point(:,1) - edge(:,1);
0094     ly = point(:,2) - edge(:,2);
0095     
0096 elseif nPoints == 1
0097     % one point, several edges
0098     dx = edge(:, 3) - edge(:, 1);
0099     dy = edge(:, 4) - edge(:, 2);
0100     lx = point(ones(nEdges, 1), 1) - edge(:, 1);
0101     ly = point(ones(nEdges, 1), 2) - edge(:, 2);
0102     
0103 elseif nEdges == 1
0104     % several points, one edge
0105     dx = (edge(3) - edge(1)) * ones(nPoints, 1);
0106     dy = (edge(4) - edge(2)) * ones(nPoints, 1);
0107     lx = point(:, 1) - edge(1);
0108     ly = point(:, 2) - edge(2);
0109 
0110 else
0111     % Np points and Ne edges:
0112     % Create an array for each parameter, so that the result will be a
0113     % Np-by-Ne matrix of booleans (requires more memory, and uses repmat)
0114 
0115     x0 = repmat(edge(:, 1)', nPoints, 1);
0116     y0 = repmat(edge(:, 2)', nPoints, 1);
0117     dx = repmat(edge(:, 3)', nPoints,  1) - x0;
0118     dy = repmat(edge(:, 4)', nPoints,  1) - y0;
0119     
0120     lx = repmat(point(:, 1), 1, nEdges) - x0;
0121     ly = repmat(point(:, 2), 1, nEdges) - y0;
0122 end
0123 
0124 % test if point is located on supporting line
0125 b1 = abs(lx.*dy - ly.*dx) ./ (dx.*dx + dy.*dy) < tol;
0126 
0127 % compute position of point with respect to edge bounds
0128 % use different tests depending on line angle
0129 ind     = abs(dx) > abs(dy);
0130 t       = zeros(size(dx));
0131 t(ind)  = lx( ind) ./ dx( ind);
0132 t(~ind) = ly(~ind) ./ dy(~ind);
0133 
0134 % check if point is located between edge bounds
0135 b = t >- tol & t-1 < tol & b1;

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022