### Highlights from Distance from a point to polygon

4.75
4.8 | 4 ratings Rate this file 20 Downloads (last 30 days) File Size: 2.96 KB File ID: #19398 Version: 1.0

# Distance from a point to polygon

### Alejandro Weinstein (view profile)

31 Mar 2008 (Updated )

Calculate the distance from a point to 2-D polygon, and the point on the polygon closest to the poin

File Information
Description

Calculate the distance from a point to 2-D polygon, and the point on the polygon closest to the point.

This is based on the function made by Michael Yoshpe. Also include the test suggested by Eric Schmitz.

Acknowledgements

Distance From Points To Polyline Or Polygon inspired this file.

This file inspired Flow Cytometry Gui For Matlab.

MATLAB release MATLAB 7.5 (R2007b)
30 Mar 2014 Ilya

### Ilya (view profile)

To Michael Yoshpe's comment (about Eric Schmitz's comment):
why do we need "clear id"? By the way, it consumes quite a lot of time.

15 Jun 2013 liu

### liu (view profile)

04 Aug 2010 Diederick

### Diederick (view profile)

@ Michael. I don't think your reply to the above correction is correct. I have found wrong performance of your code due to numerical imprecision as well. When a rib is exactly vertical, or horizontal, the projected point, due to numerical error, might not lay exactly on the rib and then the test for whether the projected point is inside the line segment or not returns the wrong result and as a result the wrong distance is reported.

I have fixed it with slightly different code, though not importantly so:
% DN: correct projected point locations for exactly vertical or horizontal
% ribs, because of numerical precision problem the projection may not lay
% exactly on the rib, and then the test below to find if in segment errors
% out.
qvertical = B==0;
xp(qvertical)=xv(qvertical);
qhorizontal = A==0;
yp(qhorizontal)=yv(qhorizontal);

28 Nov 2008 Michael Yoshpe

### Michael Yoshpe (view profile)

The following code you added to my original function is unnecessary, since the formula that computes xp and yp automatically takes care of these cases:

% Test for the case where a polygon rib is
% either horizontal or vertical. From Eric Schmitz
id = find(diff(xv)==0);
xp(id)=xv(id);
clear id
id = find(diff(yv)==0);
yp(id)=yv(id);

The only case we should guard against is a case when BOTH A (-diff(yv)) and B (diff(xv)) are identically zero at some points. It effectively means that the input for polygon verices includes duplicate points, i.e. some ribs have zero length and no projection is possible. This case should be flagged out and the function should return an error.

Michael Yoshpe

Comment only
03 Apr 2008 John D'Errico

Reasonably good. A couple of minor points to quibble about.

There is no H1 line. When you wish to remember the name of that function you downloaded last year to find the closes point on a polygon, how will you remember the name p_poly_dist? You might want to try lookfor. Lookfor is a nice tool that searches the FIRST comment line of all the functions on your search path, looking for keywords. The first comment line here is sadly composed of just a bunch of asterisks. So the only way to find this function will be if you try

lookfor *