Skip to content

Instantly share code, notes, and snippets.

@uwezi
Created March 19, 2023 11:36
Show Gist options
  • Save uwezi/722b0d161bc96a8b39ff1aba3c9eff7c to your computer and use it in GitHub Desktop.
Save uwezi/722b0d161bc96a8b39ff1aba3c9eff7c to your computer and use it in GitHub Desktop.
[Hilbert curve] How to draw a Hilbert curve in Manim #manim #vmobject #math #hilbert
# original code @vasek.rozhon, modified by @uwezi
#https://discord.com/channels/581738731934056449/1086715297928581161/1086722509417762836
from manim import *
class Hilbert(Scene):
def construct(self):
from hilbertcurve.hilbertcurve import HilbertCurve
side_length = 4
bounding_square = (
Square(color=GRAY).scale_to_fit_width(
side_length).shift(0)
)
def create_curve(iter):
distances = list(range(4**iter))
points = HilbertCurve(iter, 2).points_from_distances(distances)
points = [[p[0], p[1], 0] for p in points]
'''
curve = Polygon(*points, *reversed(points), color=RED).scale_to_fit_width(
side_length * (1 - 2 ** (-iter))
).move_to(bounding_square.get_center())
'''
curve = VMobject()
curve.set_points_as_corners(points)
curve.set_color(RED).scale_to_fit_width(
side_length * (1 - 2 ** (-iter))
).move_to(bounding_square.get_center())
return curve
self.play(
FadeIn(bounding_square),
)
self.wait()
curve1 = create_curve(1)
self.play(
FadeIn(curve1)
)
self.wait()
for i in range(2, 5):
curve2 = create_curve(i)
self.play(
Transform(curve1, curve2)
)
self.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment