Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import division
- from math import sin,cos,tan,atan2,hypot,pi
- from copy import deepcopy
- x="x"
- y="y"
- azimuth="azimuth"
- displacement="displacement"
- pi=3.141592653
- class flowglyph:
- def __init__(self,left=None,up=None,right=None,down=None):
- self.left=left;self.up=up;self.right=right;self.down=down;
- self.value=0
- self.inpath=0
- class amplifierglyph:
- def __init__(self,magnitude,left=None,up=None,right=None,down=None):
- self.left=left;self.up=up;self.right=right;self.down=down;
- self.magnitude=magnitude
- self.value=0
- self.inpath=0
- class traceglyph:
- def __init__(self,varname):
- self.varname=varname
- self.value=0
- self.inpath=0
- class circleglyph:
- def __init__(self,startangle=0,left=None,up=None,right=None,down=None):
- self.left=left;self.up=up;self.right=right;self.down=down;
- self.value=startangle
- self.inpath=0
- def setvals(self):
- self.value=self.value%(2*pi)
- self.y=sin(self.value)
- self.x=cos(self.value)
- #self.hypot=hypot(self.x,self.y)
- def isnum(obj):
- return isinstance(obj,float) or isinstance(obj,int)
- class grid:
- def __init__(self,constants={},glyphs={}): #glyph {(x,y):glyph..}
- self.constants=constants
- self.glyphs=glyphs
- def pipein(self,pos,value):
- for i in self.glyphs:
- self.glyphs[i].inputs=0
- self.glyphs[pos].value+=value
- self.eminatefrom(pos)
- def drawpath(self):
- s=""
- for j in range(32):
- for i in range(32):
- s+= str(self.glyphs[(i,j)].inpath) if (i,j) in self.glyphs else " "
- s+="\n"
- return s
- def traces(self):
- o={}
- for i in self.glyphs:
- if isinstance(self.glyphs[i],traceglyph):
- o[self.glyphs[i].varname]=self.glyphs[i].value
- return o
- def next(self):
- for i in self.glyphs:
- g=self.glyphs[i]
- if isinstance(g,flowglyph) or isinstance(g,amplifierglyph) or isinstance(g,traceglyph):
- self.glyphs[i].value=0
- def calculatepaths(self,pos,first=True):
- if first:
- for i in self.glyphs:
- self.glyphs[i].inpath=0
- currentglyph=self.glyphs[pos]
- if isinstance(currentglyph,traceglyph):
- currentglyph.inpath+=1
- else:
- if currentglyph.inpath>0:
- currentglyph.inpath+=1
- else:
- currentglyph.inpath+=1
- direction=[i!=None for i in [currentglyph.left,currentglyph.up,currentglyph.right,currentglyph.down]]
- if direction[0]:#left
- self.calculatepaths((pos[0]-1,pos[1]),False)
- if direction[1]:#up
- self.calculatepaths((pos[0],pos[1]-1),False)
- if direction[2]:#right
- self.calculatepaths((pos[0]+1,pos[1]),False)
- if direction[3]:#down
- self.calculatepaths((pos[0],pos[1]+1),False)
- def eminatedirection(self,val,np):
- if np in self.glyphs:
- nextglyph=self.glyphs[np]
- if isnum(val):
- nextglyph.value+=val*val
- nextglyph.inputs+=1
- if isinstance(nextglyph,circleglyph):
- nextglyph.setvals()
- if nextglyph.inputs==nextglyph.inpath:
- self.eminatefrom(np)
- else:
- if val==x:
- nextglyph.value+=currentglyph.x
- if val==y:
- nextglyph.value+=currentglyph.y
- if val==azimuth:
- nextglyph.value+=val
- if val!=None:
- nextglyph.inputs+=1
- if isinstance(nextglyph,circleglyph):
- nextglyph.setvals()
- if nextglyph.inputs==nextglyph.inpath:
- self.eminatefrom(np)
- def eminatefrom(self,pos):
- currentglyph=self.glyphs[pos]
- if isinstance(currentglyph,traceglyph):
- pass
- else:
- val=deepcopy(currentglyph.value)
- if isinstance(currentglyph,amplifierglyph):
- mag=currentglyph.magnitude
- if isinstance(mag,str):
- mag=self.constants[mag]
- val*=mag
- #left
- leftval=currentglyph.left
- np=(pos[0]-1,pos[1])
- self.eminatedirection(leftval,np)
- #up
- upval=currentglyph.up
- np=(pos[0],pos[1]-1)
- self.eminatedirection(upval,np)
- #right
- rightval=currentglyph.right
- np=(pos[0]+1,pos[1])
- self.eminatedirection(rightval,np)
- #down
- downval=currentglyph.down
- np=(pos[0],pos[1]+1)
- eminatedirection(downval),
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement