概要
wslでmanimやってみた。
練習問題やってみた。
練習問題
ルービックキューブを描け。
成果物
サンプルコード
from manim import *
class RubiksCube(VGroup):
def __init__(self, **kwargs):
self.colors = ["#FFD500", "#C41E3A", "#009E60", "#FF5800", "#0051BA", "#FFFFFF"]
vectors = [OUT, RIGHT, UP, LEFT, DOWN, IN]
faces = [self.create_face(color, vector) for color, vector in zip(self.colors, vectors)]
VGroup.__init__(self, *faces, **kwargs)
self.set_shade_in_3d(True)
def create_face(self, color, vector):
squares = VGroup(*[self.create_square(color) for x in range(9)])
squares.arrange_in_grid(3, 3, buff = 0)
squares.set_width(2)
squares.move_to(OUT, OUT)
squares.apply_matrix(z_to_vector(vector))
return squares
def create_square(self, color):
square = Square(stroke_width = 3, stroke_color = BLACK, fill_color = color, fill_opacity = 1, side_length = 1, )
square.flip()
return square
def get_face(self, vect):
self.sort(lambda p: np.dot(p, vect))
return self[-(12 + 9):]
class test(ThreeDScene):
def construct(self):
cube = RubiksCube()
cube.set_fill(opacity = 0.8)
cube.set_stroke(width = 1)
axes = ThreeDAxes()
self.add(axes, cube)
self.move_camera(phi = 70 * DEGREES, theta = -140 * DEGREES, )
self.begin_ambient_camera_rotation(rate = 0.02)
self.wait(2)
self.play(Rotate(cube, TAU / 4, RIGHT, run_time = 3))
self.wait(2)
self.play(Rotate(cube, TAU / 4, UP, run_time = 3))
self.wait(2)
self.play(Rotate(cube, -TAU / 3, np.ones(3), run_time = 3))
self.wait(2)
以上。