Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.12 KB | None | 0 0
  1. from __future__ import division
  2. from math import sin,cos,tan,atan2,hypot,pi
  3. from copy import deepcopy
  4. x="x"
  5. y="y"
  6. azimuth="azimuth"
  7. displacement="displacement"
  8. pi=3.141592653
  9.  
  10. class flowglyph:
  11.     def __init__(self,left=None,up=None,right=None,down=None):
  12.         self.left=left;self.up=up;self.right=right;self.down=down;
  13.         self.value=0
  14.         self.inpath=0
  15. class amplifierglyph:
  16.     def __init__(self,magnitude,left=None,up=None,right=None,down=None):
  17.         self.left=left;self.up=up;self.right=right;self.down=down;
  18.         self.magnitude=magnitude
  19.         self.value=0
  20.         self.inpath=0
  21. class traceglyph:
  22.     def __init__(self,varname):
  23.         self.varname=varname
  24.         self.value=0
  25.         self.inpath=0
  26. class circleglyph:
  27.     def __init__(self,startangle=0,left=None,up=None,right=None,down=None):
  28.         self.left=left;self.up=up;self.right=right;self.down=down;
  29.         self.value=startangle
  30.         self.inpath=0
  31.     def setvals(self):
  32.         self.value=self.value%(2*pi)
  33.         self.y=sin(self.value)
  34.         self.x=cos(self.value)
  35.         #self.hypot=hypot(self.x,self.y)
  36.        
  37. def isnum(obj):
  38.     return isinstance(obj,float) or isinstance(obj,int)
  39.        
  40.        
  41. class grid:
  42.     def __init__(self,constants={},glyphs={}): #glyph {(x,y):glyph..}
  43.         self.constants=constants
  44.         self.glyphs=glyphs
  45.     def pipein(self,pos,value):
  46.         for i in self.glyphs:
  47.             self.glyphs[i].inputs=0
  48.         self.glyphs[pos].value+=value
  49.         self.eminatefrom(pos)
  50.     def drawpath(self):
  51.         s=""
  52.         for j in range(32):
  53.            
  54.             for i in range(32):
  55.                 s+= str(self.glyphs[(i,j)].inpath) if (i,j) in self.glyphs else " "
  56.             s+="\n"
  57.         return s
  58.     def traces(self):
  59.         o={}
  60.         for i in self.glyphs:
  61.             if isinstance(self.glyphs[i],traceglyph):
  62.                 o[self.glyphs[i].varname]=self.glyphs[i].value
  63.         return o
  64.     def next(self):
  65.         for i in self.glyphs:
  66.             g=self.glyphs[i]
  67.             if isinstance(g,flowglyph) or isinstance(g,amplifierglyph) or isinstance(g,traceglyph):
  68.                 self.glyphs[i].value=0
  69.     def calculatepaths(self,pos,first=True):
  70.         if first:
  71.             for i in self.glyphs:
  72.                 self.glyphs[i].inpath=0
  73.         currentglyph=self.glyphs[pos]
  74.         if isinstance(currentglyph,traceglyph):
  75.             currentglyph.inpath+=1
  76.         else:
  77.             if currentglyph.inpath>0:
  78.                 currentglyph.inpath+=1
  79.             else:
  80.                 currentglyph.inpath+=1
  81.                 direction=[i!=None for i in [currentglyph.left,currentglyph.up,currentglyph.right,currentglyph.down]]
  82.                 if direction[0]:#left
  83.                     self.calculatepaths((pos[0]-1,pos[1]),False)
  84.                 if direction[1]:#up
  85.                     self.calculatepaths((pos[0],pos[1]-1),False)                
  86.                 if direction[2]:#right
  87.                     self.calculatepaths((pos[0]+1,pos[1]),False)
  88.                 if direction[3]:#down
  89.                     self.calculatepaths((pos[0],pos[1]+1),False)
  90.     def eminatedirection(self,val,np):
  91.         if np in self.glyphs:
  92.                 nextglyph=self.glyphs[np]
  93.                 if isnum(val):
  94.  
  95.                     nextglyph.value+=val*val
  96.                     nextglyph.inputs+=1
  97.                     if isinstance(nextglyph,circleglyph):
  98.                         nextglyph.setvals()
  99.                     if nextglyph.inputs==nextglyph.inpath:
  100.                         self.eminatefrom(np)
  101.                 else:
  102.                     if val==x:
  103.                         nextglyph.value+=currentglyph.x
  104.                     if val==y:
  105.                         nextglyph.value+=currentglyph.y
  106.                     if val==azimuth:
  107.                         nextglyph.value+=val
  108.                     if val!=None:
  109.                         nextglyph.inputs+=1
  110.                         if isinstance(nextglyph,circleglyph):
  111.                             nextglyph.setvals()
  112.                         if nextglyph.inputs==nextglyph.inpath:
  113.                             self.eminatefrom(np)
  114.            
  115.     def eminatefrom(self,pos):
  116.         currentglyph=self.glyphs[pos]
  117.         if isinstance(currentglyph,traceglyph):
  118.             pass
  119.         else:
  120.             val=deepcopy(currentglyph.value)
  121.             if isinstance(currentglyph,amplifierglyph):
  122.                 mag=currentglyph.magnitude
  123.                 if isinstance(mag,str):
  124.                     mag=self.constants[mag]
  125.                 val*=mag
  126.             #left
  127.             leftval=currentglyph.left
  128.             np=(pos[0]-1,pos[1])
  129.             self.eminatedirection(leftval,np)
  130.             #up
  131.             upval=currentglyph.up
  132.             np=(pos[0],pos[1]-1)
  133.             self.eminatedirection(upval,np)
  134.             #right            
  135.             rightval=currentglyph.right
  136.             np=(pos[0]+1,pos[1])
  137.             self.eminatedirection(rightval,np)
  138.             #down                      
  139.             downval=currentglyph.down
  140.             np=(pos[0],pos[1]+1)
  141.             eminatedirection(downval),
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement