from manimlib.imports import *
import numpy as np
class Sierpinski(MovingCameraScene):
def construct(self):
def b_afin(Cx,Cy):
b_afin = np.array([
[1,0,-Cx],
[0,1,-Cy],
[0,0,1],
])
return b_afin
def a_afin(Cx,Cy):
a_afin = np.array([
[1,0,Cx],
[0,1,Cy],
[0,0,1]
])
return a_afin
def rotate_matrix(rad):
r_matrix = np.array([
[np.cos(rad),-1*np.sin(rad),0],
[np.sin(rad),np.cos(rad),0],
[0,0,1],
])
return r_matrix
def create_tri(a,b,c):
length = b[0]-a[0]
c = [0,0,0]
c[0] = a[0] + length / 2
c[1] = a[1] + np.sqrt(3)*length / 2
c = np.array([c[0],c[1],1])
n1 = (a + c)/2
n2 = (b + a)/2
n3 = (c + b )/2
line1 = Line(n1,n2,stroke_width =1).set_color(BLUE_A)
line2 = Line(n2,n3,stroke_width =1).set_color(BLUE_A)
line3 = Line(n3,n1,stroke_width =1).set_color(BLUE_A)
length = np.linalg.norm(n2-a)
length2 = np.linalg.norm(b-n2)
length3 = np.linalg.norm(n3-n1)
dots = [a,n2,n1,n2,b,n3,n1,n3,c]
if length < 0.125/2:
return
else:
lines = VGroup(line1,line2,line3)
self.add(lines)
self.wait(0.1)
create_tri(a,n2,n1)
create_tri(n2,b,n3)
create_tri(n1,n3,c)
return dots
a = np.array([-3,-2,1])
Ax = a[0]
Ay = a[1]
b = [0,0,1]
b[0] = a[0] + 6
b[1] = a[1]
b[2] = 1
b = np.array([b[0],b[1],b[2]])
c = a_afin(Ax,Ay)@(rotate_matrix(np.pi/3)@(b_afin(Ax,Ay)@b))
tri = Polygon(a,b,c,stroke_width =1)
self.play(ShowCreation(tri))
create_tri(a,b,c)
self.wait(1)