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