python - How to draw the largest polygon from a set of points -


so, have set of points (x,y), , want able draw largest polygon these points vertices. can use patches.polygon() in matplotlib, draws lines between points in order give them. doesn't automatically want. example, if want draw square, , sort points increasing x, , increasing y, won't square, 2 connecting triangles. (the line "crosses over")

so problem find way sort list of points such "go around outside" of polygon when iterating on list.

or there maybe other functionality in matplotlib can me?

as suggested ready simple solution calculate angles inner point points , sort them.

so here numpy function calculate ccworder:

in []: def ccworder(a):    ..:     a= a- mean(a, 1)[:, none]    ..:     return argsort(arctan2(a[1, :], a[0, :]))    ..: 

and simple demonstration:

in []: out[]: array([[0, 0, 1, 1],        [0, 1, 1, 0]]) in []: ccworder(a) out[]: array([0, 3, 2, 1]) 

update:
may seem kind ordering somehow tedious calculate, numpycan provide nice abstraction make them pretty straightforward.

caveat: joe , others have pointed out, ccworder form proper order on convex hull if points ready on convex hull. i.e. somehow order missing, seems op's case, can recovered. of'course there other situations ccworder use full.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -