POLYGONAREA Compute the signed area of a polygon. A = polygonArea(POINTS); Compute area of a polygon defined by POINTS. POINTS is a N-by-2 array of double containing coordinates of vertices. Vertices of the polygon are supposed to be oriented Counter-Clockwise (CCW). In this case, the signed area is positive. If vertices are oriented Clockwise (CW), the signed area is negative. If polygon is self-crossing, the result is undefined. Examples % compute area of a simple shape poly = [10 10;30 10;30 20;10 20]; area = polygonArea(poly) area = 200 % compute area of CW polygon area2 = polygonArea(poly(end:-1:1, :)) area2 = -200 % Computes area of a paper hen x = [0 10 20 0 -10 -20 -10 -10 0]; y = [0 0 10 10 20 10 10 0 -10]; poly = [x' y']; area = polygonArea(poly) area = 400 % Area of unit square with 25% hole pccw = [0 0; 1 0; 1 1; 0 1]; pcw = pccw([1 4 3 2], :) * .5 + .25; polygonArea ([pccw; nan(1,2); pcw]) ans = 0.75 References algo adapted from P. Bourke web page http://paulbourke.net/geometry/polygonmesh/ See also polygons2d, polygonCentroid, polygonSecondAreaMoments, triangleArea
0001 function area = polygonArea(poly, varargin) 0002 %POLYGONAREA Compute the signed area of a polygon. 0003 % 0004 % A = polygonArea(POINTS); 0005 % Compute area of a polygon defined by POINTS. POINTS is a N-by-2 array 0006 % of double containing coordinates of vertices. 0007 % 0008 % Vertices of the polygon are supposed to be oriented Counter-Clockwise 0009 % (CCW). In this case, the signed area is positive. 0010 % If vertices are oriented Clockwise (CW), the signed area is negative. 0011 % 0012 % If polygon is self-crossing, the result is undefined. 0013 % 0014 % Examples 0015 % % compute area of a simple shape 0016 % poly = [10 10;30 10;30 20;10 20]; 0017 % area = polygonArea(poly) 0018 % area = 0019 % 200 0020 % 0021 % % compute area of CW polygon 0022 % area2 = polygonArea(poly(end:-1:1, :)) 0023 % area2 = 0024 % -200 0025 % 0026 % % Computes area of a paper hen 0027 % x = [0 10 20 0 -10 -20 -10 -10 0]; 0028 % y = [0 0 10 10 20 10 10 0 -10]; 0029 % poly = [x' y']; 0030 % area = polygonArea(poly) 0031 % area = 0032 % 400 0033 % 0034 % % Area of unit square with 25% hole 0035 % pccw = [0 0; 1 0; 1 1; 0 1]; 0036 % pcw = pccw([1 4 3 2], :) * .5 + .25; 0037 % polygonArea ([pccw; nan(1,2); pcw]) 0038 % ans = 0039 % 0.75 0040 % 0041 % References 0042 % algo adapted from P. Bourke web page 0043 % http://paulbourke.net/geometry/polygonmesh/ 0044 % 0045 % See also 0046 % polygons2d, polygonCentroid, polygonSecondAreaMoments, triangleArea 0047 % 0048 0049 % ------ 0050 % Author: David Legland 0051 % E-mail: david.legland@inrae.fr 0052 % Created: 2004-05-05 0053 % Copyright 2004-2024 INRA - TPV URPOI - BIA IMASTE 0054 0055 %% Process special cases 0056 0057 % in case of polygon sets, computes the sum of polygon areas 0058 if iscell(poly) 0059 area = 0; 0060 for i = 1:length(poly) 0061 area = area + polygonArea(poly{i}); 0062 end 0063 return; 0064 end 0065 0066 % check there are enough points 0067 if size(poly, 1) < 2 0068 area = 0; 0069 return; 0070 end 0071 0072 % case of polygons with holes -> computes the sum of areas 0073 if any(isnan(poly)) 0074 area = sum(polygonArea(splitPolygons(poly))); 0075 return; 0076 end 0077 0078 0079 %% Process single polygons or single rings 0080 0081 % extract coordinates 0082 if nargin == 1 0083 % polygon given as N-by-2 array 0084 px = poly(:, 1); 0085 py = poly(:, 2); 0086 0087 elseif nargin == 2 0088 % poylgon given as two N-by-1 arrays 0089 px = poly; 0090 py = varargin{1}; 0091 end 0092 0093 % indices of next vertices 0094 N = length(px); 0095 iNext = [2:N 1]; 0096 0097 % compute area (vectorized version) 0098 area = sum(px .* py(iNext) - px(iNext) .* py) / 2;