add initial scanline visualization for the billboards task

This commit is contained in:
Bibin Muttappillil 2022-10-29 15:27:29 +02:00
parent 036680d610
commit 072a1501b3
1 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,51 @@
from manim import *
field = Rectangle(width=10.0, height=5.0)
class Billboard(Rectangle):
def __init__(self, x, y, w, h):
Rectangle.__init__(self, width=w, height=h)
self.x = x
self.y = y
self.w = w
self.h = h
self.align_to(field, UL)
self.shift(x*RIGHT + y*DOWN)
class Scanline(VGroup):
def __init__(self):
self.line = Line(field.get_top(), field.get_bottom(), color=RED).align_to(field, LEFT)
self.tops = dict()
VGroup.__init__(self)
self.add(self.line)
def insert(self, y):
self.tops[y] = Dot().move_to(self.line.get_top()).shift(y*DOWN)
self.add(self.tops[y])
def delete(self, y):
self.remove(self.tops[y])
del self.tops[y]
class Main(Scene):
def construct(self):
boards = [Billboard(x, y, w, h) for x, y, w, h in [(1, 0, 3, 3), (2, 2, 5, 2)]]
self.add(*boards)
poi = sorted([(b.x, 1, i) for i, b in enumerate(boards)] + [(b.x + b.w, -1, i) for i, b in enumerate(boards)])
scan = Scanline()
for x, left, i in poi:
self.play(scan.animate.move_to(field.get_left() + x*RIGHT))
if left == 1:
scan.insert(boards[i].y)
else:
scan.delete(boards[i].y)
self.wait(2)