import sys
pth='/usr/local/pyglet-1.1.4'
if not pth in sys.path: sys.path.append(pth)
from pyglet import *
pth='/usr/local/pyprocessing-0.1.2.3'
if not pth in sys.path: sys.path.append(pth)
from pyprocessing import *
p=[] #planets
def AddPlanet(x,y,possibility):
return (p.append([x,y,0,0]) if random()>possibility else false)
def setup():
size(640,480)
[[[AddPlanet(x*50,y*50,0.9) for x in range(10)] for y in range(10)]]
Tx=0; Ty=0; Gx=""; scrollsz=20
def draw():
global Tx,Ty,p
background(0)
####GUI:
fill(128); rect(10,10,100,20); fill(255); text("reset",32,27)
if mouse.pressed and mouse.x<110 and mouse.y<30:
p=[]; [[[AddPlanet(x*50,y*50,0.9) for x in range(10)] for y in range(10)]]; Tx=0; Ty=0
####simulation and drawing:
fill(255,255,158)
for i in range(len(p)):
for j in range(len(p)):
if i!=j:
absx=p[i][0]-p[j][0]; absy=p[i][1]-p[j][1]
x=max(50,math.sqrt(absx^2+absy^2))
angle=math.atan2(absy,absx)
p[i][2]+=math.cos(angle)*eval(Gx)
p[i][3]+=math.sin(angle)*eval(Gx)
p[i][0]+=p[i][2]; p[i][1]+=p[i][3]
ellipse(p[i][0]+Tx,p[i][1]+Ty,10,10)
####Scrolling:
if mouse.x<scrollsz: Tx+=10
if mouse.y<scrollsz: Ty+=10
if mouse.x>width-scrollsz: Tx-=10
if mouse.y>height-scrollsz: Ty-=10
@interact
def main(sel=list(["plot function","planet simulation"]),Fx="-1000/(x**2)"):
global Gx; Gx=Fx
if sel=="plot function":
show(plot(eval(Gx),(x,0.1,10)))
if sel=="planet simulation":
try: run()
except: pass
|
|
Click to the left again to hide and once more to show the dynamic interactive window
|