function [node,elem,flag] = check(node,elem,minel,maxel)
%[node,elem] = check(node,elem,minel,maxel)
%  node: The nodal positions
%  elem: The element connectivities
% minel: The minimum element size
% maxel: The maximum element size

%Set the initial value for the flag. 0 is good
flag=0;

%The sizes
nel=size(elem,1);
nnode=size(node,1);

%Determines the length and angle of each element
[l,angle]=len(node,elem);


while and(min(l)<0.1,flag==0)
    
    [y,i]=min(l);
    
    connec=zeros(nnode,3);
for ss=1:nnode
   index=1;
    for kk=1:nel
        if elem(kk,1)==ss || elem(kk,2)==ss
            connec(ss,index)=kk;
            index=index+1;
        end
    end
      
end
    
    
    if nnz(connec(elem(i,2),:))==1 || nnz(connec(elem(i,2),:))==3
      
   delnode=elem(i,1);
        
         if(connec(delnode,1)==i)
            reconele=connec(delnode,2);
        else
            reconele=connec(delnode,1);
        end
        
        if(elem(reconele,1)==delnode)
            elem(reconele,1)=elem(i,2);
        else
            elem(reconele,2)=elem(i,2);
        end
                 
        
        node(delnode,:)=[];
       
        for ii=1:nel
            node1=elem(ii,1);
            node2=elem(ii,2);
            if node1>delnode
                elem(ii,1)=elem(ii,1)-1;
            end
            if node2>delnode
                elem(ii,2)=elem(ii,2)-1;
            end
        end
        
        elem(i,:)=[];
        
    else
        delnode=elem(i,2);
        
         if(connec(delnode,1)==i)
            reconele=connec(delnode,2);
        else
            reconele=connec(delnode,1);
        end
        
        if(elem(reconele,1)==delnode)
            elem(reconele,1)=elem(i,1);
        else
            elem(reconele,2)=elem(i,1);
        end
                 
        
        node(delnode,:)=[];
       
        for ii=1:nel
            node1=elem(ii,1);
            node2=elem(ii,2);
            if node1>delnode
                elem(ii,1)=elem(ii,1)-1;
            end
            if node2>delnode
                elem(ii,2)=elem(ii,2)-1;
            end
        end
        
        elem(i,:)=[];
              
    end
    
%     
%     delnode=elem(i,2);
%     node(delnode,:)=[];
%     elem(nel,:)=[];
%     elem(nel-1,2)=1;
    
    nel=size(elem,1);
    nnode=size(node,1);
    
    
    
    % 
for ff=1:length(elem(:,1))
%     for k=1:i
        n1=elem(ff,1); n2=elem(ff,2);
        x11=[node(n1,1),node(n2,1)];
        y11=[node(n1,2),node(n2,2)];
        if elem(ff,3)==1
        plot(x11,y11,'k.-')
        else
          plot(x11,y11,'r.-')
        end
        hold on
end

    hold off;
    
    
    
    
    
    
    [l,angle]=len(node,elem);
    
    if nel<3
        flag=1;
    end
end

flag==0;

while and(max(l)>1.5,flag==0)
    [y,i]=max(l);
    
    
    node1=elem(i,1);
    node2=elem(i,2);
    
    x1=node(node1,1);
    y1=node(node1,2);
    x2=node(node2,1);
    y2=node(node2,2);
    
    node(nnode+1,1)=(x1+x2)/2;
    node(nnode+1,2)=(y1+y2)/2;
    
    
    
    elem(nel+1,2)=elem(i,2);
    elem(i,2)=nnode+1;
    elem(nel+1,1)=nnode+1;
    if elem(i,3)==1
        elem(nel+1,3)=1;
    else
        elem(nel+1,3)=0;
    end
   

    nel=size(elem,1);
    nnode=size(node,1);
    
    [l,angle]=len(node,elem);
 
end













% 
% 
% connec=zeros(nnode,3);
% for i=1:nnode
%    index=1;
%     for kk=1:nel
%         if elem(kk,1)==i || elem(kk,2)==i
%             connec(i,index)=kk;
%             index=index+1;
%         end
%     end
%       
% end
% 
% 
% 
% for i=1:nnode
%     if nnz(connec(i,:))==2
%        
%         if abs(angle(connec(i,1))-angle(connec(i,2)))<0.01
% %         node(i,:)=node(i,:)*(0.99+(1.01-0.99)*rand);    
%               node(i,:)=node(i,:)*(0.999+(1.001-0.999)*rand);   
%         elseif abs(angle(connec(i,1))-angle(connec(i,2)))>(pi-0.01) && abs(angle(connec(i,1))-angle(connec(i,2)))<(pi+0.01)
%               node(i,:)=node(i,:)*(0.999+(1.001-0.999)*rand);  
%         end
%     
%     elseif nnz(connec(i,:))==3
%            if abs(angle(connec(i,1))-angle(connec(i,2)))<0.01
%          node(i,:)=node(i,:)*(0.999+(1.001-0.999)*rand);  
%         elseif abs(angle(connec(i,1))-angle(connec(i,2)))>(pi-0.01) && abs(angle(connec(i,1))-angle(connec(i,2)))<(pi+0.01)
%            node(i,:)=node(i,:)*(0.999+(1.001-0.999)*rand);  
%            end
%            
%              if abs(angle(connec(i,1))-angle(connec(i,3)))<0.01
%           node(i,:)=node(i,:)*(0.999+(1.001-0.999)*rand);  
%         elseif abs(angle(connec(i,1))-angle(connec(i,3)))>(pi-0.01) && abs(angle(connec(i,1))-angle(connec(i,3)))<(pi+0.01)
%              node(i,:)=node(i,:)*(0.999+(1.001-0.999)*rand);  
%              end
%                if abs(angle(connec(i,2))-angle(connec(i,3)))<0.01
%          node(i,:)=node(i,:)*(0.999+(1.001-0.999)*rand);  
%                 elseif abs(angle(connec(i,2))-angle(connec(i,3)))>(pi-0.01) && abs(angle(connec(i,2))-angle(connec(i,3)))<(pi+0.01)
%              node(i,:)=node(i,:)*(0.999+(1.001-0.999)*rand);  
%                 end
%         
%     else
%     end
%  end




















%If any elements are too small, then fix them
% while and(min(l)<minel,flag==0)
%     [y,i]=min(l);
%     delnode=elem(i,2);
%     node(delnode,:)=[];
%     elem(nel,:)=[];
%     elem(nel-1,2)=1;
%     
%     nel=size(elem,1);
%     nnode=size(node,1);
%     
%     [l,angle]=len(node,elem);
%     
%     if nel<3
%         flag=1;
%     end
% end
% 
% %IF any elements are too big, then split them
% while and(max(l)>maxel,flag==0)
%     [y,i]=max(l);
%     
%     
%     node1=elem(i,1);
%     node2=elem(i,2);
%     
%     x1=node(node1,1);
%     y1=node(node1,2);
%     x2=node(node2,1);
%     y2=node(node2,2);
%     
%     temp=zeros(nnode+1,2);
%     
%     if i==elem(nel,1)
%         node(node1+1,1)=(x1+x2)/2;
%         node(node1+1,2)=(y1+y2)/2;    
%     else
%         temp(1:node1,:)=node(1:node1,:);    
%         temp(node1+1,1)=(x1+x2)/2;
%         temp(node1+1,2)=(y1+y2)/2;    
%         temp(node2+1:nnode+1,:)=node(node2:nnode,:);
%         node=temp;
%     end
% 
%     elem(nel,2)=nel+1;
%     elem(nel+1,1)=nel+1;
%     elem(nel+1,2)=1;     
% 
%     nel=size(elem,1);
%     nnode=size(node,1);
%     
%     [l,angle]=len(node,elem);
%  
% end

% To ensure that two elements are not inline with each other, assign a
% random deviation to the x and y coordinates of a random number between 
% 0.99 and 1.01 times the current position
% if abs(angle(1)-angle(nel))<0.01
%     node(1,:)=node(1,:)*(0.99+(1.01-0.99)*rand);
% end
% for i=2:nel
%     if abs(angle(i)-angle(i-1))<0.01
%         node(i,:)=node(i,:)*(0.99+(1.01-0.99)*rand);    
%     end
% end
    


