function meshnodnew = rmtrinod(L,meshnod,lhighlight)
%   meshnodnew = rmtrinod(L,meshnod,lhighlight);
% Three clicks define a triangle used to delete nodes from meshnod
%  lhighlight is optional,  meshnod(lhighlight,2:3) will be plotted as red *
clear global
clf
zoom off
hold off
if nargin==2
plot(meshnod(:,2),meshnod(:,3),'g*')
else
plot(meshnod(:,2),meshnod(:,3),'g+',...
   meshnod(lhighlight,2),meshnod(lhighlight,3),'r*')
end

grid
title('Remove Nodes in triangle. Click on three points of triangle')
axis('equal'); axis(L)
figure(gcf)
[XY]=ginput(3)
x1 = XY(1,1); y1 = XY(1,2);
x2 = XY(2,1); y2 = XY(2,2);
x3 = XY(3,1); y3 = XY(3,2);

z = cross(([x1 y1 0]-[x2 y2 0]),([x2 y2 0]-[x3 y3 0]));
if z(3)<0 
%  negative area change places of two points
disp([' z is negative ',num2str(z)])
XY(2,:) = [ x3 y3];
XY(3,:) = [x2 y2];
end



slope1 = (XY(2,2)-XY(1,2))/(XY(2,1)-XY(1,1));
c1 = XY(2,2)-slope1*XY(2,1);
slope2 = (XY(3,2)-XY(2,2))/(XY(3,1)-XY(2,1));
c2 = XY(2,2)-slope2*XY(2,1);
slope3 = (XY(1,2)-XY(3,2))/(XY(1,1)-XY(3,1));
c3 = XY(3,2)-slope3*XY(3,1);

if XY(3,2) > (slope1*XY(3,1)+c1)
l1 = find(meshnod(:,3) < (slope1*meshnod(:,2)+c1) );
else
l1 = find(meshnod(:,3) > (slope1*meshnod(:,2)+c1) );
end

if XY(1,2) > (slope2*XY(1,1)+c2)
l2 = find(meshnod(:,3) < (slope2*meshnod(:,2)+c2) );
else
l2 = find(meshnod(:,3) > (slope2*meshnod(:,2)+c2) );
end

if XY(2,2) > (slope3*XY(2,1)+c3)
l3 = find(meshnod(:,3) < (slope3*meshnod(:,2)+c3) );
else
l3 = find(meshnod(:,3) > (slope3*meshnod(:,2)+c3) );
end

size(l1)
size(l2)
size(l3)

l = union( union(l1,l2),l3);
%l = l3;
meshnodnew= meshnod(l,:);


%hold on
%plot(meshnodnew(:,2),meshnodnew(:,3),'r.')
%grid
%title('Red dots are the saved dots')
%hold off
