Skip to content

Instantly share code, notes, and snippets.

@zafarali
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save zafarali/a232c907e7c3533cf11e to your computer and use it in GitHub Desktop.

Select an option

Save zafarali/a232c907e7c3533cf11e to your computer and use it in GitHub Desktop.
draw a polygon using vertices
import matplotlib.pyplot as plt
def plot_polygon(vertices):
plt.figure()
for i in range(len(vertices)-1):
vertex_set = [ vertices[i] ] + [ vertices[i+1] ]
x, y = zip(*vertex_set)
plt.plot(x,y)
vertex_set = [ vertices[0] ] + [ vertices[-1] ]
x, y = zip(*vertex_set)
plt.plot(x,y)
plt.xlim([min(vertices, key=lambda v: v[0])[0]-2, max(vertices, key=lambda v: v[0])[0]+2])
plt.ylim([min(vertices, key=lambda v: v[1])[1]-2, max(vertices, key=lambda v: v[1])[1]+2])
plt.show()
if __name__ == '__main__':
vertices = [ (0,0), (0,1), (1,1), (1,0) ]
plot_polygon(vertices)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment