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"): 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:
self.goto(x, function(x)) if init:
except ZeroDivisionError: self.go(x, function(x))
self.go(x, function(x + 1 * medium)) init = False
else:
self.goto(x, function(x))
except Exception:
continue
def clean(self): def clean(self):
"""Clean the canvas redraw axes""" """Clean the canvas redraw axes"""
self.reset() self.reset()

View File

@ -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)
@ -29,20 +29,20 @@ 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,8 +86,8 @@ 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,
sticky="nw", sticky="nw",
@ -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,11 +219,11 @@ 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()
self.frame.destroy() self.frame.destroy()