I used recursion to generate fractal and got the desired result but I think my code can be better
I started using MATLAB a few days ago and since than trying and learning new things.
Now this code generates the fractal exactly as I wanted but I feel like it can a little bit neater.
Any suggestions are welcome (even off topic but related to how to use MATLAB) as I am a beginner in MATLAB, they will definitely help me in future.
hold on
x=[0 1 1/2]; %Coordinates of a equilateral triangle
fill(x,y,'k');
axis equal
axis off
abc(list)
function abc(listnew)
if abs(listnew(1,1)-listnew(1,2))>0.002
listold=listnew;
t=size(listold,2)/3;
for j=1:t
listnew(1,(j-1)*3+1:j*3)=[(listold(1,(j-1)*3+2)+listold(1,(j-1)*3+1))/2 (listold(1,(j-1)*3+3)+listold(1,(j-1)*3+2))/2 (listold(1,(j-1)*3+1)+listold(1,(j-1)*3+3))/2];
listnew(2,(j-1)*3+1:j*3)=[listold(2,(j-1)*3+1) (listold(2,(j-1)*3+3)+listold(2,(j-1)*3+2))/2 (listold(2,(j-1)*3+1)+listold(2,(j-1)*3+3))/2];
end
pause(2)
tri(listnew);
k=size(listnew,2)/3;
for m=1:k
f=(m-1)*(3^2);
l(1,(f+1):(f+3))=[listold(1,(m-1)*3+1) listnew(1,(m-1)*3+1) listnew(1,(m-1)*3+3)];
l(2,(f+1):(f+3))=[listold(2,(m-1)*3+1) listnew(2,(m-1)*3+1) listnew(2,(m-1)*3+3)];
l(1,(f+4):(f+6))=[listnew(1,(m-1)*3+1) listold(1,(m-1)*3+2) listnew(1,(m-1)*3+2)];
l(2,(f+4):(f+6))=[listnew(2,(m-1)*3+1) listold(2,(m-1)*3+2) listnew(2,(m-1)*3+2)];
l(1,(f+7):(f+9))=[listnew(1,(m-1)*3+3) listnew(1,(m-1)*3+2) listold(1,(m-1)*3+3)];
l(2,(f+7):(f+9))=[listnew(2,(m-1)*3+3) listnew(2,(m-1)*3+2) listold(2,(m-1)*3+3)];
end
abc(l);
end
end
function tri(list)
for i =1:(size(list,2)/3)
fill(list(1,(i-1)*3+1:i*3),list(2,(i-1)*3+1:i*3),'w');
end
end