

% check orientation: 


clg

[p,e,t]=initmesh('circleg','hmax',.5); 
pdeplotn(p,e,t)
hold on


% check the orientation of the triangles 

ps = p(1,t(1:3,:)) .* p(2,t([2,3,1],:)) - p(2,t(1:3,:)) .* p(1,t([2,3,1],:));
ps = reshape(ps,3,size(t,2));
disp 'The negative orientation:'
find(sum(ps)<=0) 



%  'pick up the triangle #1'

disp 'pick up the triangle #1'


p(:,t(1:3,1)) * [1/3,1,0,0; 1/3,0,1,0; 1/3,0,0,1]; x=ans(1,:); y=ans(2,:); 
plot(x,y,'o'); 
[k,w]=inwhichtri(p,t,x,y) 

corners = p(:,t([1,2,3,1],k(1))); 
plot(corners(1,:),corners(2,:),'m');   % plot the triangle

for i=1:length(k);
  point  = p(:,t(1:3,k(i)))*w(:,i); 
  plot(point(1),point(2),'x'); 
end


% any triangle

disp('choose you a point in the graph (with mouse in figure 1)'); 
[x,y]=ginput(1); 
plot(x,y,'o'); 
[k,w]=inwhichtri( p,t,x,y) % calculate the triangle
corners = p(:,t([1,2,3,1],k)); 
plot(corners(1,:),corners(2,:),'m');   % plot the triangle
point  = p(:,t(1:3,k))*w; 
plot(point(1),point(2),'x'); 

disp 'the error '

disp( norm(point - [x;y] ))

