Skip to content

Instantly share code, notes, and snippets.

@constructor-s
Created March 6, 2019 02:34
Show Gist options
  • Save constructor-s/841e50c33acbe7edf3ddde5ec5b2de6b to your computer and use it in GitHub Desktop.
Save constructor-s/841e50c33acbe7edf3ddde5ec5b2de6b to your computer and use it in GitHub Desktop.
Calculate area in polygon
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 5 20:17:08 2019
@author: bill
"""
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
matplotlib.use('Qt5Agg')
fig, ax = plt.subplots()
plt.imshow(plt.imread('curve.png'))
points = []
def onclick(event):
print('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
(event.button, event.x, event.y, event.xdata, event.ydata))
plt.plot(event.xdata, event.ydata, 'x', markersize=50)
points.append((event.xdata, event.ydata))
fig.canvas.draw()
cid = fig.canvas.mpl_connect('button_press_event', onclick)
def polyArea(x,y):
"""
https://stackoverflow.com/a/30408825/6610243
"""
return 0.5*np.abs(np.dot(x,np.roll(y,1))-np.dot(y,np.roll(x,1)))
print(polyArea(*zip(*points)))
@constructor-s
Copy link
Author

Picking points on Matplotlib: https://stackoverflow.com/a/41825214/6610243

@HiImKarl
Copy link

HiImKarl commented Mar 6, 2019

-1 for non-pythonic use of global scoped variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment