这个程序生成的图形很好看(以我的眼光),希望可以给你带来一些灵感:
这个程序的 源码如下:在Win-tc下调试通过。
#include "stdio.h"
#include "conio.h"
#include "graphics.h"
main()
{
int orgData[100][100]; //这是初始值存放区
int resData[100][100]; //这是对初始值进行图像运算后的结果存放区
int nCount, nRows, nCols, i, j, times;
int nShowTimes = 600;
int GraphDriver = DETECT, GraphMode;
for (i = 0; i < 100; i++)
{
for(j = 0; j < 100; j++)
{
orgData[i][j] = 1; //初始区值初始化为1(值只有0,1两种)
}
}
initgraph(&GraphDriver, &GraphMode, "");
setcolor(WHITE);
rectangle(270, 190, 370, 290);
for(times = 0; times < nShowTimes; times++)
{
for(nRows = 1; nRows < 99; nRows++)
{
for(nCols = 1; nCols < 99; nCols++)
{
nCount = orgData[nRows][nCols-1]
+ orgData[nRows][nCols+1]
+ orgData[nRows - 1][nCols -1]
+ orgData[nRows - 1][nCols + 1]
+ orgData[nRows + 1][nCols - 1]
+ orgData[nRows + 1][nCols + 1]
+ orgData[nRows - 1][nCols]
+ orgData[nRows + 1][nCols]; //将像素相邻的八个元素的值相加
switch(nCount)
{
case 3: //是否有且仅有3个相邻点的值是1
{
resData[nRows][nCols] = 1; //此位置结果值为1
putpixel(270 + nRows, 190 +nCols, BLACK); //此位置的像素设为黑色
}
break;
case 2://是否有且仅有2个相邻元素的值是1
{
resData[nRows][nCols] = orgData[nRows][nCols]; //此位置结果值为原始值
}
break;
default://其它情况
{
resData[nRows][nCols] = 0; //结果值是0
putpixel(270 + nRows, 190 + nCols, WHITE); //此位置像素为白色
}
break;
}
}
}
for ( i = 1; i < 99; i++)
{
for( j = 1; j < 99; j++)
{
orgData[i][j] = resData[i][j];
}
} //将运算的结果赋给初始值开始新一轮计算
}
getch();
}