Skip to content

Instantly share code, notes, and snippets.

@kebairia
Last active December 22, 2021 16:43
Show Gist options
  • Save kebairia/667e25cbc13be91bd8c88886707c60b8 to your computer and use it in GitHub Desktop.
Save kebairia/667e25cbc13be91bd8c88886707c60b8 to your computer and use it in GitHub Desktop.
playing with manim (movements and rotations)
from manim import *
import random
class Samples(Scene):
_bg = "#282828"
font_size = 30
font_size_samples = 10
label_font_size = 20
squares = 10
rows = squares/10
def construct(self):
def _sample_sq(value):
border = Square(side_length=1.1, stroke_width=1)
sq = Square(side_length=0.6, stroke_width=1, fill_color=BLUE_E, fill_opacity=0.8)
plane = NumberPlane(
x_range=[-1, 1, 0.1], y_range=[-1, 1, 0.1],
x_length=1.1, y_length=1.1,
x_axis_config={"stroke_width": 0.5},
y_axis_config={"stroke_width": 0.5},
background_line_style={
"stroke_color": GRAY,
"stroke_width": 0.2,
"stroke_opacity": 0.6,
"fill_color": GRAY_E,
"fill_opacity": 1.0
}
)
value.next_to(border, UP, SMALL_BUFF)
plane.move_to(border.get_center())
sq.move_to(border.get_center())
return VGroup(border, value, plane, sq)
self.camera.background_color = self._bg
rotations = Text("Rotations", font_size=self.font_size)
rotations.to_edge(UP, MED_SMALL_BUFF)
self.play(Write(rotations))
rotation_values = random.sample(range(1, 11), self.squares)
samples = VGroup(*[_sample_sq(MathTex(
r"\frac {\pi} {%s}" % (rotation_values[i]),
font_size=self.label_font_size)) for i in range(self.squares)])
samples.arrange_in_grid(rows=int(self.rows), buff=0.28, center=False).next_to(rotations, DOWN, buff=MED_SMALL_BUFF)
for i in range(len(samples)):
self.play(FadeIn(samples[i], scale=0.8), run_time=0.6)
self.play(Rotate(samples[i][-1], PI/rotation_values[i]))
movements = Text("Movements", font_size=self.font_size).next_to(samples, DOWN, MED_SMALL_BUFF)
mv_border = Square(side_length=4, stroke_width=1)
mv_sq = Square(side_length=0.5, stroke_width=1, fill_color=BLUE_E, fill_opacity=0.8)
mv_plane = NumberPlane(
x_range=[-4, 4, 0.5], y_range=[-4, 4, 0.5],
x_length=4, y_length=4,
x_axis_config={"stroke_width": 0.5},
y_axis_config={"stroke_width": 0.5},
background_line_style={
"stroke_color": GRAY,
"stroke_width": 0.2,
"stroke_opacity": 0.6,
"fill_color": GRAY_E,
"fill_opacity": 1.0
}
)
VGroup(mv_border, mv_sq, mv_plane).next_to(movements, DOWN, MED_SMALL_BUFF)
self.play(Write(movements))
self.play(Write(mv_border), Write(mv_sq), DrawBorderThenFill(mv_plane))
for mov in [UP, UR, RIGHT, DR, DOWN, DL, LEFT, UL]:
self.play(mv_sq.animate.shift(mov), rate_func=there_and_back, run_time=1.5)
self.wait(0.2)
self.wait(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment