(GUI/graphic program)
from math import *
from random import *
from tkinter import *
#from transcol import *
#this is the "transcol" module, please put it in the right directory:
def rgb(r,g,b):
hr=list(hex(int(r)))
hg=list(hex(int(g)))
hb=list(hex(int(b)))
hr.remove("0")
hr.remove("x")
hg.remove("0")
hg.remove("x")
hb.remove("0")
hb.remove("x")
if len(hr)<2:
hr.insert(0,"0")
if len(hg)<2:
hg.insert(0,"0")
if len(hb)<2:
hb.insert(0,"0")
return "#"+"".join(hr)+"".join(hg)+"".join(hb)
print("Choose a zoom (recommended: 20):")
zoom=int(input())
print("Choose the amount of flattening (recommended: 10):")
flat=int(input())
print("please input your function:")
print("f(x,y)=")
ineq=input()
spell=list(ineq)
for l in range(len(spell)):
if spell[l]=="^":
spell[l]="**"
outeq="".join(spell)
print("Type 'y' and press Enter if you want to see the gradient, otherwise just press Enter:")
gradiente=input()
wx=800
wy=800
w=Tk()
a=Canvas(w,width=wx,height=wy,bg=rgb(0,0,0))
a.pack()
h=1/1000
frequency=1
intensity=1
derivatax=False
derivatay=False
res=1
nts=0.1
def grid():
for xa in range(0,400,zoom):
a.create_line(400-xa,0,400-xa,800,fill=rgb(64,64,64))
a.create_line(400+xa,0,400+xa,800,fill=rgb(64,64,64))
for ya in range(0,400,zoom):
a.create_line(0,400-ya,800,400-ya,fill=rgb(64,64,64))
a.create_line(0,400+ya,800,400+ya,fill=rgb(64,64,64))
a.create_line(400,0,400,800,fill=rgb(128,128,128))
a.create_line(0,400,800,400,fill=rgb(128,128,128))
a.update()
grid()
exec("def f(x,y):\n return "+outeq)
def dxf(x,y):
return (f(x+h,y)-f(x,y))/h
def dyf(x,y):
return (f(x,y+h)-f(x,y))/h
print("")
print("Drawing function. Please wait...")
x=-400/zoom
while x<400/zoom:
y=-400/zoom
while y<400/zoom:
if f(x,y)*zoom/flat>-128 and f(x,y)*zoom/flat<128:
a.create_oval(400+x*zoom,400-y*zoom,400+x*zoom,400-y*zoom,outline=rgb(128+f(x,y)*zoom/flat,128+f(x,y)*zoom/flat,128-f(x,y)*zoom/flat),fill=rgb(128+f(x,y)*zoom/flat,128+f(x,y)*zoom/flat,128-f(x,y)*zoom/flat))
y=y+(1/zoom)
x=x+(1/zoom)
a.update()
grid()
print("Function done!")
print("Yellow represents positive values;")
print("Grey represents null values;")
print("Blue represents negative values.")
print("Black areas are where the function is either undefined or out-of-scale;")
print("Tip: if there is too much black area, try increasing the flattening.")
print("")
if derivatax:
x=-400/zoom
while x<400/zoom:
y=-400/zoom
while y<400/zoom:
if dxf(x,y)*zoom/flat>-128 and dxf(x,y)*zoom/flat<128:
a.create_oval(400+x*zoom,400-y*zoom,400+x*zoom,400-y*zoom,outline="",fill=rgb(128+dxf(x,y)*zoom/flat,128+dxf(x,y)*zoom/flat,128-dxf(x,y)*zoom/flat))
y=y+(1/zoom)
x=x+(1/zoom)
a.update()
grid()
if derivatay:
x=-400/zoom
while x<400/zoom:
y=-400/zoom
while y<400/zoom:
if dyf(x,y)*zoom/flat>-128 and dyf(x,y)*zoom/flat<128:
a.create_oval(400+x*zoom,400-y*zoom,400+x*zoom,400-y*zoom,outline="",fill=rgb(128+dyf(x,y)*zoom/flat,128+dyf(x,y)*zoom/flat,128-dyf(x,y)*zoom/flat))
y=y+(1/zoom)
x=x+(1/zoom)
a.update()
grid()
if gradiente=="y":
print("Drawing gradient. Please wait...")
x=-400
while x<400:
y=-400
while y<400:
a.create_line(400+x*zoom,400-y*zoom,400+x*zoom+dxf(x,y)*zoom*nts,400-y*zoom-dyf(x,y)*zoom*nts,fill=rgb(255,255,255))
a.create_oval(400+x*zoom+dxf(x,y)*zoom*nts-2,400-y*zoom-dyf(x,y)*zoom*nts-2,400+x*zoom+dxf(x,y)*zoom*nts+2,400-y*zoom-dyf(x,y)*zoom*nts+2,outline="",fill=rgb(255,255,255))
y=y+(1/res)
x=x+(1/res)
print("Gradient done!")
a.mainloop()