Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf8 -*-
- dimension=400 #(0.01 of A.U.)
- dt = 0.001
- delay=1
- import numpy as np
- import Tkinter
- import time
- from matplotlib import pyplot as plt
- class Planet:
- def __init__(self,canv,x,y,vx,vy,color):
- self.x = float(x)
- self.y = float(y)
- self.vx = float(vx)
- self.vy = float(vy)
- self.color = color
- self.canv = canv
- self.position=self.canv.create_oval(200+int(100*self.x)-5,200+int(100*self.y)-5,
- 200+int(100*self.x)+5,200+int(100*self.y)+5,fill=self.color,outline='white')
- def time_step(self):
- canv.delete(self.position)
- self.canv.create_oval(200+int(100*self.x),200+int(100*self.y),
- 200+int(100*self.x),200+int(100*self.y),fill=self.color,outline=self.color)
- self.vx = self.vx - np.sign(self.x)/np.sqrt(self.x**2 + self.y**2) * dt
- self.vy = self.vy - np.sign(self.y)/np.sqrt(self.x**2 + self.y**2) * dt
- self.x = self.x + self.vx * dt
- self.y = self.y + self.vy * dt
- self.position=self.canv.create_oval(200+int(100*self.x)-5,200+int(100*self.y)-5,
- 200+int(100*self.x)+5,200+int(100*self.y)+5,fill=self.color,outline='white')
- root = Tkinter.Tk()
- canv = Tkinter.Canvas(root, width = dimension, height = dimension, bg = 'black')
- canv.pack(expand='yes',fill='both')
- mercury = Planet(canv,0.38710,0.,0.,1,"orange")
- venus = Planet(canv,0.72334,0.,0.,1,"yellow")
- earth = Planet(canv,1.00000,0.,0.,1,"blue")
- mars = Planet(canv,1.52371,0.,0.,1,"red")
- system=[]
- system.append(mercury)
- system.append(venus)
- system.append(earth)
- system.append(mars)
- def on_timer():
- for p in system:
- p.time_step()
- root.after(delay, on_timer)
- on_timer()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment