1 %% Copyright (c) 2011 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
3 %% This program is free software; you can redistribute it and/or modify
4 %% it under the terms of the GNU General Public License as published by
5 %% the Free Software Foundation; either version 3 of the License, or
6 %% (at your option) any later version.
8 %% This program is distributed in the hope that it will be useful,
9 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
10 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 %% GNU General Public License for more details.
13 %% You should have received a copy of the GNU General Public License
14 %% along with this program; if not, see <http://www.gnu.org/licenses/>.
17 %% @deftypefn {Function File} { @var{a} =} shapearea (@var{pp})
18 %% Calculate the area of a 2D shape defined with piecewise smooth polynomials.
20 %% Shape is defined with piecewise smooth polynomials. @var{pp} is a
21 %% cell where each elements is a 2-by-(poly_degree+1) array containing a pair of
24 %% @code{px(i,:) = pp@{i@}(1,:)} and @code{py(i,:) = pp@{i@}(2,:)}.
26 %% @seealso{shapecentroid, shape2polygon, shapeplot}
29 function [A ccw] = shapearea (shape)
31 A = sum(cellfun (@Aint, shape));
33 warning ('geom2d:shapearea:InvalidResult', ...
34 'Shape has negative area. Assuming this is due to a clockwise parametrization of the boundary');
40 function dA = Aint (x)
45 P = polyint (conv (px, polyder(py)));
47 dA = diff(polyval(P,[0 1]));
51 %!demo % non-convex piece-wise polynomial shape
52 %! boomerang = {[ 0 -2 1; ...
60 %! A = shapearea (boomerang)
63 %! triangle = {[1 0; 0 0]; [-0.5 1; 1 0]; [-0.5 0.5; -1 1]};
64 %! A = shapearea (triangle);
68 %! circle = {[1.715729 -6.715729 0 5; ...
69 %! -1.715729 -1.568542 8.284271 0]; ...
70 %! [1.715729 1.568542 -8.284271 0; ...
71 %! 1.715729 -6.715729 0 5]; ...
72 %! [-1.715729 6.715729 0 -5; ...
73 %! 1.715729 1.568542 -8.284271 0]; ...
74 %! [-1.715729 -1.568542 8.284271 0; ...
75 %! -1.715729 6.715729 0 -5]};
76 %! A = shapearea (circle);
77 %! assert (pi*5^2, A, 5e-2);