Normal angle at each vertex of a polygon. THETA = polygonNormalAngle(POLY); where POLY is a N-by-2 array representing vertex coordinates, computes the normal angle at each vertex of the polygon. THETA is a N-by-1 array containing numeric values between -PI and +PI. The result depends on the orientation of the polygon (counter-clockwise or clockwise). THETA = polygonNormalAngle(POLY, IND); Computes the normal angle for each vertex specified by IND. If IND is a vector of vertex indices. Example % Normal angles at vertices of an isosceles right triangle are pi/2 at % right angle-vertex and 3*pi/4 for the two remaining vertices. poly = [0 0 ; 1 0 ; 0 1]; polygonNormalAngle(poly) ans = 1.5708 2.3562 2.3562 % Compute normal angles for a slightly more complicated polygon poly = [0 0;0 1;-1 1;0 -1;1 0]; % compute normal angle at each vertex theta = polygonNormalAngle(poly); % sum of all normal angle of a non-intersecting polygon equals 2*pi % (can be -2*pi if polygon is oriented clockwise) sum(theta) ans = 6.2832 See also: polygons2d, polygonOuterNormal, normalizeAngle
0001 function theta = polygonNormalAngle(poly, inds) 0002 % Normal angle at each vertex of a polygon. 0003 % 0004 % THETA = polygonNormalAngle(POLY); 0005 % where POLY is a N-by-2 array representing vertex coordinates, computes 0006 % the normal angle at each vertex of the polygon. THETA is a N-by-1 array 0007 % containing numeric values between -PI and +PI. The result depends on 0008 % the orientation of the polygon (counter-clockwise or clockwise). 0009 % 0010 % THETA = polygonNormalAngle(POLY, IND); 0011 % Computes the normal angle for each vertex specified by IND. If IND is a 0012 % vector of vertex indices. 0013 % 0014 % 0015 % Example 0016 % % Normal angles at vertices of an isosceles right triangle are pi/2 at 0017 % % right angle-vertex and 3*pi/4 for the two remaining vertices. 0018 % poly = [0 0 ; 1 0 ; 0 1]; 0019 % polygonNormalAngle(poly) 0020 % ans = 0021 % 1.5708 0022 % 2.3562 0023 % 2.3562 0024 % 0025 % % Compute normal angles for a slightly more complicated polygon 0026 % poly = [0 0;0 1;-1 1;0 -1;1 0]; 0027 % % compute normal angle at each vertex 0028 % theta = polygonNormalAngle(poly); 0029 % % sum of all normal angle of a non-intersecting polygon equals 2*pi 0030 % % (can be -2*pi if polygon is oriented clockwise) 0031 % sum(theta) 0032 % ans = 0033 % 6.2832 0034 % 0035 % See also: 0036 % polygons2d, polygonOuterNormal, normalizeAngle 0037 % 0038 0039 % ------ 0040 % Author: David Legland 0041 % e-mail: david.legland@inra.fr 0042 % Created: 2005-11-30 0043 % Copyright 2005 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas). 0044 0045 % number of polygon vertices 0046 np = size(poly, 1); 0047 0048 if nargin == 1 0049 inds = 1:np; 0050 end 0051 0052 % number of angles to compute 0053 nv = length(inds); 0054 0055 theta = zeros(nv, 1); 0056 0057 for i = 1:nv 0058 % current vertex 0059 curr = poly(inds(i), :); 0060 0061 % previous and next vertices 0062 prev = poly(mod(inds(i)-2, np)+1, :); 0063 next = poly(mod(inds(i), np)+1, :); 0064 0065 theta(i) = angle3Points(prev, curr, next) - pi; 0066 end