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 % E-mail: david.legland@inrae.fr
0023 % Created: 2003-07-17
0024 % Copyright 2003-2024 INRA - TPV URPOI - BIA IMASTE
0025 
0026 % number of points
0027 np = size(coord, 1);
0028 
0029 % allocate memory for result
0030 index = zeros(np, 1);
0031 
0032 % specify the tolerance
0033 tol = 0;
0034 if ~isempty(varargin)
0035     tol = varargin{1};
0036 end
0037 
0038 if tol == 0
0039     for i = 1:np
0040         % indices of matches
0041         ind = find(points(:,1) == coord(i,1) & points(:,2) == coord(i,2));
0042         
0043         % format current result
0044         if isempty(ind)
0045             index(i) = 0;
0046         else
0047             index(i) = ind(1);
0048         end
0049     end
0050 else
0051     for i = 1:np
0052         % indices of matches
0053         ind = find(sqrt(sum(bsxfun(@minus, points, coord) .^ 2, 2)) <= tol);
0054         
0055         % format current result
0056         if isempty(ind)
0057             index(i) = 0;
0058         else
0059             index(i) = ind(1);
0060         end
0061     end
0062 end

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