def setup(self): """ Built-in method of a Scene object from Pythonista """ self.nodes = [] self.w, self.h = (100, 100) scene_drawing.fill(1, 0, 0) scene_drawing.stroke(1, 1, 1) scene_drawing.stroke_weight(2)
def update(self): gx, gy, gz = get_gravity() m = magnorm() if sum(m): mx, my, mz = m wx, wy, wz = my * gz - mz * gy, mz * gx - mx * gz, mx * gy - my * gx absw = sqrt(sum(map(lambda t: t**2, (wx, wy, wz)))) for i in wx, wy, wz: i /= absw sx, sy, sz = gy * wz - gz * wy, gz * wx - gx * wz, gx * wy - gy * wx alpha = atan2(sy * gx - sx * gy, wx * gy - wy * gx) if alpha < 0: alpha += 2 * pi eta = acos(-gz) rho = atan2(-gx, -gy) if rho < 0: rho += 2 * pi cvert = [[4, 4, 4], [-4, 4, 4], [4, -4, 4], [4, 4, -4]] lne = 1 / sqrt(1 - gz**2) neta = [-gy * lne, gx * lne, 0] try: eta except: eta = alpha = rho = 0 # quats=[[rho,[0,0,1]],[alpha,[-gx,-gy,-gz]],[eta,neta]] physvert = [None for i in range(8)] for j in range(4): i = cvert[j] cx = -i[0] * wx + i[1] * sx - i[2] * gx / 2.8 cy = -i[0] * wy + i[1] * sy - i[2] * gy / 2.8 cz = -i[0] * wz + i[1] * sz - i[2] * gz / 2.8 cvert[j] = [cx, cy, cz] inds = ((0, 1, 3, 4), (6, 7, 5, 2)) for i in range(4): physvert[inds[0][i]] = cvert[i] physvert[inds[1][i]] = list(map(lambda x: -x, cvert[i])) faces = ((2, 3, 6, 7, '#ff6060'), (0, 1, 4, 5, '#a00000'), (0, 3, 4, 7, '#60ff60'), (1, 2, 5, 6, '#00a000'), (0, 1, 3, 2, '#6060ff'), (4, 5, 7, 6, '#0000a0')) faceord = sorted( [0, 1, 2, 3, 4, 5], key=lambda x: sum([physvert[n][2] for n in faces[x][:4]])) visvert = [] D = 4 * 2048 * .5 / (.61 * 19.8) centx, centy = self.size / 2 for i in physvert: z = i[2] rad = D * 4 * sqrt(48 - z**2) / (65 - z) visvert.append((rad * i[0] + centx, rad * i[1] + centy)) for i in faceord: scene_drawing.fill(faces[i][4]) scene_drawing.triangle_strip([visvert[j] for j in faces[i][:4]])
def draw(self): sd.stroke_weight(0.4) sd.fill(255, 255, 0) timenow = time.time() x = linspace(-10, 0, 1000) y = sin((x - timenow)) #renormalize data x, y = self.resize(x, y) self.plot(x, y)
def draw(self): if self.ds: dn, dx = self.ds z = self.zoom if True: for x in range(-dn, dn + 1): for y in range(-dn, dn + 1): if (x, y) in self.content: t = self.content[x, y] scdr.fill(self.types[t][1]) else: scdr.fill(self.bgr[1]) xr = dx + z * (dn + x) yr = dx + z * (dn + y) scdr.rect(xr, yr, z, z) elif self.redraw: x, y = self.redraw if (x, y) in self.content: t = self.content[x, y] scdr.fill(self.types[t][1]) else: scdr.fill(self.bgr[1]) xr = dx + z * (dn + x) yr = dx + z * (dn + y) scdr.rect(xr, yr, z, z)
def update(self): gx,gy,gz=get_gravity() m=magnorm() if sum(m): mx,my,mz=m wx,wy,wz=my*gz-mz*gy,mz*gx-mx*gz,mx*gy-my*gx absw=sqrt(sum(map(lambda t: t**2,(wx,wy,wz)))) for i in wx,wy,wz: i/=absw sx,sy,sz=gy*wz-gz*wy,gz*wx-gx*wz,gx*wy-gy*wx alpha=atan2(sy*gx-sx*gy,wx*gy-wy*gx) if alpha<0: alpha+=2*pi eta=acos(-gz) rho=atan2(-gx,-gy) if rho<0: rho+=2*pi cvert=[[4,4,4],[-4,4,4],[4,-4,4],[4,4,-4]] lne=1/sqrt(1-gz**2) neta=[-gy*lne,gx*lne,0] try: eta except: eta=alpha=rho=0 # quats=[[rho,[0,0,1]],[alpha,[-gx,-gy,-gz]],[eta,neta]] physvert=[None for i in range(8)] for j in range(4): i=cvert[j] cx=-i[0]*wx+i[1]*sx-i[2]*gx/2.8 cy=-i[0]*wy+i[1]*sy-i[2]*gy/2.8 cz=-i[0]*wz+i[1]*sz-i[2]*gz/2.8 cvert[j]=[cx,cy,cz] inds=((0,1,3,4),(6,7,5,2)) for i in range(4): physvert[inds[0][i]]=cvert[i] physvert[inds[1][i]]=list(map(lambda x: -x,cvert[i])) faces=((2,3,6,7,'#ff6060'),(0,1,4,5,'#a00000'),(0,3,4,7,'#60ff60'),(1,2,5,6,'#00a000'),(0,1,3,2,'#6060ff'),(4,5,7,6,'#0000a0')) faceord=sorted([0,1,2,3,4,5],key=lambda x: sum([physvert[n][2] for n in faces[x][:4]])) visvert=[] D=4*2048*.5/(.61*19.8) centx,centy=self.size/2 for i in physvert: z=i[2] rad=D*4*sqrt(48-z**2)/(65-z) visvert.append((rad*i[0]+centx,rad*i[1]+centy)) for i in faceord: scene_drawing.fill(faces[i][4]) scene_drawing.triangle_strip([visvert[j] for j in faces[i][:4]])
def update(self): gx, gy, gz = get_gravity() m = magnorm() if sum(m): mx, my, mz = m wx, wy, wz = my * gz - mz * gy, mz * gx - mx * gz, mx * gy - my * gx absw = sqrt(sum(map(lambda t: t**2, (wx, wy, wz)))) wx, wy, wz = list(map(lambda t: t / absw, (wx, wy, wz))) sx, sy, sz = gy * wz - gz * wy, gz * wx - gx * wz, gx * wy - gy * wx cvert = [[4, 4, 4], [-4, 4, 4], [-4, -4, 4], [4, -4, 4], [4, 4, -4], [-4, 4, -4], [-4, -4, -4], [4, -4, -4]] try: wx except: wx = wy = wz = sx = sy = sz = .5 ue = (-wx, -wy, -wz) un = (sx, sy, sz) uu = (-gx, -gy, -gz) physvert = [None for i in range(8)] for j in range(8): i = cvert[j] cx = i[0] * ue[0] + i[1] * un[0] + i[2] * uu[0] cy = i[0] * ue[1] + i[1] * un[1] + i[2] * uu[1] cz = i[0] * ue[2] + i[1] * un[2] + i[2] * uu[2] physvert[j] = [cx, cy, cz] faces = ((2, 3, 6, 7, (1, .38, .38, .9)), (0, 1, 4, 5, (.63, 0, 0, .9)), (0, 3, 4, 7, (.38, 1, .38, .9)), (1, 2, 5, 6, (0, .63, 0, .9)), (0, 1, 3, 2, (.38, .38, 1, .9)), (4, 5, 7, 6, (0, 0, .63, .9))) faceord = sorted( [0, 1, 2, 3, 4, 5], key=lambda t: sum([physvert[n][2] for n in faces[t][:4]])) visvert = [] D = 4 * self.size.width * .5 / (.61 * 19.8) centx, centy = self.size / 2 for i in physvert: x, y, z = i rad = D * 20 / (65 - z) visvert.append((rad * x + centx, rad * y + centy)) for i in faceord: scene_drawing.fill(faces[i][4]) points = tuple(visvert[j] for j in faces[i][:4]) scene_drawing.triangle_strip(points, points)
def update(self): gx,gy,gz=get_gravity() m=magnorm() if sum(m): mx,my,mz=m wx,wy,wz=my*gz-mz*gy,mz*gx-mx*gz,mx*gy-my*gx absw=sqrt(sum(map(lambda t: t**2,(wx,wy,wz)))) wx,wy,wz=list(map(lambda t: t/absw,(wx,wy,wz))) sx,sy,sz=gy*wz-gz*wy,gz*wx-gx*wz,gx*wy-gy*wx cvert=[[4,4,4],[-4,4,4],[-4,-4,4],[4,-4,4],[4,4,-4],[-4,4,-4],[-4,-4,-4],[4,-4,-4]] try: wx except: wx=wy=wz=sx=sy=sz=.5 ue=(-wx,-wy,-wz) un=(sx,sy,sz) uu=(-gx,-gy,-gz) physvert=[None for i in range(8)] for j in range(8): i=cvert[j] cx=i[0]*ue[0]+i[1]*un[0]+i[2]*uu[0] cy=i[0]*ue[1]+i[1]*un[1]+i[2]*uu[1] cz=i[0]*ue[2]+i[1]*un[2]+i[2]*uu[2] physvert[j]=[cx,cy,cz] faces=((2,3,6,7,(1,.38,.38,.9)),(0,1,4,5,(.63,0,0,.9)),(0,3,4,7,(.38,1,.38,.9)),(1,2,5,6,(0,.63,0,.9)),(0,1,3,2,(.38,.38,1,.9)),(4,5,7,6,(0,0,.63,.9))) faceord=sorted([0,1,2,3,4,5],key=lambda t: sum([physvert[n][2] for n in faces[t][:4]])) visvert=[] D=4*self.size.width*.5/(.61*19.8) centx,centy=self.size/2 for i in physvert: x,y,z=i rad=D*20/(65-z) visvert.append((rad*x+centx,rad*y+centy)) for i in faceord: scene_drawing.fill(faces[i][4]) points=tuple(visvert[j] for j in faces[i][:4]) scene_drawing.triangle_strip(points,points)
def update(self): gx, gy, gz = get_gravity() m = magnorm() if sum(m): mx, my, mz = m wx, wy, wz = my * gz - mz * gy, mz * gx - mx * gz, mx * gy - my * gx absw = sqrt(sum(map(lambda t: t**2, (wx, wy, wz)))) wx, wy, wz = list(map(lambda t: t / absw, (wx, wy, wz))) sx, sy, sz = gy * wz - gz * wy, gz * wx - gx * wz, gx * wy - gy * wx # print(sqrt(gx**2+hypot(gy,gz)**2),sqrt(sx**2+hypot(sy,sz)**2),sqrt(wx**2+hypot(wy,wz)**2)) '''alpha=atan2(sy*gx-sx*gy,wx*gy-wy*gx) if alpha<0: alpha+=2*pi eta=acos(-gz) rho=atan2(-gx,-gy) if rho<0: rho+=2*pi''' cvert = [[4, 4, 4], [-4, 4, 4], [-4, -4, 4], [4, -4, 4], [4, 4, -4], [-4, 4, -4], [-4, -4, -4], [4, -4, -4]] '''lne=1/sqrt(1-gz**2) neta=[-gy*lne,gx*lne,0] try: eta except: eta=alpha=rho=0''' # quats=[[rho,[0,0,1]],[alpha,[-gx,-gy,-gz]],[eta,neta]] try: wx except: wx = wy = wz = sx = sy = sz = .5 ue = (-wx, -wy, -wz) un = (sx, sy, sz) uu = (-gx, -gy, -gz) physvert = [None for i in range(8)] for j in range(8): i = cvert[j] cx = i[0] * ue[0] + i[1] * un[0] + i[2] * uu[0] cy = i[0] * ue[1] + i[1] * un[1] + i[2] * uu[1] cz = i[0] * ue[2] + i[1] * un[2] + i[2] * uu[2] physvert[j] = [cx, cy, cz] faces = ((2, 3, 6, 7, '#ff6060'), (0, 1, 4, 5, '#a00000'), (0, 3, 4, 7, '#60ff60'), (1, 2, 5, 6, '#00a000'), (0, 1, 3, 2, '#6060ff'), (4, 5, 7, 6, '#0000a0')) faceord = sorted( [0, 1, 2, 3, 4, 5], key=lambda t: sum([physvert[n][2] for n in faces[t][:4]])) visvert = [] D = 4 * 2048 * .5 / (.61 * 19.8) centx, centy = self.size / 2 '''basevert=[[wx,wy,wz],[sx,sy,sz],[gx,gy,gz]] vbvert=[]''' for i in physvert: x, y, z = i rad = D * 7 / (65 - z) visvert.append((rad * x + centx, rad * y + centy)) '''for i in basevert: x,y,z=i x*=10 y*=10 z*=10 rad=D*hypot(x,y)/(65-z) vbvert.append((rad*x+centx,rad*y+centy))''' '''if self.T: print('\n\ng:',gx,gy,gz) print('w:',wx,wy,wz) print('s:',sx,sy,sz) print('\nphys:\n',physvert) print('\nvis:\n',visvert) self.T=False''' for i in faceord: scene_drawing.fill(faces[i][4]) scene_drawing.triangle_strip([visvert[j] for j in faces[i][:4]]) '''colour=['#008000','#800000','#000080']
def update(self): if self.state == 'setup': if not self.ghostps or self.realps: cd = self.cdict Gx, Gy, npart, vmu, sigvmu, thmu, sigthmu = cd['Gx'], cd[ 'Gy'], cd['npart'], cd['vmu'], cd['sigvmu'], cd[ 'thmu'] * pi / 180, cd['sigthmu'] * pi / 180 self.ghostps = [] for i in range(npart): V = random.gauss(vmu, sigvmu) if thmu < 0: th = 2 * pi * random.random() else: th = random.gauss(thmu, sigthmu) v = vectd(V * cos(th), V * sin(th)) c = hsv2rgb(360 * random.random(), min(100, random.gauss(100, 15)), min(100, random.gauss(100, 25))) self.ghostps.append(particle(vectd(Gx, Gy), v, c)) self.realps = [] elif self.state == 'running': # TS=time.time() # DT=TS-self.timestamp DT = 1 / 60 self.deltat += DT # self.timestamp=TS ghlen = len(self.ghostps) resk = self.cdict['resk'] for j in range(ghlen + len(self.realps)): if j >= ghlen: i = self.realps[j - ghlen] else: i = self.ghostps[j] if self.boundson: COR = self.cdict['cor'] nx = i.p.x + i.v.x * 100 * DT if nx < 0: i.p.x = 0 i.v.x *= -COR i.v.y *= COR elif nx > 723: i.p.x = 723 i.v.x *= -COR i.v.y *= COR else: i.p.x = nx ny = i.p.y + i.v.y * 100 * DT if ny < 0: i.p.y = 0 i.v.x *= COR i.v.y *= -COR elif ny > 703: i.p.y = 703 i.v.x *= COR i.v.y *= -COR else: i.p.y = ny nx = i.p.x + i.v.x * 100 * DT if nx < 0: i.p.x = 0 i.v.x *= -COR i.v.y *= COR elif nx > 723: i.p.x = 723 i.v.x *= -COR i.v.y *= COR else: i.p += i.v * (100 * DT) if self.reson: i.v -= i.v.norm() * (i.v.dot(i.v) * resk) i.v += self.g * DT for i in self.ghostps + self.realps: scdr.fill(i.c) scdr.rect(i.p.x - 2, i.p.y - 2, 4, 4) if self.mainview: self.mainview['timelabel'].text = 'T = ' + str(self.deltat)
def update(self): if self.state == 'setup': if not self.ghostps or self.realps: cd = self.cdict Gx, Gy, npart, vmu, sigvmu, thmu, sigthmu = cd['Gx'], cd[ 'Gy'], cd['npart'], cd['vmu'], cd['sigvmu'], cd[ 'thmu'] * pi / 180, cd['sigthmu'] * pi / 180 self.ghostps = [] for i in range(npart): V = random.gauss(vmu, sigvmu) if thmu < 0: th = 2 * pi * random.random() else: th = random.gauss(thmu, sigthmu) v = vectd(V * cos(th), V * sin(th)) c = hsv2rgb(360 * random.random(), min(100, random.gauss(100, 15)), min(100, random.gauss(100, 25))) self.ghostps.append(particle(vectd(Gx, Gy), v, c)) self.realps = [] elif self.state == 'running': # TS=time.time() # DT=TS-self.timestamp DT = 1 / 60 self.deltat += DT # self.timestamp=TS rlen = len(self.realps) resk = self.cdict['resk'] if self.ghostps: reloc = [] COR = self.cdict['cor'] for j in range(rlen + len(self.ghostps)): if j >= rlen: i = self.ghostps[j - rlen] else: i = self.realps[j] for q in self.realps[:j] + self.realps[j + 1:]: if abs(q.p - i.p) < 4: c = (q.p - i.p).norm() nc = vectd(-c.y, c.x) vic, vqc = c.dot(i.v), c.dot(q.v) vin, vqn = nc.dot(i.v), nc.dot(q.v) vcmu = (vic + vqc) / 2 i.p -= c * 2 q.p += c * 2 i.v = c * (vcmu - vic * COR) + nc * vin q.v = c * (vcmu - vqc * COR) + nc * vqn if self.boundson: nx = i.p.x + i.v.x * 100 * DT if nx < 0: i.p.x = 0 i.v.x *= -COR elif nx > 723: i.p.x = 723 i.v.x *= -COR else: i.p.x = nx ny = i.p.y + i.v.y * 100 * DT if ny < 0: i.p.y = 0 i.v.y *= -COR elif ny > 703: i.p.y = 703 i.v.y *= -COR else: i.p.y = ny nx = i.p.x + i.v.x * 100 * DT if nx < 0: i.p.x = 0 i.v.x *= -COR elif nx > 723: i.p.x = 723 i.v.x *= -COR else: i.p += i.v * (100 * DT) if j >= rlen: if not sum( map( lambda x: abs(x.p - i.p) < 4, self.ghostps[:j - rlen] + self.ghostps[j + 1 - rlen:] + self.realps)): reloc.append(j - rlen) if self.reson: i.v -= i.v.norm() * (i.v.dot(i.v) * resk) i.v += self.g * DT if self.ghostps and reloc: for i in reloc: self.realps.append(self.ghostps[i]) for i in sorted(reloc, key=int.__neg__): del self.ghostps[i] for i in self.ghostps + self.realps: scdr.fill(i.c) scdr.rect(i.p.x - 2, i.p.y - 2, 4, 4) if self.mainview: self.mainview['timelabel'].text = 'T = ' + str(self.deltat)
def update(self): if self.state=='setup': if not self.ghostps or self.realps: cd=self.cdict Gx,Gy,npart,vmu,sigvmu,thmu,sigthmu= cd['Gx'],cd['Gy'],cd['npart'],cd['vmu'],cd['sigvmu'], cd['thmu']*pi/180,cd['sigthmu']*pi/180 self.ghostps=[] for i in range(npart): V=random.gauss(vmu,sigvmu) if thmu<0: th=2*pi*random.random() else: th=random.gauss(thmu,sigthmu) v=vectd(V*cos(th),V*sin(th)) c=hsv2rgb(360*random.random(), min(100,random.gauss(100,15)),min(100, random.gauss(100,25))) self.ghostps.append(particle(vectd(Gx,Gy),v,c)) self.realps=[] elif self.state=='running': # TS=time.time() # DT=TS-self.timestamp DT=1/60 self.deltat+=DT # self.timestamp=TS rlen=len(self.realps) resk=self.cdict['resk'] if self.ghostps: reloc=[] COR=self.cdict['cor'] for j in range(rlen+len(self.ghostps)): if j>=rlen: i=self.ghostps[j-rlen] else: i=self.realps[j] for q in self.realps[:j]+self.realps[j+1:]: if abs(q.p-i.p)<4: c=(q.p-i.p).norm() nc=vectd(-c.y,c.x) vic,vqc=c.dot(i.v),c.dot(q.v) vin,vqn=nc.dot(i.v),nc.dot(q.v) vcmu=(vic+vqc)/2 i.p-=c*2 q.p+=c*2 i.v=c*(vcmu-vic*COR)+nc*vin q.v=c*(vcmu-vqc*COR)+nc*vqn if self.boundson: nx=i.p.x+i.v.x*100*DT if nx<0: i.p.x=0 i.v.x*=-COR elif nx>723: i.p.x=723 i.v.x*=-COR else: i.p.x=nx ny=i.p.y+i.v.y*100*DT if ny<0: i.p.y=0 i.v.y*=-COR elif ny>703: i.p.y=703 i.v.y*=-COR else: i.p.y=ny nx=i.p.x+i.v.x*100*DT if nx<0: i.p.x=0 i.v.x*=-COR elif nx>723: i.p.x=723 i.v.x*=-COR else: i.p+=i.v*(100*DT) if j>=rlen: if not sum(map(lambda x:abs(x.p-i.p)<4, self.ghostps[:j-rlen]+self.ghostps[j+1-rlen:] +self.realps)): reloc.append(j-rlen) if self.reson: i.v-=i.v.norm()*(i.v.dot(i.v)*resk) i.v+=self.g*DT if self.ghostps and reloc: for i in reloc: self.realps.append(self.ghostps[i]) for i in sorted(reloc,key=int.__neg__): del self.ghostps[i] for i in self.ghostps+self.realps: scdr.fill(i.c) scdr.rect(i.p.x-2,i.p.y-2,4,4) if self.mainview: self.mainview['timelabel'].text='T = '+str(self.deltat)
def update(self): if self.state=='setup': if not self.ghostps or self.realps: cd=self.cdict Gx,Gy,npart,vmu,sigvmu,thmu,sigthmu= cd['Gx'],cd['Gy'],cd['npart'],cd['vmu'],cd['sigvmu'], cd['thmu']*pi/180,cd['sigthmu']*pi/180 self.ghostps=[] for i in range(npart): V=random.gauss(vmu,sigvmu) if thmu<0: th=2*pi*random.random() else: th=random.gauss(thmu,sigthmu) v=vectd(V*cos(th),V*sin(th)) c=hsv2rgb(360*random.random(), min(100,random.gauss(100,15)),min(100, random.gauss(100,25))) self.ghostps.append(particle(vectd(Gx,Gy),v,c)) self.realps=[] elif self.state=='running': # TS=time.time() # DT=TS-self.timestamp DT=1/60 self.deltat+=DT # self.timestamp=TS ghlen=len(self.ghostps) resk=self.cdict['resk'] for j in range(ghlen+len(self.realps)): if j>=ghlen: i=self.realps[j-ghlen] else: i=self.ghostps[j] if self.boundson: COR=self.cdict['cor'] nx=i.p.x+i.v.x*100*DT if nx<0: i.p.x=0 i.v.x*=-COR i.v.y*=COR elif nx>723: i.p.x=723 i.v.x*=-COR i.v.y*=COR else: i.p.x=nx ny=i.p.y+i.v.y*100*DT if ny<0: i.p.y=0 i.v.x*=COR i.v.y*=-COR elif ny>703: i.p.y=703 i.v.x*=COR i.v.y*=-COR else: i.p.y=ny nx=i.p.x+i.v.x*100*DT if nx<0: i.p.x=0 i.v.x*=-COR i.v.y*=COR elif nx>723: i.p.x=723 i.v.x*=-COR i.v.y*=COR else: i.p+=i.v*(100*DT) if self.reson: i.v-=i.v.norm()*(i.v.dot(i.v)*resk) i.v+=self.g*DT for i in self.ghostps+self.realps: scdr.fill(i.c) scdr.rect(i.p.x-2,i.p.y-2,4,4) if self.mainview: self.mainview['timelabel'].text='T = '+str(self.deltat)