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

engineArea=600;
Area = 0; 
pranformArea = 0;

cd('\\ad.uiuc.edu\ae\correa2\Desktop\AE441\Matlab code\Jacobi\Planforms')
[spans, chords, offsets, airfoil]=textread(fileName, '%f %f %f %s');



Chords = findChords(n, chords, spans);
cd('\\ad.uiuc.edu\ae\correa2\Desktop\AE441\Matlab code\Jacobi\Airfoils')
for i = 1:(size(chords)-1)
    for j = 1:n
        Chords(i,j);
        dist = wingSpan*(spans(i+1)-spans(i))./n;
        Area = Area+findPerim( airfoil{i}, Chords(i,j), wingSpan).*dist;
    end
end

for i = 1:(size(chords)-1)
    for j = 1:n
        dist = wingSpan*(spans(i+1)-spans(i))./n;
        pranformArea = pranformArea+Chords(i,j).*dist;
%         fprintf('Chord: %f  pranformArea: %f \n', Chords(i,j), pranformArea)
    end
end

Area = Area *2+engineArea;

WetArea = Area;
pranformArea = pranformArea*2;

fprintf('Planform Area: %f, Wetted Area: %f \n', pranformArea, WetArea)
cd('\\ad.uiuc.edu\ae\correa2\Desktop\AE441\Matlab code\Jacobi\')
end

function Chords = findChords( n, Chord, X )
% Chord = PlanData(:,2);
% X = 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