#! /usr/bin/env python #------------------------------------------------------------------------------ # Equazione della retta - By C.Fin 2013 - Python2&3 #------------------------------------------------------------------------------ try: import Tkinter as tk except ImportError: import tkinter as tk #------------------------------------------------------------------------------ MINX, MINY = -15.0, -15.0 # coord. virtuali angolo basso sinistra MAXX, MAXY = 15.0, 15.0 # coord. virtuali angolo alto destra WID, HEI = 500, 500 # dimensioni in pixel area grafica KX1 = (WID-1) / (MAXX-MINX) # coefficienti per traslazione KX2 = -MINX * KX1 KY1 = (1.0-HEI) / (MAXY-MINY) KY2 = -MINY*KY1 + HEI-1 CFONDO, CASSI, CRETTA, CGRIGL = "#252500", "#ca5500", "green", "#2a2a25" #------------------------------------------------------------------------------ def trasla(x, y): return round(x*KX1 + KX2), round(y*KY1 + KY2) #------------------------------------------------------------------------------ def disegna_quadro(): for n in range(-14, 15): # disegna griglia if n != 0 and n % 2 == 0: x1, y1 = trasla(n, MINY) x2, y2 = trasla(n, MAXY) tela.create_line(x1, y1, x2, y2, fill=CGRIGL) x1, y1 = trasla(MINX, n) x2, y2 = trasla(MAXX, n) tela.create_line(x1, y1, x2, y2, fill=CGRIGL) for n in range(-14, 15): # disegna scale x1, y1 = trasla(n, -0.3) x2, y2 = trasla(n, 0.2) tela.create_line(x1, y1, x2, y2, fill=CASSI) # Scala X if n != 0 and n % 2 == 0: tela.create_text(x1, y1+2, fill=CASSI, anchor="n", text=str(n)) x1, y1 = trasla(-0.2, n) x2, y2 = trasla(0.3, n) tela.create_line(x1, y1, x2, y2, fill=CASSI) # Scala Y if n != 0 and n % 2 == 0: tela.create_text(x1-2, y1, fill=CASSI, anchor="e", text=str(n)) x1, y1 = trasla(MINX, 0) x2, y2 = trasla(MAXX, 0) tela.create_line(x1, y1, x2, y2, fill=CASSI, arrow="last") # Asse X x1, y1 = trasla(0, MINY) x2, y2 = trasla(0, MAXY) tela.create_line(x1, y1, x2, y2, fill=CASSI, arrow="last") # Asse Y #------------------------------------------------------------------------------ def aggiorna(_): a, b = slider_a.get(), slider_b.get() x1, y1 = trasla(MINX, a*MINX + b) x2, y2 = trasla(MAXX, a*MAXX + b) tela.coords(retta, x1, y1, x2, y2) s = "Y = %-2.2f * X %s %2.1f" % (a, "+" if b>=0 else "-", abs(b)) tela.itemconfigure(testo, text=s) #------------------------------------------------------------------------------ finestra = tk.Tk() finestra.resizable(False, False) finestra.title("La retta via") tela = tk.Canvas(finestra, highlightthickness=0) tela.configure(bg=CFONDO, width=WID, height=HEI) tela.pack(side="left") cornice_a = tk.Frame(finestra) cornice_a.pack(side="left", fill="y") tk.Label(cornice_a, text="a").pack(side="top") slider_a = tk.Scale(cornice_a, from_=20.0, to=-20.0, resolution=0.01, showvalue=0, command=aggiorna) slider_a.pack(side="top", expand=1, fill="y") cornice_b = tk.Frame(finestra) cornice_b.pack(side="left", fill="y") tk.Label(cornice_b, text="b").pack(side="top") slider_b = tk.Scale(cornice_b, from_=20.0, to=-20.0, resolution=0.1 , showvalue=0, command=aggiorna) slider_b.pack(side="top", expand=1, fill="y") disegna_quadro() retta = tela.create_line(0, 0, 0, 0, fill=CRETTA) testo = tela.create_text(30, 30, fill=CRETTA, anchor="nw", text="") slider_a.set(-4.0) slider_b.set(13.0) finestra.mainloop()