From 6505b34d6758d172383b17468a97e592ed3e8939 Mon Sep 17 00:00:00 2001 From: Rnhmjoj Date: Mon, 7 Apr 2014 21:46:18 +0200 Subject: [PATCH] Skip math domain error --- graph.py | 16 ++++++++-------- plotter.py | 40 ++++++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/graph.py b/graph.py index 13ebded..dba7605 100644 --- a/graph.py +++ b/graph.py @@ -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() diff --git a/plotter.py b/plotter.py index bba286a..6bd0c6d 100644 --- a/plotter.py +++ b/plotter.py @@ -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) @@ -29,20 +29,20 @@ 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,8 +86,8 @@ class Application(tkinter.Frame): text="Exit", command=self.exit) - #Widgets layout - + # Widgets layout + self.label1.grid( column=0, row=0, sticky="nw", @@ -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,11 +219,11 @@ 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() self.frame.destroy()