Recipes for manim
from fastcore.basics import patch
from manim import *
import numpy as np
from manim.mobject.geometry import *
from manim.mobject import geometry
@patch
def intersect(self: Circle, obj: Circle, steps=500, tolerance=0.01) -> ArcPolygon:
self_points = [self.point_from_proportion(step) for step in np.linspace(0,1,steps)]
obj_points = [obj.point_from_proportion(step) for step in np.linspace(0,1,steps)]
intersections = []
for a in self_points:
for b in obj_points:
if np.linalg.norm(a-b) < tolerance:
avg = (a+b)/2
intersections.append(avg)
return ArcPolygon(*intersections, radius=1, fill_opacity=0.5, color=PURPLE_A)
%%manim IntersectionDemo
class IntersectionDemo(Scene):
def construct(self):
A = Circle(RED, fill_opacity=0.5, radius=2).shift(LEFT*2)
B = Circle(BLUE, fill_opacity=0.5).shift(RIGHT*2)
# intersection = Cutout(A, B)
self.play(ShowCreation(A), ShowCreation(B))
self.wait(1)
self.play(A.animate.shift(RIGHT), B.animate.shift(LEFT))
intersection2 = Cutout(A,B, color=GREEN)
self.play(ShowCreation(intersection2))
%%manim IntersectionDemo
class IntersectionDemo(Scene):
def construct(self):
A = Circle(RED, fill_opacity=0.5, radius=2).shift(LEFT*2)
B = Circle(BLUE, fill_opacity=0.5).shift(RIGHT*2)
intersection = A.intersect(B)
self.play(ShowCreation(A), ShowCreation(B), ShowCreation(intersection))
self.wait(1)
A2 = Circle(RED, fill_opacity=0.5, radius=2).shift(LEFT*0.5)
B2 = Circle(BLUE, fill_opacity=0.5).shift(RIGHT*0.5)
intersection2 = A2.intersect(B2)
self.play(Transform(A,A2), Transform(B,B2), Transform(intersection, intersection2))
self.wait(1)
%%manim IntersectionDemo
class IntersectionDemo(Scene):
def get_intersections(self, A, B, tolerance=0.01, steps=500):
A_points = [A.point_from_proportion(step) for step in np.linspace(0,1,steps)]
B_points = [B.point_from_proportion(step) for step in np.linspace(0,1,steps)]
intersections = []
for a in A_points:
for b in B_points:
if np.linalg.norm(a-b) < tolerance:
avg = (a+b)/2
intersections.append(avg)
return intersections
def intersect(self, A, B):
intersections = self.get_intersections(A,B)
return ArcPolygon(*intersections, radius=1, fill_opacity=0.5, color=PURPLE_A)
def construct(self):
with register_font('JetBrainsMono-Regular.ttf'):
text = Text('Hello World', font='JetBrainsMono').shift(UP*2)
self.play(Write(text))
A = Circle(RED, fill_opacity=0.5).shift(LEFT*2)
B = Circle(BLUE, fill_opacity=0.5).shift(RIGHT*2)
intersection = self.intersect(A,B)
self.play(ShowCreation(A), ShowCreation(B), ShowCreation(intersection))
self.wait(1)
A2 = Circle(RED, fill_opacity=0.5).shift(LEFT*0.5)
B2 = Circle(BLUE, fill_opacity=0.5).shift(RIGHT*0.5)
intersection2 = self.intersect(A2,B2)
self.play(Transform(A,A2), Transform(B,B2), Transform(intersection, intersection2))
self.wait(1)
%%manim IntersectionDemo -qm -v WARNING
class IntersectionDemo(Scene):
def construct(self):
with register_font('JetBrainsMono-Regular.ttf'):
text = Text('Hello World', font='JetBrainsMono').shift(UP*2)
self.play(Write(text))
A = Circle(RED, fill_opacity=0.5).shift(LEFT*2)
B = Circle(BLUE, fill_opacity=0.5).shift(RIGHT*2)
self.play(ShowCreation(A), ShowCreation(B))
self.play(A.animate.shift(RIGHT*1.5), B.animate.shift(LEFT*1.5))
self.wait(1)
%%manim IntersectionDemo
class IntersectionDemo(Scene):
def construct(self):
s1 = Square().scale(2.5)
s2 = Triangle().shift(DOWN + RIGHT).scale(0.5)
s3 = Square().shift(UP + RIGHT).scale(0.5)
s4 = RegularPolygon(5).shift(DOWN + LEFT).scale(0.5)
s5 = RegularPolygon(6).shift(UP + LEFT).scale(0.5)
c = Cutout(s1, s2, s3, s4, s5, fill_opacity=1, color=BLUE, stroke_color=RED)
self.play(Write(c), run_time=4)
%%manim IntersectionDemo -qm -v WARNING
class IntersectionDemo(Scene):
def construct(self):
s = Vector()
self.play(Write(s))
%%manim IntersectionDemo -qm -v WARNING
class IntersectionDemo(Scene):
def construct(self):
s = Vector()
self.play(Write(s))