【Matlab代码】Sierpinsk地毯

步骤:
1,将一个大正方形作为最基本的图形;
2,将第一步得到的数组缩小到三分之一;
3,将第二部得到的数组复制八份,平移到相应的位置。

figure%画图
hold on
for k=1:8^n
fill(a(4*k-3) a(4*k-2) a(4*k-1) a(4*k)], [b(4*k-3) b(4*k-2) b(4*k-1) b(4*k),'b']
end
hold off
axis off%不要坐标轴
axis equa%各坐标轴同比例
set(findobj(gcf,'type','patch'),'edgecolor','none')%将线条隐去

步骤:
1,确定这个大正方形的四个角的坐标,用两个数组分别表示横坐标和纵坐标。可以把最左下角的坐标设置为原点。
2,把这个大正方形填充成蓝色。
3,将第一步得到的数组缩小到原来的三分之一;
4,将第三步得到的数组平移到原来大正方形的正中间位置。

function ditan(n)
a=[0 1 1 0];
b=[0 0 1 1];
fill(a,b,'b')%将正方形填充蓝色
for k=1:n%迭代n次
for m=1:3^(k-1)%按行
for p=1:3^(k-1)%按列
fill(1/3^k+1/3^(k-1)*(p-1)+a/3^k,1/3^k+1/3^k+1/3^(k-1)*(m-1)+b/3^k,'w')
end
end
end
hold off
axis off%不要坐标轴
axis equal%各坐标轴同比例
set(findobj(gef,'type','patch'),'edgecolor','none')%将线条隐去

猜你喜欢

转载自blog.csdn.net/qq_41985559/article/details/109113098