Guest User

animator.py

a guest
Jul 3rd, 2019
977
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.74 KB | None | 0 0
  1. from graphviz import Graph
  2.  
  3. def convexComb (k, u, v):
  4.     if not len(u) == len(v):
  5.         raise ValueError("vectors need to have same length")
  6.     ans = []
  7.     for i in range(len(u)):
  8.         ans.append((1 - k) * u[i] + k * v[i])
  9.     return ans
  10.  
  11. class Animator:
  12.     def __init__ (self, path, fname, xl, xr, yl, yr):
  13.         self.vertices = {}
  14.         self.edges = {}
  15.         self.frameID = 0
  16.         self.path = path
  17.         self.fname = fname
  18.         self.xlMargin = xl
  19.         self.xrMargin = xr
  20.         self.ylMargin = yl
  21.         self.yrMargin = yr
  22.  
  23.     def addVertex (self, name, **attrs):
  24.         self.vertices[name] = attrs
  25.  
  26.     def setVertexAttr (self, name, attr, val):
  27.         self.vertices[name][attr] = val
  28.  
  29.     def setVertexPos (self, name, x, y):
  30.         self.vertices[name]["pos"] = str(x) + "," + str(y) + "!"
  31.  
  32.     def addEdge (self, u, v, fillRate, **attrs):
  33.         self.edges[(u, v)] = [0, fillRate, attrs]
  34.  
  35.     def setFillRate (self, u, v, fillRate):
  36.         self.edges[(u, v)][1] = fillRate
  37.  
  38.     def setFillDir (self, u, v, fillDir):
  39.         self.edges[(u, v)][0] = fillDir
  40.  
  41.     def draw (self):
  42.         G = Graph(format="png", engine="neato", directory = self.path)
  43.         G.attr("node", shape = "circle")
  44.         G.attr(splines = "true")
  45.         G.attr(sep = "1")
  46.  
  47.         G.node("dlMargin", pos = str(self.xlMargin) + "," + str(self.ylMargin) + "!",
  48.                color = "white", fontcolor = "white")
  49.         G.node("drMargin", pos = str(self.xrMargin) + "," + str(self.ylMargin) + "!",
  50.                color = "white", fontcolor = "white")
  51.         G.node("ulMargin", pos = str(self.xlMargin) + "," + str(self.yrMargin) + "!",
  52.                color = "white", fontcolor = "white")
  53.         G.node("urMargin", pos = str(self.xrMargin) + "," + str(self.yrMargin) + "!",
  54.                color = "white", fontcolor = "white")
  55.  
  56.         for (name, attrs) in self.vertices.items():
  57.             G.node(name, **attrs)
  58.         for (u, v), (fillDir, fillRate, attrs) in self.edges.items():
  59.             if fillRate > 0.005:
  60.                 if fillDir == 0:
  61.                     G.edge(u, v, **attrs, penwidth = "3", color = "black;" + str(fillRate) + ":white")
  62.                 else:
  63.                     G.edge(v, u, **attrs, penwidth = "3", color = "black;" + str(fillRate) + ":white")
  64.             if fillRate < 0.999:
  65.                 if fillDir == 0:
  66.                     G.edge(u, v, **attrs)
  67.                 else:
  68.                     G.edge(v, u, **attrs)
  69.         G.render(self.fname + str(self.frameID).zfill(4))
  70.         self.frameID += 1
  71.  
  72.     def sleep (self, frames):
  73.         for i in range(frames):
  74.             self.draw()
  75.  
  76.     def moveVertices (self, targets, steps = 40):
  77.         for vertex in targets:
  78.             if not "pos" in self.vertices[vertex]:
  79.                 raise ValueError("vertex does not have set position")
  80.  
  81.         currents = {}
  82.         for vertex in targets:
  83.             currents[vertex] = list(map(int, self.vertices[vertex]["pos"].replace("!", "").split(",")))
  84.  
  85.         for k in range(1, steps + 1):
  86.             for vertex in targets:
  87.                 cc = convexComb(k / steps, currents[vertex], targets[vertex])
  88.                 self.setVertexPos(vertex, cc[0], cc[1])
  89.             self.draw()        
  90.  
  91.     def fillEdge (self, u, v, step = 0.05):
  92.         (p, q) = (u, v)
  93.         if (u, v) in self.edges:
  94.             self.setFillDir(u, v, 0)
  95.             (p, q) = (u, v)
  96.         elif (v, u) in self.edges:
  97.             self.setFillDir(v, u, 1)
  98.             (p, q) = (v, u)
  99.         else:
  100.             raise ValueError("no such edge exists!")
  101.  
  102.         fillRate = 0
  103.         while fillRate < 0.999:
  104.             fillRate += step
  105.             self.setFillRate(p, q, fillRate)
  106.             self.draw()
Add Comment
Please, Sign In to add comment