- /
-
Sierpinski triangle
on 26 Oct 2024
- 19
- 90
- 0
- 0
- 1106
Cite your audio source here (if applicable):
drawframe(30);
Write your drawframe function below
function drawframe(i)
if i<=49
N=floor(i/5)+1;
else
N=20-floor(i/5); %Reverse order to make a continuous cycle
end
Trigs=NaN(N,1);
Trigs(1)=1;
if N>1
for k=2:N
Trigs(k)=Trigs(k-1)+3^(k-1);
end
end
VerticesX=NaN(Trigs(N),3);
VerticesY=NaN(Trigs(N),3);
VerticesX(1,:)=[0,0.5,1];
VerticesY(1,:)=[0,1,0];
if N>1
for k=2:N
for u=1:(Trigs(k)-Trigs(k-1))/3
[VerticesX(Trigs(k-1)+1+3*(u-1):Trigs(k-1)+3*u,:),VerticesY(Trigs(k-1)+1+3*(u-1):Trigs(k-1)+3*u,:)]=midPoints(VerticesX(Trigs(k-1)+u-3^(k-2),:),VerticesY(Trigs(k-1)+u-3^(k-2),:));
end
end
end
clf
if N==1
patch(VerticesX(1,:)',VerticesY(1,:)','red','EdgeColor','none');
else
patch(VerticesX(Trigs(N-1)+1:Trigs(N),:)',VerticesY(Trigs(N-1)+1:Trigs(N),:)','red','EdgeColor','none');
end
end
%Calculate new vertices
function [TrianglesX,TrianglesY]=midPoints(VertX,VertY)
TrianglesX(1,:)=[VertX(1),(VertX(1)+VertX(2))/2,(VertX(1)+VertX(3))/2];
TrianglesX(2,:)=[(VertX(1)+VertX(2))/2,VertX(2),(VertX(2)+VertX(3))/2];
TrianglesX(3,:)=[(VertX(1)+VertX(3))/2,(VertX(2)+VertX(3))/2,VertX(3)];
TrianglesY(1,:)=[VertY(1),(VertY(1)+VertY(2))/2,(VertY(1)+VertY(3))/2];
TrianglesY(2,:)=[(VertY(1)+VertY(2))/2,VertY(2),(VertY(2)+VertY(3))/2];
TrianglesY(3,:)=[(VertY(1)+VertY(3))/2,(VertY(2)+VertY(3))/2,VertY(3)];
end
Movie
Audio
This submission does not have audio.