

Ask HN: Finding a polygon given a list of random coords? - iambot

Help.<p>Is there any way of finding a polygon that would contain all of a given list of points? without any intersections.<p>I'm hoping to use it in an app built on Google's Maps API v3. Any help/links would be much appreciated.
======
JoachimSchipper
You may mean "convex hull"; you should be able to find 2D and 3D algorithms
easily (Wikipedia?).

Graphically:

    
    
        X--X  X-X         X--X--X-X
        |  | /  /         |       /
        X  X-  |          X  X   |
        \    --X          \    --X
         X--/              X--/
    
       Possible polygon    Convex hull
       based on the points of the points
       marked X            marked X

~~~
iambot
yes thats exactly what im looking for, thanks for the terminology, it makes it
easier to find what I need if i actually know what I'm looking for.

------
redxaxder
Finding the convex hull of a collection of points is a studied problem, so you
can use an existing solution:

<http://en.wikipedia.org/wiki/Convex_hull_algorithms>

Alternatively, willvarfar's approach is faster for large n, but the polygon
won't be as pretty.

~~~
iambot
thank you, I appreciated you taking the time to answer. As with other comments
on this thead, this is exactly the info I was looking for.

------
mooism2
If there's no restriction other than not wanting the lines to cross, then I'd
find the centre of the polygon by taking the average of the list of points,
then visiting those points in a clockwise direction.

If you need to restrict yourself to following streets on the map, it might be
a bit trickier...

~~~
iambot
streets dont matter, and thanks for the answer, are there any libraries for
doing calulation like this, or would i have to write it manually?

~~~
mooism2
I've no idea, sorry.

~~~
_zeos_
OpenCV

[http://opencv.willowgarage.com/documentation/cpp/structural_...](http://opencv.willowgarage.com/documentation/cpp/structural_analysis_and_shape_descriptors.html)

cv::convexHull

------
willvarfar
Sort them by angle from centre-point and join them in that order.

