From 072a1501b3e874b7ba1dacde61d3384ee52940bc Mon Sep 17 00:00:00 2001 From: bibin Date: Sat, 29 Oct 2022 15:27:29 +0200 Subject: [PATCH] add initial scanline visualization for the billboards task --- scanline/billboards_of_ny.py | 51 ++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 scanline/billboards_of_ny.py diff --git a/scanline/billboards_of_ny.py b/scanline/billboards_of_ny.py new file mode 100644 index 0000000..6a49d58 --- /dev/null +++ b/scanline/billboards_of_ny.py @@ -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)