ETikhonov

NOT a Solar System xD

Jul 6th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf8 -*-
  3. dimension=400 #(0.01 of A.U.)
  4. dt = 0.001
  5. delay=1
  6. import numpy as np
  7. import Tkinter
  8. import time
  9. from matplotlib import pyplot as plt
  10. class Planet:
  11.     def __init__(self,canv,x,y,vx,vy,color):
  12.         self.x = float(x)
  13.         self.y = float(y)
  14.         self.vx = float(vx)
  15.         self.vy = float(vy)
  16.         self.color = color
  17.         self.canv = canv
  18.         self.position=self.canv.create_oval(200+int(100*self.x)-5,200+int(100*self.y)-5,
  19.         200+int(100*self.x)+5,200+int(100*self.y)+5,fill=self.color,outline='white')
  20.     def time_step(self):
  21.         canv.delete(self.position)
  22.         self.canv.create_oval(200+int(100*self.x),200+int(100*self.y),
  23.         200+int(100*self.x),200+int(100*self.y),fill=self.color,outline=self.color)
  24.         self.vx = self.vx - np.sign(self.x)/np.sqrt(self.x**2 + self.y**2) * dt
  25.         self.vy = self.vy - np.sign(self.y)/np.sqrt(self.x**2 + self.y**2) * dt
  26.         self.x = self.x + self.vx * dt
  27.         self.y = self.y + self.vy * dt
  28.         self.position=self.canv.create_oval(200+int(100*self.x)-5,200+int(100*self.y)-5,
  29.         200+int(100*self.x)+5,200+int(100*self.y)+5,fill=self.color,outline='white')       
  30. root = Tkinter.Tk()
  31. canv = Tkinter.Canvas(root, width = dimension, height = dimension, bg = 'black')
  32. canv.pack(expand='yes',fill='both')
  33. mercury = Planet(canv,0.38710,0.,0.,1,"orange")
  34. venus = Planet(canv,0.72334,0.,0.,1,"yellow")
  35. earth = Planet(canv,1.00000,0.,0.,1,"blue")
  36. mars = Planet(canv,1.52371,0.,0.,1,"red")
  37. system=[]
  38. system.append(mercury)
  39. system.append(venus)
  40. system.append(earth)
  41. system.append(mars)
  42. def on_timer():
  43.     for p in system:
  44.         p.time_step()
  45.     root.after(delay, on_timer)
  46. on_timer()
  47. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment