


Find nearest point in Mesh structure to (x,y)
function [Point,Distance] = find_nearest_pt(xloc,yloc,Mobj)
DESCRIPTION:
Find nearest point to (xloc,yloc) in the domain of Mobj
using native coordinates of Mobj
INPUT:
xloc = x location of point (in native Mobj coordinates)
yloc = y location of point (in native Mobj coordinates)
Mobj = Mesh object with the following fields:
- nativeCoords = grid type (cartesian or spherical)
- x, y and/or lon, lat = coordinates (dependent on
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):
Geoff Cowles (University of Massachusetts Dartmouth)
Pierre Cazenave (Plymouth Marine Laboratory)
Revision history
2015-01-14 Tidy up the code a bit and add extra information to the
help.
==============================================================================

0001 function [Point,Distance] = find_nearest_pt(xloc,yloc,Mobj) 0002 % Find nearest point in Mesh structure to (x,y) 0003 % 0004 % function [Point,Distance] = find_nearest_pt(xloc,yloc,Mobj) 0005 % 0006 % DESCRIPTION: 0007 % Find nearest point to (xloc,yloc) in the domain of Mobj 0008 % using native coordinates of Mobj 0009 % 0010 % INPUT: 0011 % xloc = x location of point (in native Mobj coordinates) 0012 % yloc = y location of point (in native Mobj coordinates) 0013 % Mobj = Mesh object with the following fields: 0014 % - nativeCoords = grid type (cartesian or spherical) 0015 % - x, y and/or lon, lat = coordinates (dependent on 0016 % nativeCoords). 0017 % 0018 % OUTPUT: 0019 % Point = index of nearest vertex in the mesh 0020 % Distance = Distance from x,y to Point in Mobj native coordinates 0021 % 0022 % EXAMPLE USAGE 0023 % [Point,Distance] = find_nearest_point(50.1,100.2,Mobj) 0024 % 0025 % Author(s): 0026 % Geoff Cowles (University of Massachusetts Dartmouth) 0027 % Pierre Cazenave (Plymouth Marine Laboratory) 0028 % 0029 % Revision history 0030 % 2015-01-14 Tidy up the code a bit and add extra information to the 0031 % help. 0032 % 0033 %============================================================================== 0034 0035 % global ftbverbose 0036 subname = 'find_nearest_pt'; 0037 % if ftbverbose 0038 % fprintf('\nbegin : %s\n', subname) 0039 % end 0040 0041 %------------------------------------------------------------------------------ 0042 % Parse input arguments 0043 %------------------------------------------------------------------------------ 0044 if ~exist('xloc', 'var') || ~exist('yloc', 'var') || ~exist('Mobj', 'var') 0045 error('arguments to %s are missing', subname) 0046 end 0047 0048 %------------------------------------------------------------------------------ 0049 % Set native coordinates 0050 %------------------------------------------------------------------------------ 0051 if strcmpi(Mobj.nativeCoords, 'cartesian') 0052 x = Mobj.x; 0053 y = Mobj.y; 0054 elseif strcmpi(Mobj.nativeCoords, 'spherical') 0055 x = Mobj.lon; 0056 y = Mobj.lat; 0057 else 0058 error('Unrecognised coordinate type.') 0059 end 0060 0061 %------------------------------------------------------------------------------ 0062 % Find the nearest point 0063 %------------------------------------------------------------------------------ 0064 [Distance, Point] = min(sqrt((xloc - x).^2 + (yloc - y).^2)); 0065 0066 % if ftbverbose 0067 % fprintf(['end : ' subname '\n']) 0068 % end 0069 0070