4.66667

4.7 | 6 ratings Rate this file 13 Downloads (last 30 days) File Size: 1.99 KB File ID: #12744

Distance from a point to polygon

by Michael Yoshpe

 

22 Oct 2006 (Updated 23 Oct 2006)

Distance from a point to 2-D polygon

| Watch this File

File Information
Description

%*******************************************************************************
% function: p_poly_dist
% Description: distance from pont to polygon whose vertices are specified by the
% vectors xv and yv
% Input:
% x - point's x coordinate
% y - point's y coordinate
% xv - vector of polygon vertices x coordinates
% yv - vector of polygon vertices x coordinates
% Output:
% d - distance from point to polygon (defined as a minimal distance from
% point to any of polygon's ribs, positive if the point is outside the
% polygon and negative otherwise)

Acknowledgements
This submission has inspired the following:
Distance from a point to polygon
MATLAB release MATLAB 6.0 (R12)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
24 Apr 2007 Kardi Teknomo  
21 Oct 2007 Jesse Collins

this function would benefit greatly from also returning the point on the given polygon closest to the point from which the distance is to be found.

09 Jan 2008 Eric Schmitz

For the case where a polygon rib is either horizontal or vertical, the test for checking if the projected point is inside a segment may not work all the time because of numerical accuracy. I added a test to force either the x or y coordinate of projected point to be set equal to the corresponding rib x or y coordinate (before the section computing idx_x and idx_y):

id = find(diff(xv)==0);
xp(id)=xv(id);
clear id
id = find(diff(yv)==0);
yp(id)=yv(id);

01 Apr 2008 Alejandro Weinstein

I added the suggested test from Eric Schmitz, and also return the point of the polygon closest to the point. It is available here:
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=19398&objectType=file

06 Feb 2010 Cristian D

Thanks for this function! it was very usefull!

22 Jul 2010 Xavier Nieves

Hi, I have been trying to create a function similar to this one... if I were to have a random polygon but always have as an input the x,y coordinates of the vertices's and and the x,y coordinate of a point. can I use this function to determine the minimum distance of the point to the polygon, and if I can should express the inputs in what way... ?? nice function

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
geometric transformation Michael Yoshpe 22 Oct 2008 08:45:01
image registration Michael Yoshpe 22 Oct 2008 08:45:01
polygon point distance Michael Yoshpe 22 Oct 2008 08:45:01

Contact us at files@mathworks.com