Last active
February 4, 2017 05:07
-
-
Save ejherran/4ace2cff4e76a18309bf6dac8dd368b3 to your computer and use it in GitHub Desktop.
Simulación del valor de PI por medio del método montecarlo - Con TK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| # | |
| # Simulación del valor de PI por el método montecarlo | |
| # Javier Herran ([email protected]) | |
| # UPC - 2017 | |
| # | |
| # | |
| import random | |
| from tkinter import Tk, Frame, Canvas, BOTH, W, Text, END, NORMAL, DISABLED | |
| class PI(Frame): | |
| lv0 = 10000 | |
| lv1 = 1000 | |
| lv2 = 100 | |
| lado = 400 | |
| radio = 200 | |
| ox = 300 | |
| oy = 220 | |
| ct0 = 0 | |
| de0 = 0 | |
| pi0 = 0 | |
| ac1 = 0 | |
| ct1 = 0 | |
| pi1 = 0 | |
| ac2 = 0 | |
| ct2 = 0 | |
| pi2 = 0 | |
| isActive = False | |
| area = None | |
| datos = None | |
| def __init__(self, parent): | |
| Frame.__init__(self, parent, background="white") | |
| self.parent = parent | |
| self.parent.title("PI MONTECARLO") | |
| self.parent.bind("<Return>", self.iniciar) | |
| self.pack(fill=BOTH, expand=1) | |
| self.centrarVentana() | |
| self.area = Canvas(self) | |
| self.area.create_rectangle(0, 0, 600, 440, fill="#000000") | |
| self.area.create_oval(100, 20, 500, 420, outline="#0000ff", width=2) | |
| self.area.create_rectangle(100, 20, 500, 420, outline="#ff0000", width=2) | |
| self.area.pack(fill=BOTH, expand=1) | |
| self.datos = Text(parent, height=10, width=85) | |
| self.datos.config(state=NORMAL) | |
| self.datos.insert(END, "\n\n\n\t\t\tPULSE <ENTER> PARA INICIAR LA SIMULACIÓN") | |
| self.datos.config(state=DISABLED) | |
| self.datos.pack() | |
| def iniciar(self, e): | |
| if(not self.isActive): | |
| self.isActive = True | |
| self.after(1, self.crearPunto) | |
| def crearPunto(self): | |
| px = self.ox + ( (self.lado * random.random() ) - self.radio ) | |
| py = self.oy + ( (self.lado * random.random() ) - self.radio ) | |
| distancia = ( ((px-self.ox) ** 2) + ((py-self.oy) ** 2) ) ** 0.5 | |
| px = int(px) | |
| py = int(py) | |
| if(distancia <= self.radio): | |
| self.area.create_rectangle(px, py, px+1, py+1, fill='#00ff00', width=0) | |
| self.de0 = self.de0+1 | |
| self.pi0 = 4 * (self.de0 / self.lv0) | |
| else: | |
| self.area.create_rectangle(px, py, px+1, py+1, fill='#9E289E', width=0) | |
| self.ct0 = self.ct0+1 | |
| self.datos.config(state=NORMAL) | |
| self.datos.delete(1.0,END) | |
| self.datos.insert(END, "============================== NIVEL 0 ==============================\n") | |
| self.datos.insert(END, "INTENTOS: "+str(self.ct0)+"\tDENTRO: "+str(self.de0)+"\tPI: "+str(self.pi0)+"\n\n") | |
| self.datos.insert(END, "============================== NIVEL 1 ==============================\n") | |
| self.datos.insert(END, "INTENTOS: "+str(self.ct1)+"\tPI: "+str(self.pi1)+"\n\n") | |
| self.datos.insert(END, "============================== NIVEL 2 ==============================\n") | |
| self.datos.insert(END, "INTENTOS: "+str(self.ct2)+"\tPI: "+str(self.pi2)+"\n\n") | |
| self.datos.config(state=DISABLED) | |
| if(self.ct0 < self.lv0): | |
| self.after(1, self.crearPunto) | |
| else: | |
| self.nivel1() | |
| def nivel1(self): | |
| self.ct1 = self.ct1 + 1 | |
| self.ac1 = self.ac1+self.pi0 | |
| self.pi1 = self.ac1 / self.ct1 | |
| if(self.ct1 < self.lv1): | |
| self.area.create_rectangle(0, 0, 600, 440, fill="#000000") | |
| self.area.create_oval(100, 20, 500, 420, outline="#0000ff", width=2) | |
| self.area.create_rectangle(100, 20, 500, 420, outline="#ff0000", width=2) | |
| self.ct0 = 0 | |
| self.de0 = 0 | |
| self.pi0 = 0 | |
| self.after(1, self.crearPunto) | |
| else: | |
| self.nivel2() | |
| def nivel2(self): | |
| self.ct2 = self.ct2 + 1 | |
| self.ac2 = self.ac2+self.pi1 | |
| self.pi2 = self.ac2 / self.ct2 | |
| if(self.ct2 < self.lv2): | |
| self.area.create_rectangle(0, 0, 600, 440, fill="#000000") | |
| self.area.create_oval(100, 20, 500, 420, outline="#0000ff", width=2) | |
| self.area.create_rectangle(100, 20, 500, 420, outline="#ff0000", width=2) | |
| self.ct0 = 0 | |
| self.de0 = 0 | |
| self.pi0 = 0 | |
| self.ac1 = 0 | |
| self.ct1 = 0 | |
| self.pi1 = 0 | |
| self.after(1, self.crearPunto) | |
| else: | |
| self.datos.config(state=NORMAL) | |
| self.datos.delete(1.0,END) | |
| self.datos.insert(END, "\n\n\n\t\t\t\tPI = "+str(self.pi2)) | |
| self.datos.config(state=DISABLED) | |
| def centrarVentana(self): | |
| w = 600 | |
| h = 600 | |
| sw = self.parent.winfo_screenwidth() | |
| sh = self.parent.winfo_screenheight() | |
| x = (sw - w)/2 | |
| y = (sh - h)/2 | |
| self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y)) | |
| def main(): | |
| root = Tk() | |
| ex = PI(root) | |
| root.resizable(width=False, height=False) | |
| root.mainloop() | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment