Skip math domain error
This commit is contained in:
parent
1c4cad6754
commit
6505b34d67
14
graph.py
14
graph.py
@ -69,18 +69,18 @@ class Graph(turtle.Pen, Tools):
|
|||||||
def plot(self, function, start, stop, color="blue"):
|
def plot(self, function, start, stop, color="blue"):
|
||||||
"""Plot a function"""
|
"""Plot a function"""
|
||||||
medium = 2 / self.X
|
medium = 2 / self.X
|
||||||
|
init = True
|
||||||
self.color(color)
|
self.color(color)
|
||||||
try:
|
|
||||||
self.go(start, function(start))
|
|
||||||
except ZeroDivisionError:
|
|
||||||
self.go(start, function(start + 1 * medium))
|
|
||||||
for x in range(int(start / medium), int(stop / medium) + 1):
|
for x in range(int(start / medium), int(stop / medium) + 1):
|
||||||
x *= medium
|
x *= medium
|
||||||
try:
|
try:
|
||||||
|
if init:
|
||||||
|
self.go(x, function(x))
|
||||||
|
init = False
|
||||||
|
else:
|
||||||
self.goto(x, function(x))
|
self.goto(x, function(x))
|
||||||
except ZeroDivisionError:
|
except Exception:
|
||||||
self.go(x, function(x + 1 * medium))
|
continue
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
"""Clean the canvas redraw axes"""
|
"""Clean the canvas redraw axes"""
|
||||||
self.reset()
|
self.reset()
|
||||||
|
34
plotter.py
34
plotter.py
@ -1,8 +1,8 @@
|
|||||||
import tkinter
|
import tkinter
|
||||||
import tkinter.filedialog
|
import tkinter.filedialog
|
||||||
import re
|
import re
|
||||||
#import canvas2svg
|
|
||||||
from math import *
|
from math import *
|
||||||
|
import canvasvg
|
||||||
import graph
|
import graph
|
||||||
|
|
||||||
|
|
||||||
@ -11,7 +11,7 @@ class Application(tkinter.Frame):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
tkinter.Frame.__init__(self, window)
|
tkinter.Frame.__init__(self, window)
|
||||||
|
|
||||||
#Window settings
|
# Window settings
|
||||||
|
|
||||||
window.title("Settings")
|
window.title("Settings")
|
||||||
window.resizable(0, 0)
|
window.resizable(0, 0)
|
||||||
@ -30,19 +30,19 @@ class Application(tkinter.Frame):
|
|||||||
def widgets(self):
|
def widgets(self):
|
||||||
"""Draws widgets"""
|
"""Draws widgets"""
|
||||||
|
|
||||||
#Labels
|
# Labels
|
||||||
|
|
||||||
self.label1 = tkinter.Label(window, textvariable=self.text)
|
self.label1 = tkinter.Label(window, textvariable=self.text)
|
||||||
self.label2 = tkinter.Label(window, text="Interval:")
|
self.label2 = tkinter.Label(window, text="Interval:")
|
||||||
self.label3 = tkinter.Label(window, text="Color:")
|
self.label3 = tkinter.Label(window, text="Color:")
|
||||||
|
|
||||||
#Textboxes
|
# Textboxes
|
||||||
|
|
||||||
self.textbox1 = tkinter.Entry(window)
|
self.textbox1 = tkinter.Entry(window)
|
||||||
self.textbox2 = tkinter.Entry(window)
|
self.textbox2 = tkinter.Entry(window)
|
||||||
self.textbox3 = tkinter.Entry(window)
|
self.textbox3 = tkinter.Entry(window)
|
||||||
|
|
||||||
#Options
|
# Options
|
||||||
|
|
||||||
self.choose1 = tkinter.Radiobutton(
|
self.choose1 = tkinter.Radiobutton(
|
||||||
window,
|
window,
|
||||||
@ -52,7 +52,7 @@ class Application(tkinter.Frame):
|
|||||||
command=self.label)
|
command=self.label)
|
||||||
self.choose2 = tkinter.Radiobutton(
|
self.choose2 = tkinter.Radiobutton(
|
||||||
window,
|
window,
|
||||||
value="punti",
|
value="points",
|
||||||
text="Points",
|
text="Points",
|
||||||
variable=self.type,
|
variable=self.type,
|
||||||
command=self.label)
|
command=self.label)
|
||||||
@ -63,7 +63,7 @@ class Application(tkinter.Frame):
|
|||||||
variable=self.type,
|
variable=self.type,
|
||||||
command=self.label)
|
command=self.label)
|
||||||
|
|
||||||
#Pulsanti
|
# Pulsanti
|
||||||
|
|
||||||
self.button1 = tkinter.Button(
|
self.button1 = tkinter.Button(
|
||||||
window,
|
window,
|
||||||
@ -86,7 +86,7 @@ class Application(tkinter.Frame):
|
|||||||
text="Exit",
|
text="Exit",
|
||||||
command=self.exit)
|
command=self.exit)
|
||||||
|
|
||||||
#Widgets layout
|
# Widgets layout
|
||||||
|
|
||||||
self.label1.grid(
|
self.label1.grid(
|
||||||
column=0, row=0,
|
column=0, row=0,
|
||||||
@ -157,7 +157,7 @@ class Application(tkinter.Frame):
|
|||||||
def plot(self):
|
def plot(self):
|
||||||
"""Plot the data"""
|
"""Plot the data"""
|
||||||
|
|
||||||
#Converte la function in modo che possa essere interpretata da eval().
|
# Prapare function for evaluation.
|
||||||
function = re.sub(
|
function = re.sub(
|
||||||
"([\+-\-]?\d+)(x)",
|
"([\+-\-]?\d+)(x)",
|
||||||
"\\1*x",
|
"\\1*x",
|
||||||
@ -171,7 +171,7 @@ class Application(tkinter.Frame):
|
|||||||
"factorial(\\1)",
|
"factorial(\\1)",
|
||||||
function)
|
function)
|
||||||
|
|
||||||
#Legge l'interval inserito.
|
# set interval
|
||||||
try:
|
try:
|
||||||
interval = eval(self.textbox2.get())
|
interval = eval(self.textbox2.get())
|
||||||
except SyntaxError:
|
except SyntaxError:
|
||||||
@ -181,12 +181,12 @@ class Application(tkinter.Frame):
|
|||||||
start = float(interval[0])
|
start = float(interval[0])
|
||||||
stop = float(interval[1])
|
stop = float(interval[1])
|
||||||
|
|
||||||
#Legge il color inserito.
|
# set color
|
||||||
color = self.textbox3.get()
|
color = self.textbox3.get()
|
||||||
if color == "":
|
if color == "":
|
||||||
color = "orange"
|
color = "orange"
|
||||||
|
|
||||||
#Disegna il graph in base al type inserito.
|
# draw graph based on choosed type
|
||||||
if self.type.get() == "normal":
|
if self.type.get() == "normal":
|
||||||
function = eval("lambda x:" + function)
|
function = eval("lambda x:" + function)
|
||||||
self.graph.plot(function, start, stop, color)
|
self.graph.plot(function, start, stop, color)
|
||||||
@ -200,11 +200,11 @@ class Application(tkinter.Frame):
|
|||||||
function[1][1],
|
function[1][1],
|
||||||
color)
|
color)
|
||||||
else:
|
else:
|
||||||
function = graph.punti(eval(self.textbox1.get()))
|
function = graph.points(eval(self.textbox1.get()))
|
||||||
self.graph.plot(
|
self.graph.plot(
|
||||||
function,
|
function,
|
||||||
punti[0][0],
|
points[0][0],
|
||||||
punti[len(punti) - 1][1],
|
points[len(points) - 1][1],
|
||||||
color)
|
color)
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
@ -219,10 +219,10 @@ class Application(tkinter.Frame):
|
|||||||
"parent": self.frame,
|
"parent": self.frame,
|
||||||
"defaultextension": ".svg",
|
"defaultextension": ".svg",
|
||||||
"initialfile": "graph.svg",
|
"initialfile": "graph.svg",
|
||||||
"title": "Salva il graph"
|
"title": "Save graph"
|
||||||
}
|
}
|
||||||
file = tkinter.filedialog.asksaveasfilename(**settings)
|
file = tkinter.filedialog.asksaveasfilename(**settings)
|
||||||
canvas2svg.saveall(file, self.canvas)
|
canvasvg.saveall(file, self.canvas)
|
||||||
|
|
||||||
def exit(self):
|
def exit(self):
|
||||||
window.destroy()
|
window.destroy()
|
||||||
|
Loading…
Reference in New Issue
Block a user