Last active
August 29, 2015 14:24
-
-
Save zafarali/a232c907e7c3533cf11e to your computer and use it in GitHub Desktop.
draw a polygon using vertices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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