function A = jacobigui(B)

% GSGUI Prikaz Gauss-Seidelove metode za resevanje Poissonovega problema
% Vsak element v notranjosti matrike mora biti na koncu povprecje svojih
% stirih sosedov, vrednosti na robovih pa so fiksne

A=B;
[m,n] = size(A);
shg
clf
dx = 100;
dy = 50;
set(gcf,'double','on','name','Jacobi Gui', ...
   'menu','none','numbertitle','off','color','white', ...
   'pos',[480-(dx/2)*min(9,n), 320, 20+(n+1)*dx, (m+3)*dy], ...
   'windowbuttonupfcn','set(gcf,''tag'',''pivot'')')
axes('pos',[0 0 1 1])
axis off
Lcolor = [0 .65 0];
Ucolor = [0  0 .90];
Acolor = [0 0 0];
Grey = [.5 .5 .5];
PermColor=[1 0.5 0.5];
PartialPivotColor = [1 0 1];
CompletePivotColor = [0 1 0];
PivotColor = [1 0 0];
WhiteColor = [1 1 1];
psw=0.4

% Each element has its own handle
C=A;
for j = 1:n
   for i = 1:m
      t(i,j) = text('units','pixels','string',spf(A(i,j)), ...
         'fontname','courier','fontweight','bold','fontsize',14, ...
         'horiz','right','color',Acolor, ...
         'pos',[40+j*dx 20+(m+2-i)*dy],'userdata',[i j], ...
         'buttondownfcn','set(gcf,''userdata'',get(gco,''userdata''))');
      u(i,j) = text('units','pixels','string',spf(C(i,j)), ...
         'fontname','courier','fontweight','bold','fontsize',14, ...
         'horiz','right','color',WhiteColor, ...
         'pos',[40+j*dx 38+(m+2-i)*dy],'userdata',[i j], ...
         'buttondownfcn','set(gcf,''userdata'',get(gco,''userdata''))');
   end
end

% Glavna zanka
for step=1:4
   for i=2:n-1
      for j=2:m-1
         % pobarvamo sosede
         barva1=get(t(i,j-1),'color');
         barva2=get(t(i,j+1),'color');
         barva3=get(t(i-1,j),'color');
         barva4=get(t(i+1,j),'color');
         set(t(i,j-1),'string',spf(A(i,j-1)),'color',PivotColor)
         set(t(i,j+1),'string',spf(A(i,j+1)),'color',PivotColor)
         set(t(i-1,j),'string',spf(A(i-1,j)),'color',PivotColor)
         set(t(i+1,j),'string',spf(A(i+1,j)),'color',PivotColor)
         drawnow
         pause(psw)
         C(i,j) = (A(i,j+1)+A(i,j-1)+A(i+1,j)+A(i-1,j))/4;
         set(u(i,j),'string',spf(C(i,j)),'color',Lcolor)
         drawnow
         % sosede pobarvamo nazaj
         set(t(i,j-1),'string',spf(A(i,j-1)),'color',barva1)
         set(t(i,j+1),'string',spf(A(i,j+1)),'color',barva2)
         set(t(i-1,j),'string',spf(A(i-1,j)),'color',barva3)
         set(t(i+1,j),'string',spf(A(i+1,j)),'color',barva4)
         pause(psw)
      end
   end
   A=C;
   pause
   for i = 2:n-1
      for j = 2:m-1
         set(t(i,j),'string',spf(A(i,j)),'color',Acolor)
         set(u(i,j),'color',WhiteColor)
      end
   end
   drawnow
end

%------------------------------------------------------------

function s = spf(aij);
% Subfunction to format text strings
if aij == 0
   f = '%10.0f';
elseif (abs(aij) < 1.e-4) | (abs(aij) >= 1.e4) 
   f = '%10.1e';
else
   f = '%10.4f';
end
s = sprintf(f,aij);
