-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlotInput.py
84 lines (77 loc) · 2.47 KB
/
PlotInput.py
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
76
77
78
79
80
81
82
import tkinter
from DFT import Function
from DFT import fourier
import matplotlib.pyplot as plt
# Tkinter Window
root = tkinter.Tk()
root.title("Plot Input")
width, height = 2**10, 648
#root.geometry(f"{width}x{height}")
# Canvas to draw a Function into
canvas = tkinter.Canvas(root, width=width, height=height)
canvas.pack()
analyzeButton = tkinter.Button(root, text="Analyze", command=lambda: fourier(f))
analyzeButton.pack(side="left")
resetButton = tkinter.Button(root, text="Reset", command=lambda: reset())
resetButton.pack(side="left")
pixelSizeInput = tkinter.Entry(root, width=10)
pixelSizeInput.pack(side="right")
pixelSizeInput.insert(0, "8")
pixelSizeInput.bind("<Return>", lambda event: setPixelSize())
pixelSizeLabel = tkinter.Label(root, text="Pixel Size:")
pixelSizeLabel.pack(side="right")
# Function class
class PlotFunction(Function):
def __init__(self, N, size):
self.size=size
self.points = {}
self.N = N
self.ovals = {}
def reset(self):
self.points = {}
self.ovals = {}
def evaluate(self, x):
if x in self.points:
return self.points[x]
return self.interpolate(x)
def addPoint(self, x, y):
x,y = x//self.size*self.size, y//self.size*self.size
if x in self.ovals:
canvas.delete(self.ovals[x])
self.ovals[x] = canvas.create_rectangle(x, y, x+self.size, y+self.size, fill="black")
self.points[x//self.size] = (height//2-y)//self.size
def interpolate(self, x):
if x < 0 or x > self.N:
return 0
x1 = x2 = x
while x1>0 and x1 not in self.points:
x1 -= 1
while x2<self.N and x2 not in self.points:
x2 += 1
if x1 == 0 or x2 == self.N:
if x1 == 0:
if x2 == self.N:
return 0
return self.points[x2]
return self.points[x1]
y1 = self.points[x1]
y2 = self.points[x2]
return y1 + (y2-y1)/(x2-x1)*(x-x1)
# Draw Function using mouse movement
def draw(event, f):
if event.x < width and event.y < height:
f.addPoint(event.x, event.y)
def getFunction():
canvas.bind("<B1-Motion>", lambda event: draw(event, f))
def reset():
f.reset()
canvas.delete("all")
def setPixelSize():
global f
f = PlotFunction(width//int(pixelSizeInput.get()), int(pixelSizeInput.get()))
reset()
getFunction()
# remove old plt plots
plt.close("all")
setPixelSize()
root.mainloop()