%function [] = PlotFoil(name,Hs)

close all;

results_filenm = create_filenm([dir 'FlappingFoil_' name],Hs,Ls,Lc,dt,tf,b)
load([results_filenm '.mat']);

BuoyScale = 1.0;
FoilScale = 6.0;



Tf = 60;

load([results_filenm '.mat']);


%xoa(3) = -20;



t = get(Buoy,'t');

%Get Buoy motion results
x = get(Buoy,'x');
xdot = get(Buoy,'xdot');
xddot = get(Buoy,'xddot');
 
%Get Foil motion results
y = get(Foil,'x');
ydot = get(Foil,'xdot');
yddot = get(Foil,'xddot');

xoa = get(Foil,'x0');
xob = get(Buoy,'x0');
xoa(3) = -20;

Bforce = get(Buoy,'Forces');
Fforce = get(Foil,'Forces');

%MOOS Buoy
%BuoyA2
buoy_pts = [-.8 -.8 -.65  -.35 -0.2 -0.2    0.2  0.2 .35   .65 .8 .8 -.8; 
             .4 -.3 -0.5  -0.5 -.65 -1.25 -1.25 -.65 -.5  -0.5 -.3 .4  .4];  %meters

if (name == 'BuoyA10')
  buoy_pts(1,:) = buoy_pts(1,:)*3.6/1.6;  %Scale up for larger buoy.
end
    
if (name == 'BuoyA20')
  buoy_pts(1,:) = buoy_pts(1,:)*5.0/1.6;  %Scale up for larger buoy.
end

buoy_pts = BuoyScale*buoy_pts;
   
%Set up points for foil
xx = 0:.01:1;
yy = NACA_4Digit_Ordinates(.15,xx);
foil_pts = [-xx(end:-1:1)+.25 -xx+.25; yy(end:-1:1) -yy];
foil_pts = foil_pts*Lc;
foil_pts = FoilScale*foil_pts;


max_rng = max(max(abs(buoy_pts)));
current_buoy_center = [xob(1) xob(3)];           
           
xinc = current_buoy_center(1)-10*max_rng:20*max_rng/100:current_buoy_center(1)+10*max_rng;
eta0 = eta(wave,xinc,0);


fh = figure('Position',[25 58 1561 1063]);

ah1 = axes('OuterPosition',[0 0 .5 1]);



set(fh,'DoubleBuffer','on');
set(ah1,'nextplot','replace');
set(ah1,'FontSize',18);

hold(ah1,'on');
axis(ah1,'equal');


%h_buoy = plot(ah1,buoy_pts(1,:)+xob(1),buoy_pts(2,:)+xob(3),'k','LineWidth',1.5);
h_buoy = fill(buoy_pts(1,:)+xob(1)+x(1,1),buoy_pts(2,:)+xob(3)+x(1,3),'y','LineWidth',1.0);

hold on;
axis equal;
xlabel('m');
ylabel('m');

%Plot Incident Wave
h_incwave = plot(ah1,xinc,eta0,'b','LineWidth',1.5);

%Plot Timestamp
h_time=text(-8,4,sprintf('t = %4.1f s',0*dt),'FontSize',20);

%Plot Plate
h_foil = plot(ah1,foil_pts(1,:)+xoa(1)+y(1,1),foil_pts(2,:)+xoa(3)+y(1,3),'k','LineWidth',1.5);
%set(h_foil,'EraseMode','xor');
%set(h_foil,'EraseMode','normal');


%Plot Tether
tether_pts = zeros(2);
tether_pts(1,:) = [p_aB(:,1) p_aF(:,1)];
tether_pts(2,:) = [p_aB(:,3) p_aF(:,3)];
tether_pts(:,1) = BuoyScale*tether_pts(:,1);
tether_pts(:,2) = FoilScale*tether_pts(:,2);

clear ppts;
ppts(1,1) = tether_pts(1,1)+xob(1)+x(1,1);
ppts(1,2) = tether_pts(1,2)+xoa(1)+y(1,1);
ppts(2,1) = tether_pts(2,1)+xob(3)+x(1,3);
ppts(2,2) = tether_pts(2,2)+xoa(3)+y(1,3);
h_tether = plot(ah1,ppts(1,:),ppts(2,:),'r-','LineWidth',1.5);


foo(1) = -8; 
foo(2) = 8;
foo(3) = -25;
foo(4) = 5;
axis(ah1,foo);
axis(ah1,'equal');


ah2 = axes('OuterPosition',[.5 .2 .5 .6]);
set(ah2,'FontSize',16);
set(ah2,'nextplot','replace');
Vmax = norm(max(ydot(:,1:3)));
scale = 2*max_rng/Vmax;
V = [ 0 ydot(1,1)*scale; 0 ydot(1,3)*scale];

Radius = 4*max(foil_pts(1,:)/FoilScale);
xx = -Radius:Radius/100:Radius;
yy = sqrt(Radius^2-xx.^2);
set(gca,'nextplot','replace');

h_circle = plot([xx xx(end:-1:1)],[yy -yy(end:-1:1)],'b');

axis equal;
hold on;
h_foilAOA = plot(foil_pts(1,:)/FoilScale,foil_pts(2,:)/FoilScale,'k','LineWidth',1.5);
%set(h_foilAOA,'EraseMode','xor');
%set(h_foil,'EraseMode','normal');
foo(1) = -2*Radius;
foo(2) = +2*Radius;
foo(3) = -2*Radius;
foo(4) = +2*Radius;
axis(foo);
xlabel('m');
ylabel('m');

%h_V = arrow(V(:,1),V(:,2));


th = atan2(V(2,2),V(1,2));
len = norm(V)/2;
V(1,1) = (Radius+len)*cos(th);
V(2,1) = (Radius+len)*sin(th);
V(1,2) = Radius*cos(th);
V(2,2) = Radius*sin(th);
h_AOA = arrow(V(:,1),V(:,2),'EdgeColor','blue','FaceColor','blue');


scaleF = max_rng/norm(max(Fforce{3}));

F = [ 0 Fforce{3}(1,1)*scaleF ; 0 Fforce{3}(1,3)*scaleF];
h_Force = arrow(F(:,1),F(:,2),'EdgeColor','green','FaceColor','green');



NN = min(Tf./dt,tf./dt);

pause

for i = 1:NN;  %length(t)
  pause(.95*dt);

%Replot Buoy 
  R = [cos(x(i,5)) sin(x(i,5)); -sin(x(i,5)) cos(x(i,5))];
  ppts = R*buoy_pts;  
  for j = 1:length(ppts)
     ppts(1,j) = ppts(1,j)+x(i,1)+xob(1);
     ppts(2,j) = ppts(2,j)+x(i,3)+xob(3);
  end
  set(h_buoy,'XData',ppts(1,:),'YData',ppts(2,:));
  
  
%Replot Tether
  ppts = tether_pts;
  ppts(:,1) = R*tether_pts(:,1);  
  R = [cos(y(i,5)) sin(y(i,5)); -sin(y(i,5)) cos(y(i,5))];
  ppts(:,2) = R*tether_pts(:,2);  
  ppts(1,1) = ppts(1,1)+xob(1)+x(i,1);
  ppts(1,2) = ppts(1,2)+xoa(1)+y(i,1);
  ppts(2,1) = ppts(2,1)+xob(3)+x(i,3);
  ppts(2,2) = ppts(2,2)+xoa(3)+y(i,3);
  
  set(h_tether,'XData',ppts(1,:),'YData',ppts(2,:));
  
%Replot Wave  
  eta0 = eta(wave,xinc-x(i,1),i*dt);
  set(h_incwave,'Xdata',xinc,'YData',eta0);
  
%Replot Time-stamp
  set(h_time,'string',sprintf('t = %4.1f s',i*dt));
  
%Replot Plate  
  R = [cos(y(i,5)) sin(y(i,5)); -sin(y(i,5)) cos(y(i,5))];
  ppts = R*foil_pts;     
  set(h_foil,'XData',ppts(1,:),'YData',ppts(2,:));
    
  ppts = R*foil_pts;  
  for j = 1:length(ppts)
     ppts(1,j) = ppts(1,j)+y(i,1)+xoa(1);
     ppts(2,j) = ppts(2,j)+y(i,3)+xoa(3);
  end
  set(h_foil,'XData',ppts(1,:),'YData',ppts(2,:));

  

 %Replot Foil and AOA arrows.
  V = [ 0 ydot(i,1)*scale; 0 ydot(i,3)*scale];
  %set(h_V,'Visible','off'); clear h_V;

  %h_V = arrow(V(:,1),V(:,2));
  
  th = atan2(V(2,2),V(1,2));
  len = norm(V);
  V(1,1) = (Radius+len)*cos(th);
  V(2,1) = (Radius+len)*sin(th);
  V(1,2) = Radius*cos(th);
  V(2,2) = Radius*sin(th);  
  set(h_AOA,'Visible','off'); clear h_AOA;
  h_AOA = arrow(V(:,1),V(:,2),'EdgeColor','blue','FaceColor','blue');
  
    
  F = [ 0 Fforce{3}(i,1)*scaleF; 0 Fforce{3}(i,3)*scaleF];
  set(h_Force,'Visible','off'); clear h_Force;
  if(F(1,2) > 0)
    h_Force = arrow(F(:,1),F(:,2),'EdgeColor','green','FaceColor','green');
  else
    h_Force = arrow(F(:,1),F(:,2),'EdgeColor','red','FaceColor','red');
  end
  
  
%filename = ['foo' int2str(i), '.ppm'];
%print ('-dppmraw', filename);
%filename = ['foo' int2str(i), '.tif'];
%print ('-dtiff', filename);


%Frames(i) = getframe(gcf);
end

%map = colormap;
%mpgwrite(Frames,map,'example.mpg')
%clear Frames;


 