% illustration of interference of two point sources
clear all;
close all;


L=12;
N=800;
k= 5;
lambda=(2*pi)/k;
SC=2*L/N;
I=sqrt(-1);

%sep = 4;
sep = 0.5*(lambda*5.5); % hay que dividir por dos porque ambas fuentes se mueven en sep, es decir dist = 2*sep.
FASE=0;
dfi=exp(I*FASE);
P=[1:20]/2.753;
seps=(lambda*P)/2;
%seps=sort([-seps,seps]);

%seps=[-sep,0,sep];
seps=[-2.5,-1.5,-0.5,0.5,1.5,2.5]
%seps=[-2.5,2.5]

%seps=[-0.5,0.5]
%seps=[-1.5,1.5]

V=linspace(-L, L, N);
[X, Y] = meshgrid(V, V);
 
Z=zeros(size(X));

%R1= sqrt( (X-sep).^2 + Y.^2 );
%R2= sqrt( (X+sep).^2 + Y.^2 );

for i=1:length(seps)
    temp=sqrt( (X-seps(i)).^2 + Y.^2 );
    Z=Z+exp(I*k*temp)./temp;
end


% Sum of Green's functions for two point sources
%Z = dfi*exp(I*k*R1)./R1 + exp(I*k*R2)./R2;
 
M=10;
T=linspace(0.0, 2*pi, M); T=T(1:(M-1));
cut = 0.8;
scale = 55/(2*cut);
 
for p=1:1
   for iter=1:length(T)
 
      figure(1); clf; hold on;
 
      W = real(Z*exp(-I*T(iter)));
      W = max(W, -cut);
      W = min(W, cut);
 
      image(scale*(W+cut));
      axis equal; axis off;
 
      file=sprintf('Frame%d.png', 1000+iter);
      disp(sprintf('Saving to %s', file));
      print('-dpng',  '-zbuffer',  '-r100', file);
      pause(0.1);
 
   end
end
 
% saved to gif with the command
% convert -density 100 -loop 1000 -delay 10 Frame100* Two_sources_interference.gif
% then cropped and scaled in Gimp
