Home > matGeom > polygons2d > findPoint.m

findPoint

PURPOSE ^

FINDPOINT Find index of a point in an set from its coordinates.

SYNOPSIS ^

function index = findPoint(coord, points, varargin)

DESCRIPTION ^

FINDPOINT Find index of a point in an set from its coordinates.
 
   IND = findPoint(POINT, ARRAY) 
   Returns the index of point whose coordinates match the 1-by-2 row array
   POINT in the N-by-2 array ARRAY. If the point is not found, returns 0.
   If several points are found, keep only the first one.

   If POINT is a M-by-2 array, the result is a M-by-1 array, containing
   the index in the array of each point given by COORD, or 0 if the point
   is not found.

   IND = findPoint(POINT, ARRAY, TOL) 
   use specified tolerance, to find point within a distance of TOL.
   Default tolerance is zero.

   See also
    points2d, minDistancePoints, distancePoints, findClosestPoint

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function index = findPoint(coord, points, varargin)
0002 %FINDPOINT Find index of a point in an set from its coordinates.
0003 %
0004 %   IND = findPoint(POINT, ARRAY)
0005 %   Returns the index of point whose coordinates match the 1-by-2 row array
0006 %   POINT in the N-by-2 array ARRAY. If the point is not found, returns 0.
0007 %   If several points are found, keep only the first one.
0008 %
0009 %   If POINT is a M-by-2 array, the result is a M-by-1 array, containing
0010 %   the index in the array of each point given by COORD, or 0 if the point
0011 %   is not found.
0012 %
0013 %   IND = findPoint(POINT, ARRAY, TOL)
0014 %   use specified tolerance, to find point within a distance of TOL.
0015 %   Default tolerance is zero.
0016 %
0017 %   See also
0018 %    points2d, minDistancePoints, distancePoints, findClosestPoint
0019 
0020 %   -----
0021 %   author: David Legland
0022 %   INRA - TPV URPOI - BIA IMASTE
0023 %   created the 17/07/2003.
0024 %
0025 
0026 %   HISTORY
0027 %   10/02/2004 documentation
0028 %   09/08/2004 rewrite faster, and add support for multiple points
0029 
0030 % number of points
0031 np = size(coord, 1);
0032 
0033 % allocate memory for result
0034 index = zeros(np, 1);
0035 
0036 % specify the tolerance
0037 tol = 0;
0038 if ~isempty(varargin)
0039     tol = varargin{1};
0040 end
0041 
0042 if tol == 0
0043     for i = 1:np
0044         % indices of matches
0045         ind = find(points(:,1) == coord(i,1) & points(:,2) == coord(i,2));
0046         
0047         % format current result
0048         if isempty(ind)
0049             index(i) = 0;
0050         else
0051             index(i) = ind(1);
0052         end
0053     end
0054 else
0055     for i = 1:np
0056         % indices of matches
0057         ind = find(sqrt(sum(bsxfun(@minus, points, coord) .^ 2, 2)) <= tol);
0058         
0059         % format current result
0060         if isempty(ind)
0061             index(i) = 0;
0062         else
0063             index(i) = ind(1);
0064         end
0065     end
0066 end

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