EDGEPOSITION Return position of a point on an edge. POS = edgePosition(POINT, EDGE); Computes position of point POINT on the edge EDGE, relative to the position of edge vertices. EDGE has the form [x1 y1 x2 y2], POINT has the form [x y], and is assumed to belong to edge supporting line. The result POS has the following meaning: POS < 0: POINT is located before the first vertex POS = 0: POINT is located on the first vertex 0 < POS < 1: POINT is located between the 2 vertices (on the edge) POS = 1: POINT is located on the second vertex POS > 1: POINT is located after the second vertex POS = edgePosition(POINT, EDGES); If EDGES is an array of NL edges, return NE positions, corresponding to each edge. POS = edgePosition(POINTS, EDGE); If POINTS is an array of NP points, return NP positions, corresponding to each point. POS = edgePosition(POINTS, EDGES); If POINTS is an array of NP points and EDGES is an array of NE edges, return an array of [NP NE] position, corresponding to each couple point-edge. POS = edgePosition(POINTS, EDGES, 'diag'); When POINTS and EDGES are two arrays with same number of rows, returns single column vector corresponding to each pair of point-edge. See also edges2d, createEdge, isPointOnEdge, edgeToLine
0001 function pos = edgePosition(point, edge, varargin) 0002 %EDGEPOSITION Return position of a point on an edge. 0003 % 0004 % POS = edgePosition(POINT, EDGE); 0005 % Computes position of point POINT on the edge EDGE, relative to the 0006 % position of edge vertices. 0007 % EDGE has the form [x1 y1 x2 y2], 0008 % POINT has the form [x y], and is assumed to belong to edge supporting 0009 % line. The result POS has the following meaning: 0010 % POS < 0: POINT is located before the first vertex 0011 % POS = 0: POINT is located on the first vertex 0012 % 0 < POS < 1: POINT is located between the 2 vertices (on the edge) 0013 % POS = 1: POINT is located on the second vertex 0014 % POS > 1: POINT is located after the second vertex 0015 % 0016 % POS = edgePosition(POINT, EDGES); 0017 % If EDGES is an array of NL edges, return NE positions, corresponding to 0018 % each edge. 0019 % 0020 % POS = edgePosition(POINTS, EDGE); 0021 % If POINTS is an array of NP points, return NP positions, corresponding 0022 % to each point. 0023 % 0024 % POS = edgePosition(POINTS, EDGES); 0025 % If POINTS is an array of NP points and EDGES is an array of NE edges, 0026 % return an array of [NP NE] position, corresponding to each couple 0027 % point-edge. 0028 % 0029 % POS = edgePosition(POINTS, EDGES, 'diag'); 0030 % When POINTS and EDGES are two arrays with same number of rows, returns 0031 % single column vector corresponding to each pair of point-edge. 0032 % 0033 % See also 0034 % edges2d, createEdge, isPointOnEdge, edgeToLine 0035 % 0036 0037 % ------ 0038 % Author: David Legland 0039 % E-mail: david.legland@inrae.fr 0040 % Created: 2004-05-25 0041 % Copyright 2004-2024 INRA - Cepia Software Platform 0042 0043 % parse input options 0044 diag = false; 0045 if ~isempty(varargin) && strcmp(varargin{1}, 'diag') 0046 diag = true; 0047 end 0048 0049 % number of points and of edges 0050 nEdges = size(edge, 1); 0051 nPoints = size(point, 1); 0052 0053 if nPoints == nEdges && diag 0054 % reshape into N-by-1 arrays 0055 dxe = (edge(:,3) - edge(:,1)); 0056 dye = (edge(:,4) - edge(:,2)); 0057 dxp = point(:,1) - edge(:,1); 0058 dyp = point(:,2) - edge(:,2); 0059 else 0060 % reshape arrays to result in NP-by-NE arrays 0061 dxe = (edge(:,3) - edge(:,1))'; 0062 dye = (edge(:,4) - edge(:,2))'; 0063 dxp = bsxfun(@minus, point(:,1), edge(:,1)'); 0064 dyp = bsxfun(@minus, point(:,2), edge(:,2)'); 0065 end 0066 0067 % compute position 0068 pos = (dxp .* dxe + dyp .* dye) ./ (dxe .* dxe + dye .* dye);