I am trying to do a simple plot interface that allows me to click to add points to a list and then with another key or another click, call the triangulation of those points.
Matplotlib offers a small example for adding points to a line but I dont know how to make it so that I simply add points to a list and then call a function to triangulate
from matplotlib import pyplot as plt
class LineBuilder:
def __init__(self, line):
self.line = line
self.xs = list(line.get_xdata())
self.ys = list(line.get_ydata())
self.cid = line.figure.canvas.mpl_connect('button_press_event', self)
def __call__(self, event):
print('click', event)
if event.inaxes!=self.line.axes: return
self.xs.append(event.xdata)
self.ys.append(event.ydata)
self.line.set_data(self.xs, self.ys)
self.line.figure.canvas.draw()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click to build line segments')
line, = ax.plot([0], [0]) # empty line
linebuilder = LineBuilder(line)
plt.show()
I use scikit delunay triangulation
import numpy as np
from scipy.spatial import Delaunay
points=np.array([[134,30],[215,114],[160,212],[56,181],[41,78]])
tri = Delaunay(points)
plt.triplot(points[:,0], points[:,1], tri.simplices.copy())
plt.plot(points[:,0], points[:,1], 'o')
plt.show()
Thank you
