News for hackers. Stuff that matters.

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

Help.

Is there any way of finding a polygon that would contain all of a given list of points? without any intersections.

I'm hoping to use it in an app built on Google's Maps API v3. Any help/links would be much appreciated.

( 9 comments | 4 points | on HN )
Re:Ask HN: Finding a polygon given a list of random coords? by JoachimSchipper on Saturday August 06, 2011 @02:21PM (#2854514)
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
Re:Ask HN: Finding a polygon given a list of random coords? by iambot on Saturday August 06, 2011 @05:58PM (#2855028)
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.
Re:Ask HN: Finding a polygon given a list of random coords? by redxaxder on Saturday August 06, 2011 @02:22PM (#2854517)
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.

Re:Ask HN: Finding a polygon given a list of random coords? by iambot on Saturday August 06, 2011 @06:03PM (#2855038)
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.
Re:Ask HN: Finding a polygon given a list of random coords? by mooism2 on Saturday August 06, 2011 @02:01PM (#2854462)
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...

Re:Ask HN: Finding a polygon given a list of random coords? by iambot on Saturday August 06, 2011 @02:08PM (#2854479)
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?
Re:Ask HN: Finding a polygon given a list of random coords? by mooism2 on Saturday August 06, 2011 @02:53PM (#2854606)
I've no idea, sorry.
Re:Ask HN: Finding a polygon given a list of random coords? by _zeos_ on Saturday August 13, 2011 @12:16AM (#2880222)
Re:Ask HN: Finding a polygon given a list of random coords? by willvarfar on Saturday August 06, 2011 @02:06PM (#2854476)
Sort them by angle from centre-point and join them in that order.