%function Area = WhettedArea( fileName, wingSpan )
function WhettedArea(fileName,wingSpan, n)
% fileName = 'AirData.mat';
% wingSpan = 1;
% n = 50;

Area = 0; 
pranformArea = 0;

PlanData1 = load(fileName);
PlanData = PlanData1.(fileName(1:end-4));



Chords = findChords(n, PlanData);
for i = 1:(size(PlanData,1)-1)
    for j = 1:n
        dist = wingSpan*(PlanData{i+1,1}-PlanData{i,1})./n;
        Area = Area+findPerim( PlanData{i,4}, Chords(i,j), wingSpan).*dist;
%         fprintf('Chord: %f Perimiter: %f Area: %f\n', Chords(i,j), findPerim( PlanData{i,4}, Chords(i,j), wingSpan), Area)
    end
end

for i = 1:(size(PlanData,1)-1)
    for j = 1:n
        dist = wingSpan*(PlanData{i+1,1}-PlanData{i,1})./n;
        pranformArea = pranformArea+Chords(i,j).*dist;
%         fprintf('Chord: %f  pranformArea: %f \n', Chords(i,j), pranformArea)
    end
end
WetArea = Area *2;
pranformArea = pranformArea*2;

fprintf('Planform Area: %f, Wetted Area: %f \n', pranformArea, WetArea)
end

function Chords = findChords( n, PlanData )
Chord = cell2mat(PlanData(:,2));
X = cell2mat(PlanData(:,1));
for( i = 1:(length(Chord)-1) )
    Xtemp = linspace(X(i),X(i+1),n);
    Chords(i,:) = (Chord(i+1)-Chord(i))./(X(i+1)-X(i)).*(Xtemp-X(i))+Chord(i);
    
end
end

function peri = findPerim( airFile, chord, span)
peri = 0;
Airfoil = load(airFile);
Airfoil = Airfoil*chord*span;
for i = 1:(size(Airfoil,1)-1)
    peri = peri + sqrt((Airfoil(i+1,1)-Airfoil(i,1))^2+(Airfoil(i+1,2)-Airfoil(i,2))^2);
end
end