Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from manim import *
- config.frame_width = config.frame_height = 8
- config.pixel_width = config.pixel_height
- length = 5
- unit = 10
- 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 get_rectangle(plane, a, b, c, d, col):
- a = plane.c2p(a[0], a[1], a[2])
- b = plane.c2p(b[0], b[1], b[2])
- c = plane.c2p(c[0], c[1], c[2])
- d = plane.c2p(d[0], d[1], d[2])
- return Polygon(*[a, b, c, d], color = col)
- 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
- )
- return line
- class SweepLine(Scene):
- def construct(self):
- plane = get_numberplane(unit, 1, unit, 1)
- self.play(Create(plane))
- rectangles = [get_rectangle(plane, (1, 1, 0), (1, 3, 0), (8, 3, 0), (8, 1, 0), BLUE),
- get_rectangle(plane, (3, 2, 0), (3, 5, 0), (6, 5, 0), (6, 2, 0), RED),
- get_rectangle(plane, (2, 4, 0), (2, 7, 0), (5, 7, 0), (5, 4, 0), YELLOW),
- get_rectangle(plane, (3, 6, 0), (3, 8, 0), (4, 8, 0), (4, 6, 0), PURPLE),
- get_rectangle(plane, (8, 8, 0), (8, 9, 0), (9, 9, 0), (9, 8, 0), "#6f42f5")]
- self.play(*[Create(rect) for rect in rectangles])
- lines = [[] for i in range(10)]
- cnt = [0 for i in range(10)]
- lines[1].append((1, 3, 1))
- lines[2].append((4, 7, 1))
- lines[3].append((2, 5, 1))
- lines[3].append((6, 8, 1))
- lines[4].append((6, 8, -1))
- lines[5].append((4, 7, -1))
- lines[6].append((2, 5, -1))
- lines[8].append((1, 3, -1))
- lines[8].append((8, 9, 1))
- lines[9].append((8, 9, -1))
- sweep_line = create_line((0, -1), (0, 11), GREEN, plane)
- self.play(Create(sweep_line))
- for i in range(0, 10):
- self.play(sweep_line.animate(run_time = 1).move_to(plane.c2p(i + 0.5, 5, 0)))
- for p in lines[i]:
- for j in range(p[0], p[1]):
- cnt[j] = cnt[j] + p[2]
- sub = []
- for j in range(10):
- if (cnt[j] > 0):
- tmp = get_rectangle(plane, (i, j, 0), (i, j + 1, 0), (i + 1, j + 1, 0), (i + 1, j, 0), "#ffb3b3")
- tmp.set_fill("#ffb3b3", 0.5)
- sub.append(tmp)
- if (len(sub) != 0):
- self.play(*[Create(x) for x in sub], run_time = 1)
- self.remove(*sweep_line)
- self.wait(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement