Advertisement
Mike4235

Sweep-Line_gif1

Oct 17th, 2022
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.63 KB | None | 0 0
  1. from manim import *
  2.  
  3. # Some default configs of manim, adjusting aspect ratio and render dimensions
  4. config.frame_width = config.frame_height = 8
  5. config.pixel_width = config.pixel_height
  6.  
  7. length = 5
  8. flag = 1
  9.  
  10. def get_numberplane(x, gapx, y, gapy):
  11.     return NumberPlane(
  12.         x_range = (0.0, x, gapx),
  13.         y_range = (0.0, y, gapy),
  14.         x_length = length,
  15.         y_length = length,
  16.         background_line_style = {
  17.             "stroke_color": GRAY,
  18.             "stroke_opacity": 0.3
  19.         },
  20.         axis_config = {
  21.             "include_ticks": True,
  22.             "include_numbers": True,
  23.             "font_size" : 18
  24.         }
  25.     )
  26.  
  27. def create_line(start, end, color, plane):
  28.     line = Line(
  29.         start = plane.c2p(start[0], start[1], 0.0),
  30.         end = plane.c2p(end[0], end[1], 0.0),
  31.         color = color
  32.     )
  33.     line.stroke_width = length / 2
  34.  
  35.     return line
  36.  
  37. def create_cnt(labels, plane):
  38.     count = NumberLine(
  39.         x_range = [0, 9, 1],
  40.         length = plane.y_length / 10 * 9,
  41.         stroke_width = 0,
  42.         include_numbers = False,
  43.         include_ticks = False,
  44.         font_size = 24,
  45.         rotation = 90 * DEGREES,
  46.     ).next_to(plane, RIGHT)
  47.     count.set_y(count.get_y() + 1 / length)
  48.     count.add_labels(labels)
  49.  
  50.     return count
  51.  
  52. def create_text(text, plane):
  53.     return Text(text, font = "Roboto", font_size = 36).next_to(plane, DOWN)
  54.  
  55. class PiLine:
  56.     def __init__(self, x, type, l, r):
  57.         self.type = type
  58.         self.x = x
  59.         self.l = l
  60.         self.r = r
  61.  
  62. class SweepLine(Scene):
  63.     def construct(self):
  64.         # Initial plane
  65.         prev_plane = get_numberplane(1.0, 0.1, 1.0, 0.1)
  66.        
  67.         blue_input = [[0.36295, 0.557494, 0.184032], [0.0479258, 0.214097, 0.508344], [0.234284, 0.969098, 0.739363]]
  68.         prev_blue_lines = [create_line((sx, y), (ex, y), BLUE, prev_plane) for sx, ex, y in blue_input]
  69.  
  70.         red_input = [[0.499323, 0.739797, 0.138495], [0.829265, 0.22551, 0.290582], [0.791082, 0.069214, 0.450979]]
  71.         prev_red_lines = [create_line((x, sy), (x, ey), RED, prev_plane) for sy, ey, x in red_input]
  72.  
  73.         self.play(Create(prev_plane))
  74.         text = create_text("Mặt phẳng ban đầu", prev_plane)
  75.         self.add(text)
  76.         self.play(*[Create(line) for line in prev_blue_lines], *[Create(line) for line in prev_red_lines])
  77.         self.wait(2)
  78.         self.remove(*text)
  79.         # Coordinate compression plane
  80.         plane = get_numberplane(1.0, 0.1, 10, 1)
  81.  
  82.         blue_input = [[0.36295, 0.557494, 2], [0.0479258, 0.214097, 5], [0.234284, 0.969098, 6]]
  83.         blue_lines = [create_line((sx, y), (ex, y), BLUE, plane) for sx, ex, y in blue_input]
  84.  
  85.         red_input = [[4, 7, 0.138495], [9, 3, 0.290582], [8, 1, 0.450979]]
  86.         red_lines = [create_line((x, sy), (x, ey), RED, plane) for sy, ey, x in red_input]
  87.  
  88.         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)])
  89.         text = create_text("Mặt phẳng sau khi nén tung độ", plane)
  90.         self.add(text)
  91.         self.wait(2)
  92.         self.remove(*text)
  93.         # Sweep Line
  94.         sweep_line = create_line((0, -1), (0, 11), GREEN, plane)
  95.         self.play(Create(sweep_line))
  96.  
  97.         # Cnt array
  98.         labels = {}
  99.         for i in range(10):
  100.             labels[i] = 0
  101.         count = create_cnt(labels, plane)
  102.         self.play(Create(count))
  103.  
  104.         lines = [PiLine(0.0, 2, 0, 0),
  105.                  PiLine(0.0479258, 1, 5, 0),
  106.                  PiLine(0.138495, 0, 4, 7),
  107.                  PiLine(0.214097, -1, 5, 0),
  108.                  PiLine(0.234284, 1, 6, 0),
  109.                  PiLine(0.290582, 0, 3, 9),
  110.                  PiLine(0.36295, 1, 2, 0),
  111.                  PiLine(0.450979, 0, 1, 8),
  112.                  PiLine(0.557494, -1, 2, 0),
  113.                  PiLine(0.969098, -1, 6, 0),
  114.                  PiLine(1.0, 2, 0, 0)
  115.                 ]
  116.         answer = "Đáp án = 0"
  117.         s_answer = [" + 1", " + 1", " + 2"]
  118.         intersect = [[5], [6], [2, 6]]
  119.         text = create_text(answer, plane).move_to(plane.c2p(0.2, -1.5, 0))
  120.         self.play(Write(text))
  121.  
  122.         cur = 0
  123.         br = [text]
  124.         for i in range(1, 11):
  125.             self.play(sweep_line.animate(run_time = 1).move_to(plane.c2p(lines[i].x, 5, 0)))
  126.  
  127.             if lines[i].type == 1 or lines[i].type == -1:
  128.                 labels[lines[i].l] += lines[i].type
  129.                 tmp = create_cnt(labels, plane)
  130.                 self.play(Transform(count, tmp))
  131.             elif lines[i].type == 0:
  132.                 to_be_added = []
  133.                 to_be_deleted = []
  134.                 for j in range(lines[i].l, lines[i].r + 1):
  135.                     tmp = Circle().surround(count.labels[j], buffer_factor = 2.0)
  136.                     to_be_added.append(tmp)
  137.                     to_be_deleted.append(tmp)
  138.  
  139.                 for y in intersect[cur]:
  140.                     tmp = plane.c2p(lines[i].x, y, 0.0)
  141.                     d = Dot(point = tmp, radius = 0.04)
  142.                     to_be_added.append(d)
  143.  
  144.                 self.play(*[Create(c) for c in to_be_added])
  145.                 self.wait(2)
  146.                 self.remove(*[c for c in to_be_deleted])
  147.  
  148.                 tmp = Text(s_answer[cur], font = "Roboto", font_size = 36).next_to(br[-1], RIGHT)
  149.                 answer += s_answer[cur]
  150.                 br.append(tmp)
  151.  
  152.                 cur = cur + 1
  153.                 self.play(Write(tmp))
  154.  
  155.         self.remove(*sweep_line)
  156.         self.remove(*[s for s in br])
  157.         self.play(Write(create_text("Đáp án = 4", plane)))
  158.         self.wait(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement