Created
March 6, 2019 02:34
-
-
Save constructor-s/841e50c33acbe7edf3ddde5ec5b2de6b to your computer and use it in GitHub Desktop.
Calculate area in polygon
This file contains 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
#!/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))) |
-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
Picking points on Matplotlib: https://stackoverflow.com/a/41825214/6610243