Outer normal vector for a given vertex(ices). NV = polygonOuterNormal(POLY, VIND) Where POLY is a polygon and VIND is the index of a vertex, returns the outer normal vector of the specified vertex. The normal is computed by averaging the tangent vectors of the two neighbor edges, i.e. by computing a finite difference of the neighbor vertices. NV = polygonOuterNormal(POLY) Returns an array with as many vectors as the number of vertices of the input polygon, containing the outer normal of each vertex. Example % compute outer normals to an ellipse elli = [50 50 40 20 30]; poly = ellipseToPolygon(elli, 200); figure; hold on; drawPolygon(poly, 'b'); axis equal; axis([0 100 10 90]); inds = 1:10:200; pts = poly(inds, :); drawPoint(pts, 'bo') vect = polygonOuterNormal(poly, inds); drawVector(pts, vect*10, 'b'); See also polygons2d, polygonPoint, polygonNormalAngle
0001 function vect = polygonOuterNormal(poly, iVertex) 0002 % Outer normal vector for a given vertex(ices). 0003 % 0004 % NV = polygonOuterNormal(POLY, VIND) 0005 % Where POLY is a polygon and VIND is the index of a vertex, returns the 0006 % outer normal vector of the specified vertex. 0007 % The normal is computed by averaging the tangent vectors of the two 0008 % neighbor edges, i.e. by computing a finite difference of the neighbor 0009 % vertices. 0010 % 0011 % NV = polygonOuterNormal(POLY) 0012 % Returns an array with as many vectors as the number of vertices of the 0013 % input polygon, containing the outer normal of each vertex. 0014 % 0015 % 0016 % Example 0017 % % compute outer normals to an ellipse 0018 % elli = [50 50 40 20 30]; 0019 % poly = ellipseToPolygon(elli, 200); 0020 % figure; hold on; 0021 % drawPolygon(poly, 'b'); axis equal; axis([0 100 10 90]); 0022 % inds = 1:10:200; pts = poly(inds, :); drawPoint(pts, 'bo') 0023 % vect = polygonOuterNormal(poly, inds); 0024 % drawVector(pts, vect*10, 'b'); 0025 % 0026 % See also 0027 % polygons2d, polygonPoint, polygonNormalAngle 0028 % 0029 0030 % ------ 0031 % Author: David Legland 0032 % e-mail: david.legland@inrae.fr 0033 % Created: 2017-11-23, using Matlab 8.6.0.267246 (R2015b) 0034 % Copyright 2017 INRA - Cepia Software Platform. 0035 0036 % number of vertices 0037 nv = size(poly, 1); 0038 0039 % if indices not specified, compute for all vertices 0040 if nargin == 1 0041 iVertex = 1:nv; 0042 end 0043 0044 % allocate memory 0045 vect = zeros(length(iVertex), 2); 0046 0047 % compute normal vector of each result vertex 0048 for i = 1:length(iVertex) 0049 iNext = mod(iVertex(i), nv) + 1; 0050 iPrev = mod(iVertex(i)-2, nv) + 1; 0051 tangent = (poly(iNext,:) - poly(iPrev,:)) / 2; 0052 vect(i,:) = [tangent(2) -tangent(1)]; 0053 end