POLYLINEPOINT Extract a point from a polyline. POINT = polylinePoint(POLYLINE, POS) POLYLINE is a N*2 array containing coordinate of polyline vertices POS is comprised between 0 (first point of polyline) and Nv-1 (last point of the polyline). Example poly = [10 10;20 10;20 20;30 30]; polylinePoint(poly, 0) [10 10] polylinePoint(poly, 3) [30 30] polylinePoint(poly, 1.4) [20 14] See also polygons2d ------ Author: David Legland e-mail: david.legland@grignon.inra.fr Created: 2009-04-30, using Matlab 7.7.0.471 (R2008b) Copyright 2009 INRA - Cepia Software Platform.
0001 function point = polylinePoint(poly, pos) 0002 %POLYLINEPOINT Extract a point from a polyline. 0003 % 0004 % POINT = polylinePoint(POLYLINE, POS) 0005 % POLYLINE is a N*2 array containing coordinate of polyline vertices 0006 % POS is comprised between 0 (first point of polyline) and Nv-1 (last 0007 % point of the polyline). 0008 % 0009 % 0010 % Example 0011 % poly = [10 10;20 10;20 20;30 30]; 0012 % polylinePoint(poly, 0) 0013 % [10 10] 0014 % polylinePoint(poly, 3) 0015 % [30 30] 0016 % polylinePoint(poly, 1.4) 0017 % [20 14] 0018 % 0019 % 0020 % See also 0021 % polygons2d 0022 % 0023 % ------ 0024 % Author: David Legland 0025 % e-mail: david.legland@grignon.inra.fr 0026 % Created: 2009-04-30, using Matlab 7.7.0.471 (R2008b) 0027 % Copyright 2009 INRA - Cepia Software Platform. 0028 0029 0030 % number of points to compute 0031 Np = length(pos(:)); 0032 0033 % number of vertices in polyline 0034 Nv = size(poly, 1); 0035 0036 % allocate memory results 0037 point = zeros(Np, 2); 0038 0039 % iterate on points 0040 for i=1:Np 0041 % compute index of edge (between 0 and Nv) 0042 ind = floor(pos(i)); 0043 0044 % special case of last point of polyline 0045 if ind==Nv-1 0046 point(i,:) = poly(end,:); 0047 continue; 0048 end 0049 0050 % format index to ensure being on polyline 0051 ind = min(max(ind, 0), Nv-2); 0052 0053 % position on current edge 0054 t = min(max(pos(i)-ind, 0), 1); 0055 0056 % parameters of current edge 0057 x0 = poly(ind+1, 1); 0058 y0 = poly(ind+1, 2); 0059 dx = poly(ind+2,1)-x0; 0060 dy = poly(ind+2,2)-y0; 0061 0062 % compute position of current point 0063 point(i, :) = [x0+t*dx, y0+t*dy]; 0064 end