Showing
14 changed files
with
82 additions
and
6 deletions
... | @@ -87,16 +87,19 @@ class CAniTikZ (CAni) : | ... | @@ -87,16 +87,19 @@ class CAniTikZ (CAni) : |
87 | % (key, self.__class__.__name__)) | 87 | % (key, self.__class__.__name__)) |
88 | def ptr (self) : | 88 | def ptr (self) : |
89 | return Pointer(self) | 89 | return Pointer(self) |
90 | - def tex (self, **tikz) : | 90 | + def tex (self, head=None, tail=None, **tikz) : |
91 | opt = TikZ(tikz) | 91 | opt = TikZ(tikz) |
92 | return cleandoc(r"""\begin{{tikzpicture}}[{opt.tikzpicture}] | 92 | return cleandoc(r"""\begin{{tikzpicture}}[{opt.tikzpicture}] |
93 | - {code} | 93 | + {head}{code}{tail} |
94 | \end{{tikzpicture}} | 94 | \end{{tikzpicture}} |
95 | - """).format(opt=opt, code="\n ".join(self.tikz(**tikz).splitlines())) | 95 | + """).format(opt=opt, |
96 | + head=(head + "\n") if head else "", | ||
97 | + tail=("\n" + tail) if tail else "", | ||
98 | + code="\n ".join(self.tikz(**tikz).splitlines())) | ||
96 | 99 | ||
97 | class Pointer (CAniTikZ) : | 100 | class Pointer (CAniTikZ) : |
98 | def __init__ (self, data) : | 101 | def __init__ (self, data) : |
99 | - self._d = data | 102 | + self.__dict__.update(_d=data, nodeid=None) |
100 | def val (self) : | 103 | def val (self) : |
101 | return self._d | 104 | return self._d |
102 | def __tikz__ (self, src, opt) : | 105 | def __tikz__ (self, src, opt) : |
... | @@ -109,6 +112,10 @@ class Pointer (CAniTikZ) : | ... | @@ -109,6 +112,10 @@ class Pointer (CAniTikZ) : |
109 | return self._d[key] | 112 | return self._d[key] |
110 | def __setitem__ (self, key, val) : | 113 | def __setitem__ (self, key, val) : |
111 | self._d[key] = val | 114 | self._d[key] = val |
115 | + def __getattr__ (self, key) : | ||
116 | + return getattr(self._d, key) | ||
117 | + def __setattr__ (self, key, val) : | ||
118 | + setattr(self._d, key, val) | ||
112 | 119 | ||
113 | class Value (CAniTikZ) : | 120 | class Value (CAniTikZ) : |
114 | def __init__ (self, init=None, **tikz) : | 121 | def __init__ (self, init=None, **tikz) : | ... | ... |
1 | -all: heap.pdf qs-partition.pdf qs-main.pdf | 1 | +all: heap.pdf qs-partition.pdf qs-main.pdf stack-push.pdf |
2 | +gif: qs-partition.gif qs-main.gif stack-push.gif | ||
3 | +mp4: qs-partition.mp4 qs-main.mp4 stack-push.mp4 | ||
2 | 4 | ||
3 | %.pdf: %.py tpl.tex | 5 | %.pdf: %.py tpl.tex |
4 | ln -sf ../codanim . | 6 | ln -sf ../codanim . |
... | @@ -8,5 +10,22 @@ all: heap.pdf qs-partition.pdf qs-main.pdf | ... | @@ -8,5 +10,22 @@ all: heap.pdf qs-partition.pdf qs-main.pdf |
8 | pdflatex tpl | 10 | pdflatex tpl |
9 | cp tpl.pdf $@ | 11 | cp tpl.pdf $@ |
10 | 12 | ||
13 | +%.gif: %.pdf | ||
14 | + rm -rf _gif | ||
15 | + mkdir _gif | ||
16 | + gs -dSAFER -DBATCH -dNOPAUSE -sDEVICE=png16m -r400 -sOutputFile=_gif/%03d.png $< | ||
17 | + mogrify -resize 800x600 -format gif _gif/*.png | ||
18 | + gifsicle -k64 -d80 -l0 _gif/*.gif -O2 -o $@ | ||
19 | + rm -rf _gif | ||
20 | + | ||
21 | +%.mp4: %.pdf | ||
22 | + rm -rf _mp4 | ||
23 | + mkdir _mp4 | ||
24 | + gs -dSAFER -DBATCH -dNOPAUSE -sDEVICE=png16m -r400 -sOutputFile=_mp4/%03d.png $< | ||
25 | + ffmpeg -r 1 -s 1024x768 -i _mp4/%03d.png -vcodec libx264 -crf 25 -pix_fmt yuv420p $@ | ||
26 | + rm -rf _mp4 | ||
27 | + | ||
11 | clean: | 28 | clean: |
12 | rm -f out.* $$(grep '^*' ../.gitignore) | 29 | rm -f out.* $$(grep '^*' ../.gitignore) |
30 | + rm -rf _gif | ||
31 | + rm -rf _mp4 | ... | ... |
No preview for this file type
examples/qs-main.gif
0 → 100644
5.08 MB
examples/qs-main.mp4
0 → 100644
No preview for this file type
No preview for this file type
examples/qs-partition.gif
0 → 100644
841 KB
examples/qs-partition.mp4
0 → 100644
No preview for this file type
No preview for this file type
examples/stack-push.gif
0 → 100644
87.2 KB
examples/stack-push.mp4
0 → 100644
No preview for this file type
examples/stack-push.pdf
0 → 100644
No preview for this file type
examples/stack-push.py
0 → 100644
1 | +from codanim.data import Heap, Struct, Value | ||
2 | +from codanim.flow import FUNC, BLOCK, ENV, WS, DECL, EXPR, STMT | ||
3 | + | ||
4 | +heap = Heap() | ||
5 | + | ||
6 | +stack = heap.new(Struct({"val": Value(1), "next": None})) | ||
7 | +stack = heap.new(Struct({"val": Value(2), "next": stack})) | ||
8 | +s = Value(stack, nodeid="s") | ||
9 | +t = Value(None, nodeid="t", value={"xshift": "-25mm"}) | ||
10 | + | ||
11 | +v = Heap(heap={"grow": "above", | ||
12 | + "separation": "0pt"}) | ||
13 | +h = Heap(heap={"grow": "right"}, | ||
14 | + ) | ||
15 | +h.new(t) | ||
16 | +h.new(s) | ||
17 | +v.new(heap) | ||
18 | +v.new(h) | ||
19 | + | ||
20 | +push = FUNC(BLOCK(ENV("s", s), | ||
21 | + ENV("u", Value(3)), | ||
22 | + ENV("top", t), | ||
23 | + ENV("new", heap.new), | ||
24 | + ENV("Struct", Struct), | ||
25 | + ENV("Value", Value), | ||
26 | + WS("\n "), | ||
27 | + DECL("top", EXPR("new(Struct({'val': None, 'next': None}))", | ||
28 | + src="malloc(sizeof(StackCell))"), | ||
29 | + src="Stack {name} = {init};"), | ||
30 | + WS("\n "), | ||
31 | + STMT("top.val = u", src="top->val = u;"), | ||
32 | + WS("\n "), | ||
33 | + STMT("top.next = s", src="top->next = *s;"), | ||
34 | + WS("\n "), | ||
35 | + STMT("s = top", src="*s = top;"), | ||
36 | + WS("\n")), | ||
37 | + src="void stackPush (Stack* s, uint u) {{{body}}}\n") | ||
38 | + | ||
39 | +# run code and save its animation | ||
40 | +push.IP += 1 | ||
41 | +push() | ||
42 | +with open("out.code", "w") as out : | ||
43 | + out.write(push.tex()) | ||
44 | + | ||
45 | +# save data and animation | ||
46 | +with open("out.tikz", "w") as out : | ||
47 | + out.write(v.tex(tail=r""" | ||
48 | + \node[above] at (s.north) {\texttt{s}}; | ||
49 | + \node[above] at (t.north) {\texttt{top}}; | ||
50 | + """)) |
-
Please register or login to post a comment