Created
April 6, 2022 08:56
-
-
Save behackl/ee653371fb4903e904625bf2f5a23978 to your computer and use it in GitHub Desktop.
Implementation of an unfolding Dragon curve animation using Manim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from manim import * | |
class Dragon(MovingCameraScene): | |
def construct(self): | |
dragon_curve = VMobject(stroke_color=GOLD) | |
dragon_curve_points = [LEFT, RIGHT] | |
dragon_curve.set_points_as_corners(dragon_curve_points) | |
dragon_curve.corners = dragon_curve_points | |
self.add(dragon_curve) | |
dragon_curve.add_updater( | |
lambda mobject: mobject.set_style(stroke_width=self.camera.frame.width / 10), | |
) | |
dragon_curve.update() | |
self.wait() | |
def rotate_half_points(points, alpha): | |
static_part = points[:len(points)//2] | |
about_point = points[len(points)//2] | |
mat = rotation_matrix(-PI/2 * alpha, OUT) | |
rotated_part = [ | |
np.dot((point - about_point), mat.T) + about_point | |
for point in reversed(static_part) | |
] | |
return static_part + [about_point] + rotated_part | |
def rotate_half_curve(mobject, alpha): | |
corners = mobject.corners | |
new_corners = rotate_half_points(corners, alpha) | |
mobject.set_points_as_corners(new_corners) | |
return mobject | |
for it in range(10): | |
rotated_curve = VMobject().set_points_as_corners(rotate_half_points(dragon_curve.corners, 1)) | |
self.play( | |
UpdateFromAlphaFunc(dragon_curve, rotate_half_curve), | |
self.camera.auto_zoom(rotated_curve, margin=1) | |
) | |
current_corners = rotate_half_points(dragon_curve.corners, 1) | |
current_corners = current_corners + current_corners[-1::-1] | |
dragon_curve.set_points_as_corners(current_corners) | |
dragon_curve.corners = current_corners | |
self.wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment