Finding the distance between a polygon and a point


I’m trying to find the distance between a point and a polygon (as represented by a vector of ofPoints).

I’ve been attempting to implement the Paul Bourke approach described here:

but I’m getting incorrect results.

My attempt is here:

My strategy was to apply the Bourke function for each segment of the polygon and then to return the smallest of those results as the answer. But something is going wrong.

Would love some help figuring out this math.


– Greg

Never mind. I figured it out. Went back and based my implementation on Bourke’s java example rather than the c example and I got it working. Here’s the code:

That snippet has both distanceFromLine and distanceFromPolygon, which uses distanceFromLine on each segment to get the result.

Hope this is useful for someone!

this is already inside OF, see ofPolyline::getClosestPoint()

for closest point on a line, this method uses getClosestPointUtil() inside the cpp, but it’s not exposed via the header.

Gah! Seeing it now in the header on GH. Oh, well. At least I learned something about Bourke’s algorithm :slight_smile:

Any particular reason that there’s nothing about ofPolyline on Also, I came across ofInsidePoly() in the course of this adventure, which takes a vector of points rather than an ofPolyline. And now looking at ofPolyline it doesn’t look like it has an equivalent function so I’d need to store the points of my polygon both as a vector of ofPoints and as an ofPolyline, eh?

Oh and thanks :slight_smile:

PS. I’d love to pitch in on improving the documentation in some way. Anything I can do to help? James and I are working on a thing to make it easier to find addons. Hopefully we’ll have something to show soon.

ofInsidePoly() is pre-ofPolyline, and should be turned into a static function of ofPolyline at some point. i just added an issue

regarding documentation, there’s at least three threads on the development subforum about this. i think what needs to happen is someone/people need to be propose a solution, be told that their solution will be incorporated into the website/download so they know they’re not wasting their time, and then do it in one pass… but we’ll see what happens.

So, since I’m familiar with it at this point, I started working on trying to create a patch that would solve that issue. I made the changes in my own local copy of OF at first so that I could get it to compile with the re-written version of the code I was asking about here. I made the changes Math.h/Math.cpp and ofPolyline.h/ofPolyine.cpp. Everything compiles but I’m getting a linker error:

Undefined symbols for architecture i386:
“ofPolyline::inside(ofVec3f const&, ofPolyline const&)”, referenced from:
testApp::draw() in testApp.o
d: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status

(ofPolyline::indside() is the new static method I added to ofPolyline with the updated guts of the former ofInsidePolygon().)

I tried cleaning both my app and OF as targets as well as deleting the .a file from openFrameworksCompiled/lib/osx/

I’m sure it’s something simple I’m missing to get this to compile and then I can test and move my changes into a real fork of the OF code base and issue a pull request all proper like. I’ve done an hour of googling now though without any luck in getting past this linker error. Any suggestions?


If it helps, my updated versions of ofPolyline.h and ofPolyline.cpp look like this:

in the cpp, change them both from:

static bool inside  


bool ofPolyline::inside  

Ah, thanks. That did it. Code compiled and works correctly with my example. I submitted a pull request against the develop branch:

Thanks for all your help on this, Kyle.