[C++] 几行代码生成漂亮图片,数学家就是牛!
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[C++] 几行代码生成漂亮图片,数学家就是牛!,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5310字,纯文字阅读大概需要8分钟。
内容图文
信息获得处:http://news.cnblogs.com/n/501488/
分形:http://baike.baidu.com/subview/83243/11213590.htm?fr=aladdin
代码:[采用宏定义的来选择绘制不同的图,所以把最后两个给注释掉了,因为宏冲突,想自己试试可以单独编译运行]
PS:生成的图片是ppm格式的要用格式工厂之类的软件进行转换,也可以自己在程序里直接把图片生成bmp或者其他格式的~
1 // NOTE: compile with g++ filename.cpp -std=c++11 2 #include <iostream> 3 #include <cmath> 4 #include <cstdlib> 5#define DIM 1024 6#define DM1 (DIM-1) 7#define _sq(x) ((x)*(x)) // square 8#define _cb(x) abs((x)*(x)*(x)) // absolute value of cube 9#define _cr(x) (unsigned char)(pow ((x),1.0/3.0)) // cube root 10#define F 11 12 unsigned char GR (int,int); 13 unsigned char BL (int,int); 14 15 unsigned char RD (int i,int j){ 16 #ifdef A 17return (char)(_sq(cos(atan2(j-512,i-512)/2))*255); 18#endif 19 20 #ifdef B 21#define r(n) (rand ()%n) 22staticchar c[1024][1024]; 23return!c[i][j]?c[i][j]=!r(999)?r(256):RD ((i+r(2))%1024,(j+r(2))%1024):c[i][j]; 24#endif 25 26 #ifdef C 27float x=0,y=0; 28int k; 29for(k=0;k++<256;){ 30float a=x*x-y*y+ (i-768.0)/512; 31 y=2*x*y+ (j-512.0)/512; 32 x=a; 33if(x*x+y*y>4)break; 34 } 35return log (k)*47; 36#endif 37 38 #ifdef D 39double a=0,b=0,c,d,n=0; 40while((c=a*a)+(d=b*b)<4&&n++<880){ 41 b=2*a*b+j*8e-9-.645411; 42 a=c-d+i*8e-9+.356888; 43 } 44return255*pow ((n-80)/800,3.); 45#endif 46 47 #ifdef E 48staticdouble k; 49 k+=rand ()/1./RAND_MAX; 50int l=k; 51 l%=512; 52return l>255?511-l:l; 53#endif 54 55 #ifdef F 56float s=3./(j+99); 57float y=(j+sin ((i*i+_sq (j-700)*5)/100./DIM)*35)*s; 58return (int((i+DIM)*s+y)%2+int((DIM*2-i)*s+y)%2)*127; 59#endif 60/* 61 #ifdef G 62 #define D DIM 63 #define M m[(x+D+ (d==0)-(d==2))%D][(y+D+ (d==1)-(d==3))%D] 64 #define R rand ()%D 65 #define B m[x][y] 66 return(i+j)?256-(BL (i,j))/2:0; 67 #endif 68*/ 69/* 70 #ifdef H 71 #define A float a=0,b,k,r,x 72 #define B int e,o 73 #define C (x) x>255?255:x 74 #define R return #define D DIM 75 R BL (i,j)*(D-i)/D; 76 #endif 77*/ 78return0; 79} 80 unsigned char GR (int i,int j){ 81 #ifdef A 82return (char)(_sq (cos (atan2(j-512,i-512)/2-2*acos (-1)/3))*255); 83#endif 84 85 #ifdef B 86staticchar c[1024][1024]; 87return!c[i][j]?c[i][j]=!r(999)?r(256):GR ((i+r(2))%1024,(j+r(2))%1024):c[i][j]; 88#endif 89 90 #ifdef C 91float x=0,y=0; 92int k; 93for(k=0;k++<256;){ 94float a=x*x-y*y+ (i-768.0)/512; 95 y=2*x*y+ (j-512.0)/512; 96 x=a; 97if(x*x+y*y>4)break; 98 } 99return log (k)*47; 100#endif101102 #ifdef D 103double a=0,b=0,c,d,n=0; 104while((c=a*a)+(d=b*b)<4&&n++<880){ 105 b=2*a*b+j*8e-9-.645411; 106 a=c-d+i*8e-9+.356888; 107 } 108return255*pow ((n-80)/800,.7); 109#endif110111 #ifdef E 112staticdouble k; 113 k+=rand ()/1./RAND_MAX; 114int l=k; 115 l%=512; 116return l>255?511-l:l; 117#endif118119 #ifdef F 120float s=3./(j+99); 121float y=(j+sin ((i*i+_sq (j-700)*5)/100./DIM)*35)*s; 122return (int(5*((i+DIM)*s+y))%2+int(5*((DIM*2-i)*s+y))%2)*127; 123#endif124/*125 #ifdef G 126 #define A 127 static int m[D][D],e,x,y,d,c[4],f,n; 128 if (i+j<1){ 129 for (d=D*D;d;d--){ 130 m[d%D][d/D]=d%6?0:rand ()%2000?1:255; 131 } 132 for (n=1 return RD (i,j); 133 #endif 134*/135/*136 #ifdef H 137 #define E DM1 138 #define F static float 139 #define G for ( 140 #define H r=a*1.6/D+2.4;x=1.0001*b/D 141 R BL (i,j)*(D-j/2)/D; 142 #endif 143*/144return0; 145} 146 unsigned char BL (int i,int j){ 147 #ifdef A 148return (char)(_sq (cos (atan2(j-512,i-512)/2+2*acos (-1)/3))*255); 149#endif150151 #ifdef B 152staticchar c[1024][1024]; 153return!c[i][j]?c[i][j]=!r(999)?r(256):BL((i+r(2))%1024,(j+r(2))%1024):c[i][j]; 154#endif155156 #ifdef C 157float x=0,y=0; 158int k; 159for(k=0;k++<256;){ 160float a=x*x-y*y+ (i-768.0)/512; 161 y=2*x*y+ (j-512.0)/512; 162 x=a; 163if(x*x+y*y>4)break; 164 } 165return128-log (k)*23; 166#endif167168 #ifdef D 169double a=0,b=0,c,d,n=0; 170while((c=a*a)+(d=b*b)<4&&n++<880){ 171 b=2*a*b+j*8e-9-.645411; 172 a=c-d+i*8e-9+.356888; 173 } 174return255*pow ((n-80)/800,.5); 175#endif176177 #ifdef E 178staticdouble k; 179 k+=rand ()/1./RAND_MAX; 180int l=k; 181 l%=512; 182return l>255?511-l:l; 183#endif184185 #ifdef F 186float s=3./(j+99); 187float y=(j+sin ((i*i+_sq (j-700)*5)/100./DIM)*35)*s; 188return (int(29*((i+DIM)*s+y))%2+int(29*((DIM*2-i)*s+y))%2)*127; 189#endif190/*191 #ifdef G 192 A;n;n++){ 193 x=R;y=R; 194 if(B==1){ 195 f=1; 196 for(d=0;d<4;d++){ 197 c[d]=M; 198 f=f<c[d]?c[d]:f; 199 } 200 if(f>2){B=f-1;} 201 else{ 202 ++e%=4; 203 d=e; 204 if(!c[e]){ 205 B=0;M=1; 206 }}}}} 207 return m[i][j]; 208 #endif 209*/210/*211 #ifdef H 212 F c[D][D]; 213 if(i+j<1){A;B;G;a<D;a+=0.1){G b=0;b<D;b++){H;G k=0;k<D;k++){x=r*x*(1-x); 214 if(k>D/2){e=a;o=(E*x);c[e][o]+=0.01;}}}}} 215 R C (c[j][i])*i/D; 216 #endif 217*/218219return0; 220} 221222void pixel_write (int,int); 223 FILE *fp; 224int main (){ 225 fp = fopen ("MathPic.ppm","wb"); 226 fprintf (fp, "P6\n%d %d\n255\n", DIM, DIM); 227for(int j=0;j<DIM;j++) 228for(int i=0;i<DIM;i++) 229 pixel_write (i,j); 230 fclose (fp); 231return0; 232} 233void pixel_write (int i, int j){ 234static unsigned char color[3]; 235 color[0] = RD (i,j)&255; 236 color[1] = GR (i,j)&255; 237 color[2] = BL (i,j)&255; 238 fwrite (color, 1, 3, fp); 239 }
原文:http://www.cnblogs.com/zjutlitao/p/3915860.html
内容总结
以上是互联网集市为您收集整理的[C++] 几行代码生成漂亮图片,数学家就是牛!全部内容,希望文章能够帮你解决[C++] 几行代码生成漂亮图片,数学家就是牛!所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。