function [meshele]=maketri2(L,meshele,meshnod)
% [meshelenew]=maketri2(L,meshele,meshnod);
%  Select three points of array x,y to make a triangle
% then select another point to use last two plus new one for 
%  new triangle
% terminate by clicking to too small an x
% L = min and max for x,y axis
%
disp(['maketri'])
disp(['Click on three nodes to define triangle element'])
disp(['Click on another node and use most recent three'])
disp(['Finish one set and start another selecting NEW'])
disp(['Finish whole routine by selecting QUIT'])
disp(['Undo the last triangle and start a new one  UNDO'])
disp(['Clicking too fast can mess up, beep tells you it is o.k. to click'])
sou = ones(100,1);  
l = 1:2:99;
sou(l) = 0*sou(l);
sou2 = ones(100,1);
l = 1:4:99;  sou2(l) = 0*sou2(l);
l = 2:4:99;  sou2(l) = 0*sou2(l);


x = meshnod(:,2);
y = meshnod(:,3);
ld = 1:length(x);

clf
plot(x(ld),y(ld),'g*')
axis(L)
axis('equal')
title('maketri.m Select Points After Beeps')

hold on

if meshele ~= []
n = length(meshele);
meshtri = meshele(:,:);
meshtri(:,1) = meshtri(:,4);
x4 = meshtri';
y4 = meshtri';
x4(:) = x(meshtri');
y4(:) = y(meshtri');
fill(x4,y4,'w')
else
meshele = [];
n = 0;
end

pbquit = uicontrol(gcf,'Style','radio','String','QUIT','Value',0 ...
 ,'units','normalized','position',[.02 .05 .107 .047] );
pbnew = uicontrol(gcf,'Style','radio','String','NEW','Value',1 ...
 ,'units','normalized','position',[.02 .1 .107 .047] );
pbundo = uicontrol(gcf,'Style','radio','String','UNDO','Value',0 ...
 ,'units','normalized','position',[.02 .15 .107 .047] );


while(1)   

if ~get(pbnew,'value')   % First triangle exception
  sound(sou)
  [xin,yin]=ginput(1);
  drawnow
end
  drawnow

if get(pbquit,'value')
  disp('  QUIT movenod')
  break
end


if ~get(pbundo,'value') & ~get(pbnew,'value') & ~get(pbquit,'value') 
%  Process the third input point of a set.
ele(1) = ele(2);
ele(2) = ele(3);
d = (x(ld)-xin).^2+(y(ld)-yin).^2;
ele(3) = ld(find(d==min(d)));

%    Check for right handed triangle
x1 = meshnod(ele(1),2);
x2 = meshnod(ele(2),2);
x3 = meshnod(ele(3),2);
y1 = meshnod(ele(1),3);
y2 = meshnod(ele(2),3);
y3 = meshnod(ele(3),3);
z = cross(([x1 y1 0]-[x2 y2 0]),([x2 y2 0]-[x3 y3 0]));
disp([ ele z(3) ])
if abs(z(3))<0.000001 
%  zero area points are colinear or paired
disp('Colinear, no triangle')
elseif z(3)>0
% points are in correct order
%disp('Righthanded triangle')
n = n+1;
meshele=[meshele ; [n ele]];
else
% points are in wrong order
%disp('Backwards triangle  corrected')
n = n+1;
meshele=[meshele ; [n ele(1) ele(3) ele(2) ]];
end


k = findobj('color',[0 0 1],'parent',gca)
delete(k)         %The blue stars 
fill(x(ele),y(ele),'c')
%fill(x(ele),y(ele),'w')
plot(x(ele(2:3)),y(ele(2:3)),'b*')
% plot(x(ele(2:3)),y(ele(2:3)),'b*',x(ele(1)),y(ele(1)),'g*')
%end
drawnow
end    %  End of Process the third input point of a set.

if get(pbnew,'value')  %  restart a set
disp(['Restart a new set, click three times'])
k = findobj('color',[0 0 1],'parent',gca)
delete(k)         %The blue stars 
set(pbnew,'value',0)
for i = 2:3
  sound(sou2)
  [xin, yin] = ginput(1);
  drawnow
  if get(pbquit,'value') | get(pbnew,'value')
    break
  end
  d = (x(ld)-xin).^2+(y(ld)-yin).^2;
  ele(i) = ld(find(d==min(d)));
hold on
plot(x(ele(i)),y(ele(i)),'bo')
end
  if get(pbquit,'value')
    disp('  QUIT movenod')
    break
  end
end

if get(pbundo,'value')  
%undo button pressed undo the last one
disp(' UNDO')
[l ,junk]= size(meshele);
meshele= meshele(1:(l-1),:);
n = n-1;

% Replot it without the last triangle
hold off
plot(x,y,'g*')
axis(L)
axis('equal')
title('maketri.m Select Points After Beeps')
hold on
meshtri = meshele(:,:);
meshtri(:,1) = meshtri(:,4);
x4 = meshtri';
y4 = meshtri';
x4(:) = x(meshtri');
y4(:) = y(meshtri');
fill(x4,y4,'w')
plot(x(ele(2:3)),y(ele(2:3)),'b*')
set(pbundo,'value',0)
set(pbnew,'value',1)
drawnow
end

  if get(pbquit,'value')
    disp('  QUIT movenod')
    break
  end

end   % END of MAIN WHILE LOOP

hold off
meshelenew = meshele;
delete(pbundo)
delete(pbquit)
delete(pbnew)
