%function [i,w] = inwhichtri(p,t,x,y)
%
% returns 'i' a column of 't' containing the point (x,y)
% 'w' are the canonical coordinates of the triangle 
% (Due to numerical instability the 'i' might not be the first triangle
% in list corresponding to the point. 
%
% see exwhichtri for an example
%
% last edited April 16th, 1997 (c) jvpf
 
function [i,w] = inwhichtri(p,t,x,y)

n = min(length(x), length(y)); 
k = size(t,2); 
[area,w0,wx,wy]=triproperties(p,t); 
for j=1:n;
  wj = w0 + x(j)*wx +y(j)*wy; 
  f = find(all(wj >= -eps)); % here we relied that the orientation is positive!
  if length(f) >= 1, 
     f = f(1);  % the point might be on an edge, so return only one tri.
     i(j)  = f;  
     w(:,j) = wj(:,f)/area(f);
   end
end

  




