function fourants(d,step)
% Four ants simulation (quick and dirty :) )
%
% Usage: 	fourants
% or		fourants(InitialDistanse,Step);
%
% Example: 	fourants(2,0.005)
%
% For Matlab 5.x		 
% File: fourants.m						 
% Copyright (c) Igor Kagan					 
% kigor@tx.technion.ac.il
% http://igoresha.virtualave.net
% Last modified 20.08.00

if nargin < 2
	d=1;
	step = 0.001;
end


figure
set(gca,'Drawmode','fast','NextPlot','add','box','on','Xlim',[0 d],'Ylim',[0 d]);
axis('square');

antA = [0 0];
antB = [0 d];
antC = [d d];
antD = [d 0];

while d>step
	p_antA = antA;
	antA = makestep(antA,antB,step);
	antB = makestep(antB,antC,step);
	antC = makestep(antC,antD,step);
	antD = makestep(antD,p_antA,step);
	plot(antA(1),antA(2),'r.','EraseMode','background'); hold on;
	plot(antB(1),antB(2),'b.','EraseMode','background'); hold on;		
	plot(antC(1),antC(2),'g.','EraseMode','background'); hold on;
	plot(antD(1),antD(2),'c.','EraseMode','background'); hold on;
	drawnow;
	d = dist(antA,antB');
end

function newloc=makestep(l1,l2,step)

x1 = l1(1);
y1 = l1(2);
x2 = l2(1);
y2 = l2(2);

ta = (y2-y1)/(x2-x1+eps);
a = atan(ta);
y = y1+abs(sin(a)*step)*sign(y2-y1);
x = x1+abs(cos(a)*step)*sign(x2-x1);

newloc = [x,y];
