Consider a regular triangular area, divide it into four equal triangles of half height and remove the one in the middle. Apply the same operation recursively to each of the three remaining triangles. If we repeated this procedure infinite times, we'd obtain
something with an area of zero. The fractal that evolves this way is called the Sierpinski Triangle. Although its topological dimension is 2, its Hausdorff-Besicovitch dimension is log(3)/log(2)~1.58, a fractional value (that's why it is called a fractal).
By the way, the Hausdorff-Besicovitch dimension of the Norwegian coast is approximately 1.52, its topological dimension being 1.
For this problem, you are to outline the Sierpinski Triangle up to a certain recursion depth, using just ASCII characters. Since the drawing resolution is thus fixed, you'll need to grow the picture appropriately. Draw the smallest triangle (that is not divided
any further) with two slashes, to backslashes and two underscores like this:
/\ /__\
To see how to draw larger triangles, take a look at the sample output.
3 2 1 0
/\
/__\
/\ /\
/__\/__\
/\ /\
/__\ /__\
/\ /\ /\ /\
/__\/__\/__\/__\
/\
/__\
/\ /\
/__\/__\
/\
/__\

#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<string.h>
char sanjiao[2048][2048];
void draw(int i,int j,int n){
if(n==1){sanjiao[i][j]='/';sanjiao[i][j+1]='_';sanjiao[i][j+2]='_';sanjiao[i][j+3]='\\';sanjiao[i-1][j+1]='/';sanjiao[i-1][j+2]='\\';}
else{int dis=(int)pow(2.0,n);
draw(i,j,n-1);
draw(i,j+dis,n-1);
draw(i-dis/2,j+dis/2,n-1);}
}
int main(){
int n;
while(1){
scanf("%d",&n);
if(n==0)break;
for(int i=0;i<2048;i++)
memset(sanjiao[i],' ',sizeof(sanjiao[i]));
int dis=(int)pow(2.0,n);
draw(dis,1,n);
int i,j;
for(i=1;i<=dis;i++){
for(j=1;j<=dis*2;j++)
printf("%c",sanjiao[i][j]);
printf("\n");}
printf("\n");
}
return 0;}
本文介绍如何使用递归算法绘制谢尔宾斯基三角形,并提供了一个使用ASCII字符实现的具体示例。通过调整递归深度,可以生成不同复杂度的分形图案。

1233

被折叠的 条评论
为什么被折叠?



