function [A] = f_closed_area(C)
% This function calculates the area enclosed by a closed curve C, 
% which can be traveled 'counterclockwise' ONLY. Not crossed loops.
% It's coordinate points (x,y) are given by the matrix 'C'.
% Where:  col.1 = x_coords.  and   col.2 = y_coords.
% The calculation method it is based on the Green's Theorem.
%
% For more details see:       http://www.ma.iup.edu/projects/CalcDEMma/Green/Green.html
% 
% by          Jose Luis Prego Borges    
%             Universitat Politecnica de Catalunya  Barcelona-Spain
% email:      joseluisjujuy@ubbi.com
%
% ver 1.0     30/03/2006  

      n = max(size(C));
         
if n <= 2
   disp('Cant calculate area with only 2 points!..') 
else
    [nx,ny] = size(C);
    if nx < ny   % are the coordinates given by colums?
        C = C';  % no! Change to column version. 
    end
    if C(n,:) ~= C(1,:);  % check to see in first-point = last-point --> closed lood
            C2 = zeros(nx+1,ny); % if not...closed the loop! 
     C2(1:n,:) = C(1:n,:);      
     C2(n+1,:) = C(1,:);    % putting a dummy point
             C = C2;
    end    
    x = C(:,1);  % getting the 'x' coordinate
    y = C(:,2);  % getting the 'y' coordinate
    for i = 1:n-1
        a(i) = 0.5*((x(i+1) + x(i))*(y(i+1) - y(i))) - 0.5*((y(i+1) + y(i))*(x(i+1) - x(i)));  % Calculating integral points for numeric integration...see the web above :)
    end
    A = 0.5*sum(a);% calculating numeric integral and scaling the final area  :) 
end



