Advertisement
Mike4235

Sweep-Line_gif2

Oct 17th, 2022 (edited)
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. from manim import *
  2.  
  3. config.frame_width = config.frame_height = 8
  4. config.pixel_width = config.pixel_height
  5.  
  6. length = 5
  7. unit = 10
  8. def get_numberplane(x, gapx, y, gapy):
  9.     return NumberPlane(
  10.         x_range = (0.0, x, gapx),
  11.         y_range = (0.0, y, gapy),
  12.         x_length = length,
  13.         y_length = length,
  14.         background_line_style = {
  15.             "stroke_color": GRAY,
  16.             "stroke_opacity": 0.3
  17.         },
  18.         axis_config = {
  19.             "include_ticks": True,
  20.             "include_numbers": True,
  21.             "font_size" : 18
  22.         }
  23.     )
  24.  
  25. def get_rectangle(plane, a, b, c, d, col):
  26.     a = plane.c2p(a[0], a[1], a[2])
  27.     b = plane.c2p(b[0], b[1], b[2])
  28.     c = plane.c2p(c[0], c[1], c[2])
  29.     d = plane.c2p(d[0], d[1], d[2])
  30.     return Polygon(*[a, b, c, d], color = col)
  31.  
  32. def create_line(start, end, color, plane):
  33.     line = Line(
  34.         start = plane.c2p(start[0], start[1], 0.0),
  35.         end = plane.c2p(end[0], end[1], 0.0),
  36.         color = color
  37.     )
  38.     return line
  39.  
  40. class SweepLine(Scene):
  41.     def construct(self):
  42.         plane = get_numberplane(unit, 1, unit, 1)
  43.         self.play(Create(plane))
  44.        
  45.         rectangles = [get_rectangle(plane, (1, 1, 0), (1, 3, 0), (8, 3, 0), (8, 1, 0), BLUE),
  46.                       get_rectangle(plane, (3, 2, 0), (3, 5, 0), (6, 5, 0), (6, 2, 0), RED),
  47.                       get_rectangle(plane, (2, 4, 0), (2, 7, 0), (5, 7, 0), (5, 4, 0), YELLOW),
  48.                       get_rectangle(plane, (3, 6, 0), (3, 8, 0), (4, 8, 0), (4, 6, 0), PURPLE),
  49.                       get_rectangle(plane, (8, 8, 0), (8, 9, 0), (9, 9, 0), (9, 8, 0), "#6f42f5")]
  50.         self.play(*[Create(rect) for rect in rectangles])
  51.  
  52.         lines = [[] for i in range(10)]
  53.         cnt = [0 for i in range(10)]
  54.         lines[1].append((1, 3, 1))
  55.         lines[2].append((4, 7, 1))
  56.         lines[3].append((2, 5, 1))
  57.         lines[3].append((6, 8, 1))
  58.         lines[4].append((6, 8, -1))
  59.         lines[5].append((4, 7, -1))
  60.         lines[6].append((2, 5, -1))
  61.         lines[8].append((1, 3, -1))
  62.         lines[8].append((8, 9, 1))
  63.         lines[9].append((8, 9, -1))
  64.  
  65.         sweep_line = create_line((0, -1), (0, 11), GREEN, plane)
  66.         self.play(Create(sweep_line))
  67.  
  68.         for i in range(0, 10):
  69.             self.play(sweep_line.animate(run_time = 1).move_to(plane.c2p(i + 0.5, 5, 0)))
  70.  
  71.             for p in lines[i]:
  72.                 for j in range(p[0], p[1]):
  73.                     cnt[j] = cnt[j] + p[2]
  74.  
  75.             sub = []
  76.             for j in range(10):
  77.                 if (cnt[j] > 0):
  78.                     tmp = get_rectangle(plane, (i, j, 0), (i, j + 1, 0), (i + 1, j + 1, 0), (i + 1, j, 0), "#ffb3b3")
  79.                     tmp.set_fill("#ffb3b3", 0.5)
  80.                     sub.append(tmp)
  81.             if (len(sub) != 0):
  82.                 self.play(*[Create(x) for x in sub], run_time = 1)
  83.  
  84.         self.remove(*sweep_line)
  85.         self.wait(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement