Sierpinski三角形

转载请标明地址:http://www.cnblogs.com/wangmengmeng/

效果图:

 

通项公式:An=3的N-1次方

源代码:

 1 #include <graphics.h>
 2 #include <conio.h>
 3 #include <time.h>
 4 
 5 void main()
 6 {
 7     srand((unsigned)time(NULL));                        // 设置随机种子
 8     POINT P[3] = {{320, 50}, {120, 400}, {520, 400}};    // 设定三角形的三个顶点
 9     POINT p = {rand() % 640, rand() % 480};                // 随机产生当前点
10 
11     // 初始化图形模式
12     initgraph(640, 480);
13 
14     setbkcolor(WHITE);
15     cleardevice();
16 
17     // 绘制三万个点
18     int n;
19     for(int i = 0; i <= 30000; i++)
20     {
21         n = rand() % 3;
22         p.x = (p.x + P[n].x) / 2;
23         p.y = (p.y + P[n].y) / 2;
24         putpixel(p.x, p.y, GREEN);
25     }
26 
27     // 按任意键退出
28     getch();
29     closegraph();
30 }
原文地址:https://www.cnblogs.com/wangmengmeng/p/4715524.html