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, numpy
can 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
Post a Comment