function[al]=line_patch2(x,y,n,tang,color,line_style)
xr=[];yr=[];



m=length(x);
im=[1:m-1];
xlim=get(gca,'xlim');
ylim=get(gca,'ylim');
pos=get(gca,'position');
ta=tang*((ylim(2)-ylim(1))/pos(4))/((xlim(2)-xlim(1))/pos(3));
if(tang==0)
  ta=1e-9;
end
if (ta>0)
x0=xlim(1)+(ylim(1)-ylim(2))/ta;
xb=linspace(x0,xlim(2),n);
xb1=xb+(-ylim(1)+ylim(2))/ta;
end
if(ta<0)
 x0=xlim(2)+(ylim(1)-ylim(2))/ta;
 xb=linspace(xlim(1),x0,n);
 xb1=xb+(-ylim(1)+ylim(2))/ta;
end
if(isnan(tang))
      ta=1e9;
  xb=linspace(xlim(1),xlim(2),n);
  xb1=xb+(-ylim(1)+ylim(2))/ta;
end
X=NaN;
Y=NaN;
for i=1:n
   
        [xi,yi]=line_inters(x(im),y(im),x(im+1),y(im+1),xb(i),ylim(1),xb1(i),ylim(2));
        ux=(xi-x(im)).*(xi-x(im+1));
         uy=(yi-y(im)).*(yi-y(im+1));
       ind=find(ux<=0 & uy<=0);
       
       nn= length(ind);
%xi(ind)
%yi(ind)
       if(nn~=0)
  stdx=std(xi(ind));
  stdy=std(yi(ind));
  if(stdx>=stdy)
      [xrab,i1,j1]=unique(xi(ind));
      yrab=yi(ind(i1));
  else
      [yrab,i1,j1]=unique(yi(ind));
      xrab=xi(ind(i1));
  end

  nr=prod(size(xrab));
 if(round(nr/2)*2==nr)
xr=reshape([reshape(xrab,2,nr/2); NaN*ones(1,nr/2)],1,nr/2*3);
yr=reshape([reshape(yrab,2,nr/2); NaN*ones(1,nr/2)],1,nr/2*3);
end
        X=[X xr];
        Y=[Y yr];
        end
  
end
if(nargin==4)
    color='k';
    line_style='-';
end
al=line(X,Y,'color',color,'linestyle',line_style,'linewidth',0.25);
set(gca,'xlim',xlim,'ylim',ylim,'layer','top')
 
function[xi,yi]=line_inters(x1,y1,x2,y2,x3,y3,x4,y4);
%find coordinates of the intersection of two lines
% that go through the points <1 , 2 >, and <3 , 4>
keyx=[x1==x2];
keyy=[y1==y2];

x3=x3*ones(size(x1));
y3=y3*ones(size(x1));
x4=x4*ones(size(x1));
y4=y4*ones(size(x1));
xi=((y3.*x4-y4.*x3).*(x2-x1)-(y1.*x2-y2.*x1).*(x4-x3))./((y2-y1).*(x4-x3)-(y4-y3).*(x2-x1)).*(1-keyx)+keyx.*x1;
yi=((x3.*y4-y3.*x4).*(y2-y1)-(x1.*y2-y1.*x2).*(y4-y3))./((x2-x1).*(y4-y3)-(x4-x3).*(y2-y1)).*(1-keyy)+keyy.*y1;
