soi-manim/scanline/billboards_of_ny.py

54 lines
1.4 KiB
Python
Raw Normal View History

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, 1, 2.3, 3.5), (1.5, 2, 9, 2), (2, 1.5, 4, 3), (3.7, 2.1, 3, 1.4), (4.5, 0, 2.8, 4.2), (6.3, 1.1, 2.8, 3.6)]]
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(0.5)
self.wait(2)