mirror of
https://github.com/3b1b/manim.git
synced 2025-08-02 02:35:22 +08:00
Merge pull request #217 from 3b1b/eop
More functionality for GraphScene
This commit is contained in:
@ -1,243 +0,0 @@
|
||||
from big_ol_pile_of_manim_imports import *
|
||||
from eop.pascal import *
|
||||
|
||||
|
||||
WIDTH = 12
|
||||
HEIGHT = 1
|
||||
GRADE_COLOR_1 = COLOR_HEADS = RED
|
||||
GRADE_COLOR_2 = COLOR_TAILS = BLUE
|
||||
NB_ROWS = 6
|
||||
|
||||
|
||||
class Coin(Circle):
|
||||
CONFIG = {
|
||||
"radius": 0.2,
|
||||
"stroke_width": 3,
|
||||
"stroke_color": WHITE,
|
||||
"fill_opacity": 1,
|
||||
"symbol": "\euro",
|
||||
}
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
Circle.__init__(self,**kwargs)
|
||||
self.symbol_mob = TextMobject(self.symbol, stroke_color = self.stroke_color)
|
||||
self.symbol_mob.scale_to_fit_height(0.5*self.get_height()).move_to(self)
|
||||
self.add(self.symbol_mob)
|
||||
|
||||
|
||||
class Heads(Coin):
|
||||
CONFIG = {
|
||||
"fill_color": COLOR_HEADS,
|
||||
"symbol": "H",
|
||||
}
|
||||
|
||||
|
||||
class Tails(Coin):
|
||||
CONFIG = {
|
||||
"fill_color": COLOR_TAILS,
|
||||
"symbol": "T",
|
||||
}
|
||||
|
||||
class CoinStack(VGroup):
|
||||
CONFIG = {
|
||||
"spacing": 0.1,
|
||||
"size": 5,
|
||||
"face": Heads,
|
||||
}
|
||||
|
||||
def generate_points(self):
|
||||
for n in range(self.size):
|
||||
coin = self.face()
|
||||
coin.shift(n * self.spacing * RIGHT)
|
||||
self.add(coin)
|
||||
|
||||
class HeadsStack(CoinStack):
|
||||
CONFIG = { "face": Heads }
|
||||
|
||||
class TailsStack(CoinStack):
|
||||
CONFIG = { "face": Tails }
|
||||
|
||||
class TallyStack(VGroup):
|
||||
|
||||
def __init__(self,h,t,**kwargs):
|
||||
self.nb_heads = h
|
||||
self.nb_tails = t
|
||||
VGroup.__init__(self,**kwargs)
|
||||
|
||||
def generate_points(self):
|
||||
stack1 = HeadsStack(size = self.nb_heads)
|
||||
stack2 = TailsStack(size = self.nb_tails)
|
||||
stack2.next_to(stack1, RIGHT, buff = SMALL_BUFF)
|
||||
self.add(stack1, stack2)
|
||||
|
||||
|
||||
class AreaSplittingScene(Scene):
|
||||
|
||||
def create_rect_row(self,n):
|
||||
rects_group = VGroup()
|
||||
for k in range(n+1):
|
||||
proportion = float(choose(n,k)) / 2**n
|
||||
new_rect = Rectangle(
|
||||
width = proportion * WIDTH,
|
||||
height = HEIGHT,
|
||||
fill_color = graded_color(n,k),
|
||||
fill_opacity = 1
|
||||
)
|
||||
new_rect.next_to(rects_group,RIGHT,buff = 0)
|
||||
rects_group.add(new_rect)
|
||||
return rects_group
|
||||
|
||||
def split_rect_row(self,rect_row):
|
||||
|
||||
split_row = VGroup()
|
||||
for rect in rect_row.submobjects:
|
||||
half = rect.copy().stretch_in_place(0.5,0)
|
||||
left_half = half.next_to(rect.get_center(),LEFT,buff = 0)
|
||||
right_half = half.copy().next_to(rect.get_center(),RIGHT,buff = 0)
|
||||
split_row.add(left_half, right_half)
|
||||
return split_row
|
||||
|
||||
|
||||
def rect_center(self,n,i,j):
|
||||
if n < 0:
|
||||
raise Exception("wrong indices " + str(n) + ", " + str(i) + ", " + str(j))
|
||||
if i < 0 or i > n:
|
||||
raise Exception("wrong indices " + str(n) + ", " + str(i) + ", " + str(j))
|
||||
if j > choose(n,i) or j < 0:
|
||||
raise Exception("wrong indices " + str(n) + ", " + str(i) + ", " + str(j))
|
||||
|
||||
rect = self.brick_array[n][i]
|
||||
width = rect.get_width()
|
||||
left_x = rect.get_center()[0] - width/2
|
||||
spacing = width / choose(n,i)
|
||||
x = left_x + (j+0.5) * spacing
|
||||
return np.array([x,rect.get_center()[1], rect.get_center()[2]])
|
||||
|
||||
def construct(self):
|
||||
|
||||
# Draw the bricks
|
||||
|
||||
brick_wall = VGroup()
|
||||
rect_row = self.create_rect_row(0)
|
||||
rect_row.move_to(3.5*UP + 0*HEIGHT*DOWN)
|
||||
self.add(rect_row)
|
||||
brick_wall.add(rect_row)
|
||||
self.brick_array = [[rect_row.submobjects[0]]]
|
||||
|
||||
for n in range(NB_ROWS):
|
||||
# copy and shift
|
||||
new_rect_row = rect_row.copy()
|
||||
self.add(new_rect_row)
|
||||
self.play(new_rect_row.shift,HEIGHT * DOWN)
|
||||
self.wait()
|
||||
|
||||
#split
|
||||
split_row = self.split_rect_row(new_rect_row)
|
||||
self.play(FadeIn(split_row))
|
||||
self.remove(new_rect_row)
|
||||
self.wait()
|
||||
|
||||
# merge
|
||||
rect_row = self.create_rect_row(n+1)
|
||||
rect_row.move_to(3.5*UP + (n+1)*HEIGHT*DOWN)
|
||||
self.play(FadeIn(rect_row))
|
||||
brick_wall.add(rect_row)
|
||||
self.remove(split_row)
|
||||
self.wait()
|
||||
|
||||
# add to brick dict
|
||||
rect_array = []
|
||||
for rect in rect_row.submobjects:
|
||||
rect_array.append(rect)
|
||||
|
||||
self.brick_array.append(rect_array)
|
||||
|
||||
|
||||
self.play(
|
||||
brick_wall.set_fill, {"opacity" : 0.2}
|
||||
)
|
||||
|
||||
|
||||
# Draw the branches
|
||||
|
||||
for (n, rect_row_array) in enumerate(self.brick_array):
|
||||
for (i, rect) in enumerate(rect_row_array):
|
||||
pos = rect.get_center()
|
||||
tally = TallyStack(n - i, i)
|
||||
tally.move_to(pos)
|
||||
|
||||
|
||||
# from the left
|
||||
lines = VGroup()
|
||||
|
||||
if i > 0:
|
||||
for j in range(choose(n-1,i-1)):
|
||||
start_pos = self.rect_center(n-1,i-1,j)
|
||||
end_pos = self.rect_center(n,i,j)
|
||||
lines.add(Line(start_pos,end_pos, stroke_color = GRADE_COLOR_2))
|
||||
self.play(
|
||||
LaggedStart(ShowCreation, lines))
|
||||
|
||||
# from the right
|
||||
lines = VGroup()
|
||||
|
||||
if i < n:
|
||||
for j in range(choose(n-1,i)):
|
||||
start_pos = self.rect_center(n-1,i,j)
|
||||
if i != 0:
|
||||
end_pos = self.rect_center(n,i,choose(n-1,i-1) + j)
|
||||
else:
|
||||
end_pos = self.rect_center(n,i,j)
|
||||
|
||||
lines.add(Line(start_pos,end_pos, stroke_color = GRADE_COLOR_1))
|
||||
self.play(
|
||||
LaggedStart(ShowCreation, lines))
|
||||
|
||||
|
||||
|
||||
#self.play(FadeIn(tally))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
self.wait()
|
||||
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -18,7 +18,11 @@ class Histogram(VMobject):
|
||||
"y_scale" : 1.0,
|
||||
"x_labels" : "auto", # widths, mids, auto, none, [...]
|
||||
"y_labels" : "auto", # auto, none, [...]
|
||||
"x_min" : 0
|
||||
"y_label_position" : "top", # "center"
|
||||
"x_min" : 0,
|
||||
"bar_stroke_width" : 5,
|
||||
"outline_stroke_width" : 0,
|
||||
"stroke_color" : WHITE
|
||||
}
|
||||
|
||||
def __init__(self, x_values, y_values, mode = "widths", **kwargs):
|
||||
@ -119,7 +123,13 @@ class Histogram(VMobject):
|
||||
bar = Rectangle(
|
||||
width = self.widths_scaled[i],
|
||||
height = self.y_values_scaled[i],
|
||||
stroke_width = self.bar_stroke_width,
|
||||
stroke_color = self.stroke_color,
|
||||
)
|
||||
if bar.height == 0:
|
||||
bar.height = 0.01
|
||||
bar.generate_points()
|
||||
|
||||
t = float(x - self.x_min)/(self.x_max - self.x_min)
|
||||
bar_color = interpolate_color(
|
||||
self.start_color,
|
||||
@ -127,7 +137,6 @@ class Histogram(VMobject):
|
||||
t
|
||||
)
|
||||
bar.set_fill(color = bar_color, opacity = 1)
|
||||
bar.set_stroke(width = 0)
|
||||
bar.next_to(previous_bar,RIGHT,buff = 0, aligned_edge = DOWN)
|
||||
|
||||
self.bars.add(bar)
|
||||
@ -137,7 +146,12 @@ class Histogram(VMobject):
|
||||
self.x_labels_group.add(x_label)
|
||||
|
||||
y_label = TextMobject(self.y_labels[i])
|
||||
if self.y_label_position == "top":
|
||||
y_label.next_to(bar, UP)
|
||||
elif self.y_label_position == "center":
|
||||
y_label.move_to(bar)
|
||||
else:
|
||||
raise Exception("y_label_position must be top or center")
|
||||
self.y_labels_group.add(y_label)
|
||||
|
||||
if i == 0:
|
||||
@ -155,11 +169,12 @@ class Histogram(VMobject):
|
||||
# lower left
|
||||
outline_points.append(outline_points[0])
|
||||
|
||||
self.outline = Polygon(*outline_points)
|
||||
self.outline.set_stroke(color = WHITE)
|
||||
self.outline = Polygon(*outline_points,
|
||||
stroke_width = self.outline_stroke_width,
|
||||
stroke_color = self.stroke_color)
|
||||
self.add(self.bars, self.x_labels_group, self.y_labels_group, self.outline)
|
||||
|
||||
print self.submobjects
|
||||
self.move_to(ORIGIN)
|
||||
|
||||
def get_lower_left_point(self):
|
||||
return self.bars[0].get_anchors()[-2]
|
||||
@ -274,58 +289,3 @@ class FlashThroughHistogram(Animation):
|
||||
|
||||
|
||||
|
||||
|
||||
class SampleScene(Scene):
|
||||
|
||||
def construct(self):
|
||||
|
||||
x_values = np.array([1,2,3,4,5])
|
||||
y_values = np.array([4,3,5,2,3])
|
||||
|
||||
hist1 = Histogram(
|
||||
x_values = x_values,
|
||||
y_values = y_values,
|
||||
x_scale = 0.5,
|
||||
y_scale = 0.5,
|
||||
).shift(1*DOWN)
|
||||
self.add(hist1)
|
||||
self.wait()
|
||||
|
||||
y_values2 = np.array([3,8,7,15,5])
|
||||
|
||||
hist2 = Histogram(
|
||||
x_values = x_values,
|
||||
y_values = y_values2,
|
||||
x_scale = 0.5,
|
||||
y_scale = 0.5,
|
||||
x_labels = text_range(1,6,1),
|
||||
)
|
||||
|
||||
v1 = hist1.get_lower_left_point()
|
||||
v2 = hist2.get_lower_left_point()
|
||||
hist2.shift(v1 - v2)
|
||||
|
||||
# self.play(
|
||||
# ReplacementTransform(hist1,hist2)
|
||||
# )
|
||||
|
||||
self.play(
|
||||
FlashThroughHistogram(
|
||||
hist1,
|
||||
direction = "horizontal",
|
||||
mode = "linear",
|
||||
run_time = 10,
|
||||
rate_func = None,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -10,21 +10,6 @@ GRADE_COLOR_1 = RED
|
||||
GRADE_COLOR_2 = BLUE
|
||||
|
||||
|
||||
def rainbow_color(alpha):
|
||||
nb_colors = 100
|
||||
rainbow = color_gradient([RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE], nb_colors)
|
||||
rainbow = np.append(rainbow,PURPLE)
|
||||
index = int(alpha * nb_colors)
|
||||
return rainbow[index]
|
||||
|
||||
def graded_color(n,k):
|
||||
if n != 0:
|
||||
alpha = float(k)/n
|
||||
else:
|
||||
alpha = 0.5
|
||||
color = interpolate_color(GRADE_COLOR_1, GRADE_COLOR_2, alpha)
|
||||
return color
|
||||
|
||||
|
||||
def graded_square(n,k):
|
||||
return Square(
|
||||
|
@ -57,6 +57,11 @@ class GraphScene(Scene):
|
||||
def setup(self):
|
||||
self.default_graph_colors_cycle = it.cycle(self.default_graph_colors)
|
||||
|
||||
self.left_T_label = VGroup()
|
||||
self.left_v_line = VGroup()
|
||||
self.right_T_label = VGroup()
|
||||
self.right_v_line = VGroup()
|
||||
|
||||
def setup_axes(self, animate=False):
|
||||
# TODO, once eoc is done, refactor this to be less redundant.
|
||||
x_num_range = float(self.x_max - self.x_min)
|
||||
@ -291,7 +296,7 @@ class GraphScene(Scene):
|
||||
|
||||
|
||||
def get_area(self, graph, t_min, t_max):
|
||||
numerator = max(t_max - t_min, 0.01)
|
||||
numerator = max(t_max - t_min, 0.0001)
|
||||
dx = float(numerator) / self.num_rects
|
||||
return self.get_riemann_rectangles(
|
||||
graph,
|
||||
@ -439,13 +444,17 @@ class GraphScene(Scene):
|
||||
return group
|
||||
|
||||
|
||||
def add_T_label(self, x_val, color = WHITE, animated = False, **kwargs):
|
||||
def add_T_label(self, x_val, side = RIGHT, label = None, color = WHITE, animated = False, **kwargs):
|
||||
triangle = RegularPolygon(n=3, start_angle = np.pi/2)
|
||||
triangle.scale_to_fit_height(MED_SMALL_BUFF)
|
||||
triangle.move_to(self.coords_to_point(x_val, 0), UP)
|
||||
triangle.set_fill(color, 1)
|
||||
triangle.set_stroke(width = 0)
|
||||
if label == None:
|
||||
T_label = TexMobject(self.variable_point_label, fill_color = color)
|
||||
else:
|
||||
T_label = TexMobject(label, fill_color = color)
|
||||
|
||||
T_label.next_to(triangle, DOWN)
|
||||
v_line = self.get_vertical_line_to_graph(
|
||||
x_val, self.v_graph,
|
||||
@ -459,11 +468,16 @@ class GraphScene(Scene):
|
||||
Write(T_label, run_time = 1),
|
||||
**kwargs
|
||||
)
|
||||
else:
|
||||
self.add(triangle, v_line, T_label)
|
||||
|
||||
self.T_label_group = VGroup(T_label, triangle)
|
||||
if np.all(side == LEFT):
|
||||
self.left_T_label_group = VGroup(T_label, triangle)
|
||||
self.left_v_line = v_line
|
||||
self.add(self.left_T_label_group, self.left_v_line)
|
||||
elif np.all(side == RIGHT):
|
||||
self.right_T_label_group = VGroup(T_label, triangle)
|
||||
self.right_v_line = v_line
|
||||
self.add(self.right_T_label_group, self.right_v_line)
|
||||
|
||||
|
||||
|
||||
|
||||
@ -472,6 +486,7 @@ class GraphScene(Scene):
|
||||
graph,
|
||||
new_t_min,
|
||||
new_t_max,
|
||||
fade_close_to_origin = True,
|
||||
run_time = 1.0
|
||||
):
|
||||
curr_t_min = self.x_axis.point_to_number(self.area.get_left())
|
||||
@ -482,33 +497,39 @@ class GraphScene(Scene):
|
||||
new_t_max = curr_t_max
|
||||
|
||||
group = VGroup(self.area)
|
||||
if hasattr(self, "right_v_line"):
|
||||
group.add(self.left_v_line)
|
||||
group.add(self.left_T_label_group)
|
||||
group.add(self.right_v_line)
|
||||
else:
|
||||
group.add(VGroup())
|
||||
# because update_group expects 3 elements in group
|
||||
if hasattr(self, "T_label_group"):
|
||||
group.add(self.T_label_group)
|
||||
else:
|
||||
group.add(VGroup())
|
||||
group.add(self.right_T_label_group)
|
||||
|
||||
def update_group(group, alpha):
|
||||
area, v_line, T_label = group
|
||||
area, left_v_line, left_T_label, right_v_line, right_T_label = group
|
||||
t_min = interpolate(curr_t_min, new_t_min, alpha)
|
||||
t_max = interpolate(curr_t_max, new_t_max, alpha)
|
||||
new_area = self.get_area(graph,t_min, t_max)
|
||||
new_v_line = self.get_vertical_line_to_graph(
|
||||
|
||||
new_left_v_line = self.get_vertical_line_to_graph(
|
||||
t_min, graph
|
||||
)
|
||||
new_left_v_line.set_color(left_v_line.get_color())
|
||||
left_T_label.move_to(new_left_v_line.get_bottom(), UP)
|
||||
|
||||
new_right_v_line = self.get_vertical_line_to_graph(
|
||||
t_max, graph
|
||||
)
|
||||
new_v_line.set_color(v_line.get_color())
|
||||
T_label.move_to(new_v_line.get_bottom(), UP)
|
||||
new_right_v_line.set_color(right_v_line.get_color())
|
||||
right_T_label.move_to(new_right_v_line.get_bottom(), UP)
|
||||
|
||||
#Fade close to 0
|
||||
if len(T_label) > 0:
|
||||
T_label[0].set_fill(opacity = min(1, t_max))
|
||||
# Fade close to 0
|
||||
if fade_close_to_origin:
|
||||
if len(left_T_label) > 0:
|
||||
left_T_label[0].set_fill(opacity = min(1, np.abs(t_min)))
|
||||
if len(right_T_label) > 0:
|
||||
right_T_label[0].set_fill(opacity = min(1, np.abs(t_max)))
|
||||
|
||||
Transform(area, new_area).update(1)
|
||||
Transform(v_line, new_v_line).update(1)
|
||||
Transform(left_v_line, new_left_v_line).update(1)
|
||||
Transform(right_v_line, new_right_v_line).update(1)
|
||||
return group
|
||||
|
||||
return UpdateFromAlphaFunc(group, update_group, run_time = run_time)
|
||||
|
Reference in New Issue
Block a user