function num = poly_match(X,Z,v)

% POLY_MATCH matches points and polygons.

%            NOTE: If there are NESTED polygons, then
%                  then this routine only finds the FIRST
%                  polygon to which the point belongs.

% FUNCTIONS CALLED: polyform.m, pt_in_poly.m

% INPUTS: X = boundary file in format of 'polyform.m'
%         Z = output of polyform.m
%         v = vector of points to be matched


% OUTPUTS: num = vector of polygon numbers

Z = polyform(X) ;

k = length(Z) ; %number of polygons

u = ones(k,1) ;

p = length(v(:,1)); %number of points

num = zeros(p,1) ;

i = 1 ;

while i <= p
   
   x = v(i,:);
   
   j = 1 ;
   
   while j <= k 
                
   	% Define current polygon
   
      first = Z(j,1) ;
   
   	last = Z(j,2) ;
   
   	P = X(first:last,:) ;    
               
   	if pt_in_poly(x,P) == 1
         
         num(i) = j ;
         
         break
         
      end
      
      j = j + 1 ;
      
   end %end polygon search
   
   i = i + 1 
   
end 

   
         	
