RESAMPLEPOLYLINE Distribute N points equally spaced on a polyline. RES = resamplePolyline(POLY, N) Resample the input polyline POLY such that the resulting polyline RES has N points. All points of RES belong to the initial polyline, but are not necessarily vertices. Example poly = [0 10;0 0;20 0]; figure; drawPolyline(poly, 'b'); poly2 = resamplePolyline(poly, 10); hold on; drawPolyline(poly2, 'bo'); axis equal; axis([-10 30 -10 20]); See also polygons2d, drawPolyline, resamplePolygon, resamplePolylineByLength
0001 function poly2 = resamplePolyline(poly, n) 0002 %RESAMPLEPOLYLINE Distribute N points equally spaced on a polyline. 0003 % 0004 % RES = resamplePolyline(POLY, N) 0005 % Resample the input polyline POLY such that the resulting polyline RES 0006 % has N points. All points of RES belong to the initial polyline, but are 0007 % not necessarily vertices. 0008 % 0009 % Example 0010 % poly = [0 10;0 0;20 0]; 0011 % figure; drawPolyline(poly, 'b'); 0012 % poly2 = resamplePolyline(poly, 10); 0013 % hold on; 0014 % drawPolyline(poly2, 'bo'); 0015 % axis equal; axis([-10 30 -10 20]); 0016 % 0017 % See also 0018 % polygons2d, drawPolyline, resamplePolygon, resamplePolylineByLength 0019 % 0020 0021 % ------ 0022 % Author: David Legland 0023 % e-mail: david.legland@inra.fr 0024 % Created: 2011-12-09, using Matlab 7.9.0.529 (R2009b) 0025 % Copyrightf 2011 INRA - Cepia Software Platform. 0026 0027 % parametrisation of the curve 0028 s = parametrize(poly); 0029 0030 % distribute N points equally spaced 0031 Lmax = s(end); 0032 pos = linspace(0, Lmax, n); 0033 0034 poly2 = zeros(n, size(poly, 2)); 0035 for i = 1:n 0036 % index of surrounding vertices before and after 0037 ind0 = find(s <= pos(i), 1, 'last'); 0038 ind1 = find(s >= pos(i), 1, 'first'); 0039 0040 if ind0 == ind1 0041 % get position of a vertex in input polyline 0042 poly2(i, :) = poly(ind0, :); 0043 continue; 0044 end 0045 0046 % position of surrounding vertices 0047 pt0 = poly(ind0, :); 0048 pt1 = poly(ind1, :); 0049 0050 % weights associated to each neighbor 0051 l0 = pos(i) - s(ind0); 0052 l1 = s(ind1) - pos(i); 0053 0054 % linear interpolation of neighbor positions 0055 if (l0 + l1) > Lmax * 1e-12 0056 poly2(i, :) = (pt0 * l1 + pt1 * l0) / (l0 + l1); 0057 else 0058 % if neighbors are too close, do not use interpolation 0059 poly2(i, :) = pt0; 0060 end 0061 end