-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_func.py
More file actions
81 lines (68 loc) · 2.15 KB
/
graph_func.py
File metadata and controls
81 lines (68 loc) · 2.15 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
76
77
78
79
80
81
from tkinter import *
import sympy
class Graph():
def __init__(self, w, h, predpis) -> None:
self.c = Canvas(width=w, height=h)
self.width = w
self.height = h
self.predpis = predpis
def definition_to_function(self, s):
lhs, rhs = s.split("=", 1)
rhs = rhs.rstrip('; ')
args = sympy.sympify(lhs).args
f = sympy.sympify(rhs)
def f_func(*passed_args):
argdict = dict(zip(args, passed_args))
result = f.subs(argdict)
return float(result)
return f_func
def build(self):
self.c.pack()
self.ox = self.width/2
self.oy = self.height/2
self.c.create_line((0, self.oy), (self.width, self.oy))
self.c.create_line((self.ox, 0), (self.ox, self.height))
f = self.definition_to_function(self.predpis)
count = 1
l = []
for i in range(int(self.width/2 +1)):
x = i*.1 + self.ox
y = -f(i)*.1 + self.oy
x += count*3
y -= count*3
if len(l) < 2:
l.append((x, y))
else:
self.c.create_line((l[0][0], l[0][1]),( l[1][0], l[1][1]), fill="blue")
l.reverse()
l.pop()
count += 1
count = 1
l = []
for i in range(int(self.width/2 +1)):
x = (-i)*.1 + self.ox
y = -f(-i)*.1 + self.oy
x -= count*3
y += count*3
if len(l) < 2:
l.append((x, y))
else:
self.c.create_line((l[0][0], l[0][1]),( l[1][0], l[1][1]), fill="blue")
l.reverse()
l.pop()
count += 1
self.c.mainloop()
g = Graph(w=1000, h=800, predpis="f(x)=x**3")
g.build()
'''
for i in range(int(-self.width/2), int(self.width/2 + 1)):
x = i*.25 + self.ox
y = -f(i)*.25 + self.oy
print(x, y)
if x < self.width/2:
x -= count*3
else:
x += count*3
self.c.create_oval((x - 1, y - 1), (x + 1, y + 1))
count += 1
'''