function [Point,Distance] = find_nearest_point(xloc,yloc,x, y) 
% Find nearest point in Mesh structure to (x,y) 
%
% function [Point,Distance] = find_nearest_point(xloc,yloc,x,y) 
%
% DESCRIPTION:
%    Find nearest point to (xloc,yloc) in the domain of Mesh
%    using native coordinates (x,y)
%
% INPUT:
%   xloc   = x location of point (in native Mobj coordinates)
%   yloc   = y location of point (in native Mobj coordinates)
%   x, y    = Mesh nativeCoords.
%
% OUTPUT:
%   Point = index of nearest vertex in the mesh
%   Distance = Distance from x,y to Point in Mobj native coordinates
%
% EXAMPLE USAGE
%    [Point,Distance] = find_nearest_point(50.1,100.2,Mobj)
%
% Author(s):  
%
% Revision history
%
%==============================================================================

global ftbverbose 

% global ftbverbose
subname = 'find_nearest_point';
if ftbverbose
    fprintf('\nbegin : %s\n', subname)
end

%------------------------------------------------------------------------------
% Parse input arguments
%------------------------------------------------------------------------------
if ~exist('xloc', 'var') || ~exist('yloc', 'var') || ~exist('x', 'var') || ~exist('y','var')
	error('arguments to %s are missing', subname)
end


%------------------------------------------------------------------------------
% Find the nearest point
%------------------------------------------------------------------------------
[Distance, Point] = min(sqrt((xloc - x).^2 + (yloc - y).^2));

if ftbverbose
    fprintf(['end   : ' subname '\n'])
end

end

