forked from Elteoremadebeethoven/MyAnimations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththumbnail.py
257 lines (230 loc) · 8.45 KB
/
thumbnail.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#from big_ol_pile_of_manim_imports import *
from manimlib.imports import *
class underline(Line):
def __init__(self,texto,buff=0.07,**kwargs):
Line.__init__(self,texto.get_corner(DL),texto.get_corner(DR),**kwargs)
self.shift(DOWN*buff)
NEW_BLUE = "#68a8e1"
COLOR_SYMBOL = "#fffab3"
class Thumbnail(GraphScene):
CONFIG = {
"y_max": 8,
"y_axis_height": 5,
}
def construct(self):
self.add_title()
self.show_function_graph()
def add_title(self):
title = self.title = TextMobject("\\sc Manim Tutorial").scale(2.3)
title.to_edge(UP)
h_line = Line(LEFT, RIGHT)
h_line.set_height(FRAME_WIDTH - 2 * LARGE_BUFF)
h_line.next_to(title, DOWN)
h_line.set_stroke(WHITE,3)
title.to_edge(UP+LEFT)
title.shift(RIGHT)
self.title=title
self.lin_h = h_line
def show_function_graph(self):
ul=underline(self.title)
self.setup_axes(animate=False)
self.add_foreground_mobjects(self.title,ul)
def func(x):
return 0.1 * (x + 3-5) * (x - 3-5) * (x-5) + 5
def rect(x):
return 2.775*(x-1.5)+3.862
recta = self.get_graph(rect,x_min=-1,x_max=5)
graph = self.get_graph(func,x_min=0.2,x_max=9)
graph.set_color(NEW_BLUE)
input_tracker_p1 = ValueTracker(1.5)
input_tracker_p2 = ValueTracker(3.5)
def get_x_value_p1():
return input_tracker_p1.get_value()
def get_x_value_p2():
return input_tracker_p2.get_value()
def get_y_value_p1():
return graph.underlying_function(get_x_value_p1())
def get_y_value_p2():
return graph.underlying_function(get_x_value_p2())
def get_x_point_p1():
return self.coords_to_point(get_x_value_p1(), 0)
def get_x_point_p2():
return self.coords_to_point(get_x_value_p2(), 0)
def get_y_point_p1():
return self.coords_to_point(0, get_y_value_p1())
def get_y_point_p2():
return self.coords_to_point(0, get_y_value_p2())
def get_graph_point_p1():
return self.coords_to_point(get_x_value_p1(), get_y_value_p1())
def get_graph_point_p2():
return self.coords_to_point(get_x_value_p2(), get_y_value_p2())
def get_v_line_p1():
return DashedLine(get_x_point_p1(), get_graph_point_p1(), stroke_width=2)
def get_v_line_p2():
return DashedLine(get_x_point_p2(), get_graph_point_p2(), stroke_width=2)
def get_h_line_p1():
return DashedLine(get_graph_point_p1(), get_y_point_p1(), stroke_width=2)
def get_h_line_p2():
return DashedLine(get_graph_point_p2(), get_y_point_p2(), stroke_width=2)
#
input_triangle_p1 = RegularPolygon(n=3, start_angle=TAU / 4)
output_triangle_p1 = RegularPolygon(n=3, start_angle=0)
for triangle in input_triangle_p1, output_triangle_p1:
triangle.set_fill(WHITE, 1)
triangle.set_stroke(width=0)
triangle.scale(0.1)
#
input_triangle_p2 = RegularPolygon(n=3, start_angle=TAU / 4)
output_triangle_p2 = RegularPolygon(n=3, start_angle=0)
for triangle in input_triangle_p2, output_triangle_p2:
triangle.set_fill(WHITE, 1)
triangle.set_stroke(width=0)
triangle.scale(0.1)
#
input_triangle_p1.add_updater(
lambda m: m.move_to(get_x_point_p1(), DOWN)
)
output_triangle_p1.add_updater(lambda m: m.move_to(get_y_point_p1(), LEFT)
)
#
input_triangle_p2.add_updater(lambda m: m.move_to(get_x_point_p2(), DOWN)
)
output_triangle_p2.add_updater(lambda m: m.move_to(get_y_point_p2(), LEFT)
)
#
x_label_p1 = TexMobject("a")
x_label_p1.add_updater(lambda ma: ma.next_to(input_triangle_p1, DOWN, SMALL_BUFF)
)
output_label_p1 = TexMobject("f(a)")
output_label_p1.add_updater(lambda ma: ma.next_to(
output_triangle_p1, LEFT, SMALL_BUFF)
)
#
x_label_p2 = TexMobject("b")
x_label_p2.add_updater(lambda mb: mb.next_to(input_triangle_p2, DOWN, SMALL_BUFF)
)
output_label_p2 = TexMobject("f(b)")
output_label_p2.add_updater(lambda mb: mb.next_to(
output_triangle_p2, LEFT, SMALL_BUFF)
)
#
v_line_p1 = get_v_line_p1()
v_line_p1.add_updater(lambda vla: Transform(vla, get_v_line_p1())
)
#
v_line_p2 = get_v_line_p2()
v_line_p2.add_updater(lambda vlb: Transform(vlb, get_v_line_p2())
)
#
h_line_p1 = get_h_line_p1()
h_line_p1.add_updater(lambda hla: Transform(hla, get_h_line_p1())
)
#
h_line_p2 = get_h_line_p2()
h_line_p2.add_updater(lambda hlb: Transform(hlb, get_h_line_p2())
)
#
graph_dot_p1 = Dot(color=COLOR_SYMBOL)
graph_dot_p1.add_updater(lambda ma: ma.move_to(get_graph_point_p1())
)
#
graph_dot_p2 = Dot(color=COLOR_SYMBOL)
graph_dot_p2.add_updater(lambda mb: mb.move_to(get_graph_point_p2())
)
#
self.play(
ShowCreation(graph),
)
# Animacion
self.add_foreground_mobject(graph_dot_p1)
self.add_foreground_mobject(graph_dot_p2)
self.play(
DrawBorderThenFill(input_triangle_p1),
Write(x_label_p1),
ShowCreation(v_line_p1),
GrowFromCenter(graph_dot_p1),
ShowCreation(h_line_p1),
Write(output_label_p1),
DrawBorderThenFill(output_triangle_p1),
DrawBorderThenFill(input_triangle_p2),
Write(x_label_p2),
ShowCreation(v_line_p2),
GrowFromCenter(graph_dot_p2),
ShowCreation(h_line_p2),
Write(output_label_p2),
DrawBorderThenFill(output_triangle_p2),
run_time=0.5
)
self.add(
input_triangle_p2,
x_label_p2,
graph_dot_p2,
v_line_p2,
h_line_p2,
output_triangle_p2,
output_label_p2,
)
###################
pendiente_recta = self.get_secant_slope_group(
1.9, recta, dx = 1.4,
df_label = None,
dx_label = None,
dx_line_color = PURPLE,
df_line_color= ORANGE,
)
grupo_secante = self.get_secant_slope_group(
1.5, graph, dx = 2,
df_label = None,
dx_label = None,
dx_line_color = "#942357",
df_line_color= "#3f7d5c",
secant_line_color = RED,
)
start_dx = grupo_secante.kwargs["dx"]
start_x = grupo_secante.kwargs["x"]
def update_func_0(group, alpha):
dx = interpolate(start_dx, 4, alpha)
x = interpolate(start_x, 1.5, alpha)
kwargs = dict(grupo_secante.kwargs)
kwargs["dx"] = dx
kwargs["x"] = x
new_group = self.get_secant_slope_group(**kwargs)
group.become(new_group)
return group
def update_func_1(group, alpha):
dx = interpolate(start_dx, 0.001, alpha)
x = interpolate(start_x, 1.5, alpha)
kwargs = dict(grupo_secante.kwargs)
kwargs["dx"] = dx
kwargs["x"] = x
new_group = self.get_secant_slope_group(**kwargs)
group.become(new_group)
return group
self.add(
input_triangle_p2,
graph_dot_p2,
v_line_p2,
h_line_p2,
output_triangle_p2,
)
self.play(FadeIn(grupo_secante))
kwargs = {
"x_min" : 4,
"x_max" : 9,
"fill_opacity" : 0.75,
"stroke_width" : 0.25,
}
self.graph=graph
iteraciones=6
self.rect_list = self.get_riemann_rectangles_list(
graph, iteraciones,start_color=PURPLE,end_color=ORANGE, **kwargs
)
flat_rects = self.get_riemann_rectangles(
self.get_graph(lambda x : 0), dx = 0.5,start_color=invert_color(PURPLE),end_color=invert_color(ORANGE),**kwargs
)
rects = self.rect_list[0]
self.transform_between_riemann_rects(
flat_rects, rects,
replace_mobject_with_target_in_scene = True,
run_time=0.9
)