标签:
转载请标明地址:http://www.cnblogs.com/wangmengmeng/
效果图:
源代码:
自己调试了老半天......orz
1 #include <graphics.h> 2 #include <conio.h> 3 #include <math.h> 4 5 //定义全局变量 6 int rosesize=500; 7 int h=-250; 8 9 //定义结构体 10 struct DOT 11 { 12 double x; 13 double y; 14 double z; 15 double r;//hong 16 double g;//liu 17 //b (蓝) 通过r计算 18 }; 19 20 //计算点 21 bool calc(double a,double b,double c,DOT &d) 22 { 23 double j,n,o,w,z; 24 25 if(c>60){ 26 d.x=sin(a*7)*(13+5/(0.2+pow(b*4,4)))-sin(b)*50; 27 d.y=b*rosesize+50; 28 d.z=625+cos(a*7)*(13+5/(0.2+pow(b*4,4)))+b*400; 29 d.r=a*1-b/2; 30 d.g=a; 31 return true; 32 }//花柄 33 34 double A=a*2-1; 35 double B=b*2-1; 36 if(A*A+B*B<1){ 37 38 if(c>37){ 39 j=(int(c)&1); 40 n=j?6:4; 41 o=0.5/(a+0.01)+cos(b*125)*3-a*300; 42 w=b*h; 43 44 d.x=o*cos(n)+w*sin(n)+j*610-390; 45 d.y=o*sin(n)-w*cos(n)+550-j*350; 46 d.z=1180+cos(B+A)*99-j*300; 47 d.r=0.4-a*0.1+pow(1-B*B,-h*6)*0.15-a*b*0.4+cos(a+b)/5+pow(cos((o*(a+1)+(B>0?w:-w))/25),30)*0.1*(1-B*B); 48 d.g=o/1000+0.7-o*w*0.000003; 49 return true; 50 }//叶 51 52 if(c>32){ 53 c=c*1.16-0.15; 54 o=a*45-20; 55 w=b*b*h; 56 z=o*sin(c)+w*cos(c)+620; 57 58 d.x=o*cos(c)-w*sin(c); 59 d.y=28+cos(B*0.5)*99-b*b*b*60-z/2-h; 60 d.z = z; 61 d.r=(b*b*0.3+pow((1-(A*A)),7)*0.15+0.3)*b; 62 d.g=b*0.7; 63 return true; 64 }//花萼 65 66 //flower 67 o=A*(2-b)*(80-c*2); 68 w=99-cos(A)*120-cos(b)*(-h-c*4.9)+cos(pow(1-b,7))*50+c*2; 69 z=o*sin(c)+w*cos(c)+700; 70 71 d.x=o*cos(c)-w*sin(c); 72 d.y=B*99-cos(pow(b,7))*50-c/3-z/1.35+450; 73 d.z=z; 74 d.r=(1-b/1.2)*0.9+a*0.1; 75 d.g=pow((1-b),20)/4+0.05; 76 return true; 77 } 78 return false; 79 } 80 81 //主函数 82 void main() 83 { 84 //定义变量 85 short *zBuffer; 86 int x,y,z,zBufferIndex; 87 DOT dot; 88 89 //初始化 90 initgraph(640,480); 91 setbkcolor(WHITE); 92 cleardevice(); 93 94 //初始化 zbuffer 95 zBuffer = new short[rosesize*rosesize]; 96 memset(zBuffer,0,sizeof(short)*rosesize*rosesize); 97 98 for(int j=0;j<2000 && !_kbhit();j++){ 99 for(int i=0;i<10000;i++)//减少是否有按键的判断 100 if(calc(double(rand()) / RAND_MAX, double(rand()) / RAND_MAX, rand() % 46 / 0.74, dot)){ 101 z=int(dot.z+0.5); 102 x=int(dot.x*rosesize/z-h+0.5); 103 y=int(dot.y*rosesize/z-h+0.5); 104 if (y >= rosesize) continue; 105 106 zBufferIndex=y*rosesize+x; 107 108 if(!zBuffer[zBufferIndex]||zBuffer[zBufferIndex]>z){ 109 zBuffer[zBufferIndex]=z; 110 111 //画点 112 int r=~int ((dot.r*h)); 113 if(r<0) r=0; 114 if(r>255) r=255; 115 int g=~int ((dot.g*h)); 116 if(g<0) g=0; 117 if(g>255) g=255; 118 int b=~int ((dot.r*dot.r*-80)); 119 if(b<0) b=0; 120 if(b>255) b=255; 121 putpixel(x+50,y-20,RGB(r,g,b)); 122 } 123 } 124 Sleep(1); 125 }//按任意键退出 126 //退出 127 delete[]zBuffer; 128 _getch(); 129 closegraph(); 130 }
来自:C语言程序设计经典236例
标签:
原文地址:http://www.cnblogs.com/wangmengmeng/p/4716124.html