
close all;

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

t = get(Buoy,'t');

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

xoa = get(Plate,'x0');
xob = get(Buoy,'x0');

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



%MOOS Buoy
%buoy_pts = [-46 -46 -36.5  36.5 46 46 -46; 
%             23  -6 -20.5 -20.5 -6 23  23];  %inches
%buoy_pts = 0.0254*buoy_pts;  %meters 
%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 (A == 10)
  buoy_pts(1,:) = buoy_pts(1,:)*3.6/1.6;  %Scale up for larger buoy.
end
    
max_buoy_rng = max(max(abs(buoy_pts)));
current_buoy_center = [xob(1) xob(3)];




%Set up points for plate
xx = 0:.01:1;
yy = NACA_4Digit_Ordinates(.15,xx);
pts = [-xx(end:-1:1)+.25 -xx+.25; yy(end:-1:1) -yy];
pts = pts*Lc;
max_rng = max(max(abs(pts)));


xinc = current_buoy_center(1)-80*max_rng:160*max_rng/100:current_buoy_center(1)+80*max_rng;
eta0 = eta(wave,xinc,0);


%fig = figure('Position',[215 62 963 891]);
fig = figure('Position',[215 253 813 700]);

set(fig,'DoubleBuffer','on');
subplot(2,2,1)
h_buoy = plot(2*buoy_pts(1,:)+xob(1),2*buoy_pts(2,:)+xob(3),'k');
hold on;
axis equal;
foo(1) = current_buoy_center(1)-40*max_rng;
foo(2) = current_buoy_center(1)+40*max_rng;
foo(3) = current_buoy_center(2)-40*max_rng;
foo(4) = current_buoy_center(2)+40*max_rng;
axis(foo);
xlabel('m');
ylabel('m');
h_incwave = plot(xinc,eta0,'b');




%h_time = text(-10,10,['t = ',num2str(0.0,3)]); 
h_time=text(-10,10,sprintf('t = %4.1f',0*dt));

Vmax = norm(max(ydot(:,1:3)));
scale = 2*max_rng/Vmax;
V = [ 0 ydot(1,1)*scale; 0 ydot(1,3)*scale];

Radius = 1.25*max_rng;
xx = -Radius:Radius/100:Radius;
yy = sqrt(Radius^2-xx.^2);

set(gca,'nextplot','replace');

subplot(2,2,3)
h_plate = plot(15*pts(1,:)+xoa(1),15*pts(2,:)+xoa(3),'k');
axis equal;
set(h_plate,'EraseMode','xor');
%set(h_plate,'EraseMode','normal');
current_center = [xoa(1) xoa(3)];
foo(1) = current_center(1)-40*max_rng;
foo(2) = current_center(1)+40*max_rng;
foo(3) = current_center(2)-40*max_rng;
foo(4) = current_center(2)+40*max_rng;
axis(foo);
xlabel('m');
ylabel('m');
hold on;

set(gca,'nextplot','replace');
%set(gca,'nextplot','replacechildren');

scaleF = 2*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');

set(gca,'nextplot','replace');
%set(gca,'nextplot','replacechildren');

NN = min(45./dt,tf./dt);
%Frames = moviein(NN); % initialize the matrix 'Frames'

for i = 1:NN;  %length(t)

  pause(dt);
  R = [cos(x(i,5)) sin(x(i,5)); -sin(x(i,5)) cos(x(i,5))];
  ppts = 2*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,:));
  eta0 = eta(wave,xinc-x(i,1),i*dt);
  set(h_incwave,'Xdata',xinc,'YData',eta0);
  %set(h_time,'string',['t = ',num2str(i*dt,3)]);
  
  set(h_time,'string',sprintf('t = %4.1f',i*dt));
  
  R = [cos(y(i,5)) sin(y(i,5)); -sin(y(i,5)) cos(y(i,5))];
  ppts = R*pts;     
  set(h_plate,'XData',ppts(1,:),'YData',ppts(2,:));
  
  
  ppts = 15*R*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_plate,'XData',ppts(1,:),'YData',ppts(2,:));
  
  
  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');
  
  %[ivelx ively ivelz] = vel(wave,(xoa(1)+y(i,1))-(xob(1)+x(i,1)),(xoa(3)+y(i,3))-(xob(3)+x(i,3)),i*dt);
  %disp(['ivel =  ' num2str(ivelx) '  ' num2str(ivelz)]);
 
  %V = [ 0 ivelx*scale; 0 ivelz*scale];
  %set(h_INC,'Visible','off'); clear h_INC;
  %h_INC = 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
  
  %Frames(i) = getframe(gcf);
end

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


 