Skip math domain error

This commit is contained in:
Rnhmjoj 2014-04-07 21:46:18 +02:00
parent 1c4cad6754
commit 6505b34d67
2 changed files with 28 additions and 28 deletions

View File

@ -69,18 +69,18 @@ class Graph(turtle.Pen, Tools):
def plot(self, function, start, stop, color="blue"):
"""Plot a function"""
medium = 2 / self.X
init = True
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):
x *= medium
try:
self.goto(x, function(x))
except ZeroDivisionError:
self.go(x, function(x + 1 * medium))
if init:
self.go(x, function(x))
init = False
else:
self.goto(x, function(x))
except Exception:
continue
def clean(self):
"""Clean the canvas redraw axes"""
self.reset()

View File

@ -1,8 +1,8 @@
import tkinter
import tkinter.filedialog
import re
#import canvas2svg
from math import *
import canvasvg
import graph
@ -11,7 +11,7 @@ class Application(tkinter.Frame):
def __init__(self):
tkinter.Frame.__init__(self, window)
#Window settings
# Window settings
window.title("Settings")
window.resizable(0, 0)
@ -30,19 +30,19 @@ class Application(tkinter.Frame):
def widgets(self):
"""Draws widgets"""
#Labels
# Labels
self.label1 = tkinter.Label(window, textvariable=self.text)
self.label2 = tkinter.Label(window, text="Interval:")
self.label3 = tkinter.Label(window, text="Color:")
#Textboxes
# Textboxes
self.textbox1 = tkinter.Entry(window)
self.textbox2 = tkinter.Entry(window)
self.textbox3 = tkinter.Entry(window)
#Options
# Options
self.choose1 = tkinter.Radiobutton(
window,
@ -52,7 +52,7 @@ class Application(tkinter.Frame):
command=self.label)
self.choose2 = tkinter.Radiobutton(
window,
value="punti",
value="points",
text="Points",
variable=self.type,
command=self.label)
@ -63,7 +63,7 @@ class Application(tkinter.Frame):
variable=self.type,
command=self.label)
#Pulsanti
# Pulsanti
self.button1 = tkinter.Button(
window,
@ -86,7 +86,7 @@ class Application(tkinter.Frame):
text="Exit",
command=self.exit)
#Widgets layout
# Widgets layout
self.label1.grid(
column=0, row=0,
@ -157,7 +157,7 @@ class Application(tkinter.Frame):
def plot(self):
"""Plot the data"""
#Converte la function in modo che possa essere interpretata da eval().
# Prapare function for evaluation.
function = re.sub(
"([\+-\-]?\d+)(x)",
"\\1*x",
@ -171,7 +171,7 @@ class Application(tkinter.Frame):
"factorial(\\1)",
function)
#Legge l'interval inserito.
# set interval
try:
interval = eval(self.textbox2.get())
except SyntaxError:
@ -181,12 +181,12 @@ class Application(tkinter.Frame):
start = float(interval[0])
stop = float(interval[1])
#Legge il color inserito.
# set color
color = self.textbox3.get()
if color == "":
color = "orange"
#Disegna il graph in base al type inserito.
# draw graph based on choosed type
if self.type.get() == "normal":
function = eval("lambda x:" + function)
self.graph.plot(function, start, stop, color)
@ -200,11 +200,11 @@ class Application(tkinter.Frame):
function[1][1],
color)
else:
function = graph.punti(eval(self.textbox1.get()))
function = graph.points(eval(self.textbox1.get()))
self.graph.plot(
function,
punti[0][0],
punti[len(punti) - 1][1],
points[0][0],
points[len(points) - 1][1],
color)
def clear(self):
@ -219,10 +219,10 @@ class Application(tkinter.Frame):
"parent": self.frame,
"defaultextension": ".svg",
"initialfile": "graph.svg",
"title": "Salva il graph"
"title": "Save graph"
}
file = tkinter.filedialog.asksaveasfilename(**settings)
canvas2svg.saveall(file, self.canvas)
canvasvg.saveall(file, self.canvas)
def exit(self):
window.destroy()