-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_boundary.py
More file actions
75 lines (61 loc) · 2.05 KB
/
plot_boundary.py
File metadata and controls
75 lines (61 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
from scipy import interpolate
from base import plot2d
class GenBoundary (plot2d):
def __init__(self, num=5, lxy=10):
plot2d.__init__(self)
self.xx = []
self.yy = []
self.kk = 3
self.pn = num
self.axs.plot([-lxy, lxy, lxy, -lxy, -lxy],
[-lxy, -lxy, lxy, lxy, -lxy])
self.spl, = self.axs.plot(self.xx, self.yy)
self.pts, = self.axs.plot(self.xx, self.yy, 'o')
self.fig.canvas.mpl_connect('button_press_event', self._OnClick)
self.fig.canvas.mpl_connect('key_press_event', self._KeyClick)
def make_button(self, name="Next", pos=[0.7, 0.05, 0.1, 0.075]):
pos_axs = self.fig.add_axes(pos)
axs_bot = Button(pos_axs, name)
return axs_bot
def _KeyClick(self, event):
if event.key == 'p':
self._Spline(event)
elif event.key == 'c':
self._Clear(event)
def _Clear(self, event):
self.xx = []
self.yy = []
self.pn = 2
self.spl.set_xdata(self.xx)
self.spl.set_ydata(self.yy)
self.pts.set_xdata(self.xx)
self.pts.set_ydata(self.yy)
plt.draw()
def _Spline(self, event):
num = len(self.xx)
tck, u = interpolate.splprep(
[self.xx + [self.xx[0]], self.yy + [self.yy[0]]], k=self.kk, s=0
)
pu = np.linspace(0, 1, num=self.pn * num)
spline = interpolate.splev(pu, tck)
self.spl.set_xdata(spline[0])
self.spl.set_ydata(spline[1])
plt.draw()
def _OnClick(self, event):
if event.dblclick:
self._Clear()
elif event.button == 1 and event.xdata is not None:
print(event)
self.xx.append(event.xdata)
self.yy.append(event.ydata)
self.pts.set_xdata(self.xx)
self.pts.set_ydata(self.yy)
plt.draw()
if __name__ == '__main__':
obj = GenBoundary()
obj.Show()
print(*obj.xx)
print(*obj.yy)