-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.py
189 lines (161 loc) · 7.03 KB
/
scene.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from manim import *
SVG_FOLDER = "svg"
FROM_SVG = lambda name: SVG_FOLDER + "/" + name + ".svg" # I'll use an inline (which on python can be translated into lambda) that should be faster to execute unlike a normal function - Nex
class Scene1(Scene):
def intro(self):
vertices = [1, 2, 3, 4, 5, 6, 7, 8]
edges = [(1, 7), (1, 8), (2, 3), (2, 4), (2, 5), (2, 8), (3, 4), (6, 1), (6, 2), (6, 3), (7, 2), (7, 4)]
autolayouts = ["spring", "circular", "kamada_kawai", "planar", "random", "shell", "spectral", "spiral"]
graphs = [Graph(vertices, edges, layout = lt).scale(0.5) for lt in autolayouts]
r1 = VGroup(*graphs[:3]).arrange().shift(UP * 2.5)
r2 = VGroup(*graphs[3:6]).arrange()
r3 = VGroup(*graphs[6:]).arrange().shift(DOWN * 2.5)
self.play(Create(r1), Create(r2), Create(r3), run_time = 2)
self.wait()
self.play(FadeOut(r1), FadeOut(r2), FadeOut(r3), run_time = 1)
def neural_network(self):
edges = []
partitions = []
c = 0
layers = [2, 3, 3, 2] # The number of neurons in each layer
for i in layers:
partitions.append(list(range(c + 1, c + i + 1)))
c += i
for i, v in enumerate(layers[1:]):
last = sum(layers[:i + 1])
for j in range(v):
for k in range(last - layers[i], last):
edges.append((k + 1, j + last + 1))
vertices = np.arange(1, sum(layers) + 1)
graph = Graph(
vertices,
edges,
layout = 'partite',
partitions = partitions,
layout_scale = 3,
vertex_config = {'radius': 0.20},
)
self.play(Create(graph), run_time = 2)
self.wait()
self.play(FadeOut(graph), run_time = 1)
self.wait()
def metro(self):
# Initialize groups for dots and lines
dots = VGroup()
lines = VGroup()
# Metro stations setup
stations = {
"A": [0, -3, 0], "B": [0, -2, 0], "C": [0, -1, 0],
"D": [1, 0, 0], "E": [1, -2, 0], "F": [0, -1, 0],
"G": [2, -1, 0], "H": [2, 0, 0], "I": [2, 1, 0],
"J": [2, 2, 0], "K": [-3, 2, 0], "L": [-2, 2, 0],
"M": [-1, 2, 0], "N": [0, 1, 0], "O": [1, 0, 0],
"P": [2, -1, 0], "Q": [3, 0, 0], "R": [4, -1, 0]
}
edges = [
("A", "B", BLUE), ("B", "C", BLUE), ("C", "D", BLUE),
("E", "G", RED), ("G", "H", RED), ("H", "I", RED),
("I", "J", RED), ("K", "L", GREEN), ("L", "M", GREEN),
("M", "N", GREEN), ("N", "O", GREEN), ("O", "P", GREEN),
("P", "Q", GREEN), ("Q", "R", GREEN)
]
# Create dots
for data in stations.items():
dot = Dot(point = data[1], color = WHITE)
self.play(Create(dot), run_time = 0.1)
dots.add(dot) # Add dot to group
# Create lines
for start, end, color in edges:
line = Line(start = stations[start], end = stations[end], color = color, stroke_width = 8)
self.play(Create(line), run_time = 0.3)
lines.add(line) # Add line to group
# Pause to view the scene
self.wait()
# Fade out all dots and lines
self.play(FadeOut(dots), FadeOut(lines))
self.wait()
def bank(self):
banks = VGroup()
coins = VGroup()
for pos in [LEFT, RIGHT, UP, DOWN]:
bank = SVGMobject(FROM_SVG("bank")).scale(0.3)
self.play(FadeIn(bank.shift(pos * 2)), run_time = 0.7)
banks.add(bank)
for index, pos in [[3, LEFT], [3, RIGHT], [2, LEFT], [2, DOWN]]:
coin = SVGMobject(FROM_SVG("coin")).scale(0.2)
self.play(FadeIn(coin.next_to(banks[index], pos)), run_time = 0.7)
coins.add(coin)
# Move coin to bank 2
self.play(coins[0].animate.next_to(banks[0], LEFT), run_time = 0.7)
self.play(coins[3].animate.next_to(banks[1], RIGHT), run_time = 0.7)
self.wait(2)
self.play(FadeOut(banks), FadeOut(coins))
self.wait()
def servers(self):
vertices = [1, 2, 3, 4, 5, 6, 7]
edges = [(1, 7), (2, 4), (1, 4), (4, 6), (5, 6), (6, 3), (3, 7)]
graphics = {}
for num in vertices:
dot = Dot(radius = 0.3)
server = SVGMobject(FROM_SVG("server")).scale(0.17)
server.insert(0, dot)
graphics[num] = server
#SVG
graph = Graph(vertices, edges, layout = 'circular', layout_scale = 3, vertex_mobjects = graphics)
self.play(Create(graph), run_time = 4)
self.wait()
#color the edges
graph.vertices[2].set_color(BLUE)
graph.vertices[4].set_color(BLUE)
graph.vertices[1].set_color(RED)
graph.vertices[7].set_color(RED)
graph.vertices[3].set_color(GREEN)
graph.vertices[6].set_color(GREEN)
# Animate nodes 1 and 2 moving outwards and adjust the connected edges
#node1 = graph.vertices[1]
#node2 = graph.vertices[2]
#node3 = graph.vertices[3]
#node4 = graph.vertices[4]
#node5 = graph.vertices[5]
#node6 = graph.vertices[6]
#node7 = graph.vertices[7]
#node8 = graph.vertices[8]
#new_pos_node1_7 = np.array([[0, 0, 0],[0, -3, 0]])
#new_pos_node2_5 = np.array([[1,0, 0],[1, -3, 0]])
#new_pos_node1_4 = np.array([[2, 0, 0],[2, -3, 0]])
#new_pos_node6_8 = np.array([[3, 0, 0],[3, -3, 0]])
#new_pos_node2_3 = np.array([[4, 0, 0],[4, -3, 0]])
#graph.remove_edges((1, 4), (3, 2))
#graph2.remove_edges((1,7), (2,5), (6,8))
#graph.remove_vertices(3,4)
#graph2.remove_vertices(5,6,7,8)
# Move the nodes
#self.play(
# node1.animate.move_to(new_pos_node1_7[0]), node7.animate.move_to(new_pos_node1_7[1]),
# node2.animate.move_to(new_pos_node2_5[0]), node5.animate.move_to(new_pos_node2_5[1]),
# node6.animate.move_to(new_pos_node6_8[0]), node8.animate.move_to(new_pos_node6_8[1]),
# graph2.vertices[1].animate.move_to(new_pos_node1_4[0]), graph2.vertices[4].animate.move_to(new_pos_node1_4[1]),
# graph2.vertices[2].animate.move_to(new_pos_node2_3[0]), graph2.vertices[3].animate.move_to(new_pos_node2_3[1]),
# run_time=5)
#
#self.wait(3)
self.wait(4)
# The solution algorithm
def test(self):
# Create vertical lines one next to the other
dots = VGroup()
lines = VGroup()
for i in range(1, 8):
dot = Dot(point = DOWN * 5, color = RED).next_to(dots, RIGHT * 2)
line = Line(start = DOWN * 3, end = DOWN, color = WHITE).next_to(lines, RIGHT * 2)
lines.add(line)
dots.add(dot)
self.play(Create(lines), run_time = 4)
self.play(Create(dots), run_time = 4)
def construct(self):
self.intro()
self.neural_network()
self.metro()
self.bank()
self.servers()
#self.test()