% for each triangle, propogate the other points until they
% hit a boundary segment
% propogate4 draws figures and propogates two points for each
% side of each triangle

function propogate4(x,y,tri,radii,B)
tic
format short;
epsilon=.0001;
prop_limit=1000; % max steps of flow
count=0;
N=size(tri,1); % number of triangles
starts=zeros(1,3*N);
for n=1:N
for v=1:3
[n,v,N,toc,count]
v1=v;
v2=mod(v,3)+1;
a=tri(n,v1);
b=tri(n,v2);
flag=in_bdy(a,b,B);
old=n;
za=x(a)+i*y(a);
zb=x(b)+i*y(b);
z=za+radii(n,v)*(zb-za)/abs(zb-za);
count=count+1;
z0=z;
starts(6*(n-1)+v)=z0;
%scatter(real(z),imag(z),'g','filled','MarkerEdgeColor','k');
%scatter(real(za),imag(za),'c');
%scatter(real(zb),imag(zb),'b');
counter=0;
while (flag==0)&(counter<prop_limit)
counter=counter+1;
new=other_tri(a,b,old,tri);
[z,z0,new,old];
[a,b,n,v,counter];
c=setdiff(tri(new,:),[a,b]);
w1=find(tri(new,:)==a);
w2=find(tri(new,:)==b);
w3=setdiff(tri(new,:),[a,b]);
r1=abs(z-za);
r2=abs(z-zb);
if r1 < radii(new,w1)
r=r1;
a=a;
c=b;
b=w3;
old=new;
elseif r2<radii(new,w2)
r=r2;
c=a;
a=b;
b=w3;
old=new;
else
flag=1;
break;
end
za=x(a)+i*y(a);
zb=x(b)+i*y(b);
zc=x(c)+i*y(c);

z=za+r*(zb-za)/abs(zb-za) ;
count=count+1;
scatter(real(z),imag(z),'k','filled')
r;
plot_prop_arc(za,zb,zc,r)
flag=in_bdy(a,b,B);
if abs(z-z0)<epsilon
flag=1;
break
end
end % while

v1=v;
v2=mod(v,3)+1;
a=tri(n,v1);
b=tri(n,v2);
flag=in_bdy(a,b,B);
old=n;
za=x(a)+i*y(a);
zb=x(b)+i*y(b);
z=zb+radii(n,v2)*(za-zb)/abs(za-zb);
count=count+1;
z0=z;
starts(6*(n-1)+3+v)=z0;
%scatter(real(z),imag(z),'g','filled','MarkerEdgeColor','k');
%scatter(real(za),imag(za),'c');
%scatter(real(zb),imag(zb),'b');
counter=0;
while (flag==0)&(counter<100)
counter=counter+1;
new=other_tri(a,b,old,tri);
[z,z0,new,old];
[a,b,n,v,counter];
c=setdiff(tri(new,:),[a,b]);
w1=find(tri(new,:)==a);
w2=find(tri(new,:)==b);
w3=setdiff(tri(new,:),[a,b]);
r1=abs(z-za);
r2=abs(z-zb);
if r1 < radii(new,w1)
r=r1;
a=a;
c=b;
b=w3;
old=new;
elseif r2<radii(new,w2)
r=r2;
c=a;
a=b;
b=w3;
old=new;
else
flag=1;
break;
end
za=x(a)+i*y(a);
zb=x(b)+i*y(b);
zc=x(c)+i*y(c);

z=za+r*(zb-za)/abs(zb-za) ;
count=count+1;
scatter(real(z),imag(z),'k','filled')
r;
plot_prop_arc(za,zb,zc,r)
flag=in_bdy(a,b,B);
if abs(z-z0)<epsilon
flag=1;
break
end
end % while

end % for v
end % for n
scatter(real(starts),imag(starts),'g','filled','MarkerEdgeColor','k');
count

return