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);


% 
% 
% 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
    


