Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from manim import *
- # Some default configs of manim, adjusting aspect ratio and render dimensions
- config.frame_width = config.frame_height = 8
- config.pixel_width = config.pixel_height
- length = 5
- flag = 1
- def get_numberplane(x, gapx, y, gapy):
- return NumberPlane(
- x_range = (0.0, x, gapx),
- y_range = (0.0, y, gapy),
- x_length = length,
- y_length = length,
- background_line_style = {
- "stroke_color": GRAY,
- "stroke_opacity": 0.3
- },
- axis_config = {
- "include_ticks": True,
- "include_numbers": True,
- "font_size" : 18
- }
- )
- def create_line(start, end, color, plane):
- line = Line(
- start = plane.c2p(start[0], start[1], 0.0),
- end = plane.c2p(end[0], end[1], 0.0),
- color = color
- )
- line.stroke_width = length / 2
- return line
- def create_cnt(labels, plane):
- count = NumberLine(
- x_range = [0, 9, 1],
- length = plane.y_length / 10 * 9,
- stroke_width = 0,
- include_numbers = False,
- include_ticks = False,
- font_size = 24,
- rotation = 90 * DEGREES,
- ).next_to(plane, RIGHT)
- count.set_y(count.get_y() + 1 / length)
- count.add_labels(labels)
- return count
- def create_text(text, plane):
- return Text(text, font = "Roboto", font_size = 36).next_to(plane, DOWN)
- class PiLine:
- def __init__(self, x, type, l, r):
- self.type = type
- self.x = x
- self.l = l
- self.r = r
- class SweepLine(Scene):
- def construct(self):
- # Initial plane
- prev_plane = get_numberplane(1.0, 0.1, 1.0, 0.1)
- blue_input = [[0.36295, 0.557494, 0.184032], [0.0479258, 0.214097, 0.508344], [0.234284, 0.969098, 0.739363]]
- prev_blue_lines = [create_line((sx, y), (ex, y), BLUE, prev_plane) for sx, ex, y in blue_input]
- red_input = [[0.499323, 0.739797, 0.138495], [0.829265, 0.22551, 0.290582], [0.791082, 0.069214, 0.450979]]
- prev_red_lines = [create_line((x, sy), (x, ey), RED, prev_plane) for sy, ey, x in red_input]
- self.play(Create(prev_plane))
- text = create_text("Mặt phẳng ban đầu", prev_plane)
- self.add(text)
- self.play(*[Create(line) for line in prev_blue_lines], *[Create(line) for line in prev_red_lines])
- self.wait(2)
- self.remove(*text)
- # Coordinate compression plane
- plane = get_numberplane(1.0, 0.1, 10, 1)
- blue_input = [[0.36295, 0.557494, 2], [0.0479258, 0.214097, 5], [0.234284, 0.969098, 6]]
- blue_lines = [create_line((sx, y), (ex, y), BLUE, plane) for sx, ex, y in blue_input]
- red_input = [[4, 7, 0.138495], [9, 3, 0.290582], [8, 1, 0.450979]]
- red_lines = [create_line((x, sy), (x, ey), RED, plane) for sy, ey, x in red_input]
- self.play(Transform(prev_plane, plane), *[Transform(prev_blue_lines[i], blue_lines[i]) for i in range(3)], *[Transform(prev_red_lines[i], red_lines[i]) for i in range(3)])
- text = create_text("Mặt phẳng sau khi nén tung độ", plane)
- self.add(text)
- self.wait(2)
- self.remove(*text)
- # Sweep Line
- sweep_line = create_line((0, -1), (0, 11), GREEN, plane)
- self.play(Create(sweep_line))
- # Cnt array
- labels = {}
- for i in range(10):
- labels[i] = 0
- count = create_cnt(labels, plane)
- self.play(Create(count))
- lines = [PiLine(0.0, 2, 0, 0),
- PiLine(0.0479258, 1, 5, 0),
- PiLine(0.138495, 0, 4, 7),
- PiLine(0.214097, -1, 5, 0),
- PiLine(0.234284, 1, 6, 0),
- PiLine(0.290582, 0, 3, 9),
- PiLine(0.36295, 1, 2, 0),
- PiLine(0.450979, 0, 1, 8),
- PiLine(0.557494, -1, 2, 0),
- PiLine(0.969098, -1, 6, 0),
- PiLine(1.0, 2, 0, 0)
- ]
- answer = "Đáp án = 0"
- s_answer = [" + 1", " + 1", " + 2"]
- intersect = [[5], [6], [2, 6]]
- text = create_text(answer, plane).move_to(plane.c2p(0.2, -1.5, 0))
- self.play(Write(text))
- cur = 0
- br = [text]
- for i in range(1, 11):
- self.play(sweep_line.animate(run_time = 1).move_to(plane.c2p(lines[i].x, 5, 0)))
- if lines[i].type == 1 or lines[i].type == -1:
- labels[lines[i].l] += lines[i].type
- tmp = create_cnt(labels, plane)
- self.play(Transform(count, tmp))
- elif lines[i].type == 0:
- to_be_added = []
- to_be_deleted = []
- for j in range(lines[i].l, lines[i].r + 1):
- tmp = Circle().surround(count.labels[j], buffer_factor = 2.0)
- to_be_added.append(tmp)
- to_be_deleted.append(tmp)
- for y in intersect[cur]:
- tmp = plane.c2p(lines[i].x, y, 0.0)
- d = Dot(point = tmp, radius = 0.04)
- to_be_added.append(d)
- self.play(*[Create(c) for c in to_be_added])
- self.wait(2)
- self.remove(*[c for c in to_be_deleted])
- tmp = Text(s_answer[cur], font = "Roboto", font_size = 36).next_to(br[-1], RIGHT)
- answer += s_answer[cur]
- br.append(tmp)
- cur = cur + 1
- self.play(Write(tmp))
- self.remove(*sweep_line)
- self.remove(*[s for s in br])
- self.play(Write(create_text("Đáp án = 4", plane)))
- self.wait(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement