[C++] 几行代码生成漂亮图片,数学家就是牛!
2014-08-15 23:41
387 查看
信息获得处:http://news.cnblogs.com/n/501488/
分形:http://baike.baidu.com/subview/83243/11213590.htm?fr=aladdin
代码:[采用宏定义的来选择绘制不同的图,所以把最后两个给注释掉了,因为宏冲突,想自己试试可以单独编译运行]
PS:生成的图片是ppm格式的要用格式工厂之类的软件进行转换,也可以自己在程序里直接把图片生成bmp或者其他格式的~
分形:http://baike.baidu.com/subview/83243/11213590.htm?fr=aladdin
代码:[采用宏定义的来选择绘制不同的图,所以把最后两个给注释掉了,因为宏冲突,想自己试试可以单独编译运行]
PS:生成的图片是ppm格式的要用格式工厂之类的软件进行转换,也可以自己在程序里直接把图片生成bmp或者其他格式的~
// NOTE: compile with g++ filename.cpp -std=c++11 #include <iostream> #include <cmath> #include <cstdlib> #define DIM 1024 #define DM1 (DIM-1) #define _sq(x) ((x)*(x)) // square #define _cb(x) abs((x)*(x)*(x)) // absolute value of cube #define _cr(x) (unsigned char)(pow ((x),1.0/3.0)) // cube root #define F unsigned char GR (int,int); unsigned char BL (int,int); unsigned char RD (int i,int j){ #ifdef A return (char)(_sq(cos(atan2(j-512,i-512)/2))*255); #endif #ifdef B #define r(n) (rand ()%n) static char c[1024][1024]; return!c[i][j]?c[i][j]=!r(999)?r(256):RD ((i+r(2))%1024,(j+r(2))%1024):c[i][j]; #endif #ifdef C float x=0,y=0; int k; for(k=0;k++<256;){ float a=x*x-y*y+ (i-768.0)/512; y=2*x*y+ (j-512.0)/512; x=a; if(x*x+y*y>4)break; } return log (k)*47; #endif #ifdef D double a=0,b=0,c,d,n=0; while((c=a*a)+(d=b*b)<4&&n++<880){ b=2*a*b+j*8e-9-.645411; a=c-d+i*8e-9+.356888; } return 255*pow ((n-80)/800,3.); #endif #ifdef E static double k; k+=rand ()/1./RAND_MAX; int l=k; l%=512; return l>255?511-l:l; #endif #ifdef F float s=3./(j+99); float y=(j+sin ((i*i+_sq (j-700)*5)/100./DIM)*35)*s; return (int((i+DIM)*s+y)%2+int((DIM*2-i)*s+y)%2)*127; #endif /* #ifdef G #define D DIM #define M m[(x+D+ (d==0)-(d==2))%D][(y+D+ (d==1)-(d==3))%D] #define R rand ()%D #define B m[x][y] return(i+j)?256-(BL (i,j))/2:0; #endif */ /* #ifdef H #define A float a=0,b,k,r,x #define B int e,o #define C (x) x>255?255:x #define R return #define D DIM R BL (i,j)*(D-i)/D; #endif */ return 0; } unsigned char GR (int i,int j){ #ifdef A return (char)(_sq (cos (atan2(j-512,i-512)/2-2*acos (-1)/3))*255); #endif #ifdef B static char c[1024][1024]; return!c[i][j]?c[i][j]=!r(999)?r(256):GR ((i+r(2))%1024,(j+r(2))%1024):c[i][j]; #endif #ifdef C float x=0,y=0; int k; for(k=0;k++<256;){ float a=x*x-y*y+ (i-768.0)/512; y=2*x*y+ (j-512.0)/512; x=a; if(x*x+y*y>4)break; } return log (k)*47; #endif #ifdef D double a=0,b=0,c,d,n=0; while((c=a*a)+(d=b*b)<4&&n++<880){ b=2*a*b+j*8e-9-.645411; a=c-d+i*8e-9+.356888; } return 255*pow ((n-80)/800,.7); #endif #ifdef E static double k; k+=rand ()/1./RAND_MAX; int l=k; l%=512; return l>255?511-l:l; #endif #ifdef F float s=3./(j+99); float y=(j+sin ((i*i+_sq (j-700)*5)/100./DIM)*35)*s; return (int(5*((i+DIM)*s+y))%2+int(5*((DIM*2-i)*s+y))%2)*127; #endif /* #ifdef G #define A static int m[D][D],e,x,y,d,c[4],f,n; if (i+j<1){ for (d=D*D;d;d--){ m[d%D][d/D]=d%6?0:rand ()%2000?1:255; } for (n=1 return RD (i,j); #endif */ /* #ifdef H #define E DM1 #define F static float #define G for ( #define H r=a*1.6/D+2.4;x=1.0001*b/D R BL (i,j)*(D-j/2)/D; #endif */ return 0; } unsigned char BL (int i,int j){ #ifdef A return (char)(_sq (cos (atan2(j-512,i-512)/2+2*acos (-1)/3))*255); #endif #ifdef B static char c[1024][1024]; return!c[i][j]?c[i][j]=!r(999)?r(256):BL((i+r(2))%1024,(j+r(2))%1024):c[i][j]; #endif #ifdef C float x=0,y=0; int k; for(k=0;k++<256;){ float a=x*x-y*y+ (i-768.0)/512; y=2*x*y+ (j-512.0)/512; x=a; if(x*x+y*y>4)break; } return 128-log (k)*23; #endif #ifdef D double a=0,b=0,c,d,n=0; while((c=a*a)+(d=b*b)<4&&n++<880){ b=2*a*b+j*8e-9-.645411; a=c-d+i*8e-9+.356888; } return 255*pow ((n-80)/800,.5); #endif #ifdef E static double k; k+=rand ()/1./RAND_MAX; int l=k; l%=512; return l>255?511-l:l; #endif #ifdef F float s=3./(j+99); float y=(j+sin ((i*i+_sq (j-700)*5)/100./DIM)*35)*s; return (int(29*((i+DIM)*s+y))%2+int(29*((DIM*2-i)*s+y))%2)*127; #endif /* #ifdef G A;n;n++){ x=R;y=R; if(B==1){ f=1; for(d=0;d<4;d++){ c[d]=M; f=f<c[d]?c[d]:f; } if(f>2){B=f-1;} else{ ++e%=4; d=e; if(!c[e]){ B=0;M=1; }}}}} return m[i][j]; #endif */ /* #ifdef H F c[D][D]; 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); if(k>D/2){e=a;o=(E*x);c[e][o]+=0.01;}}}}} R C (c[j][i])*i/D; #endif */ return 0; } void pixel_write (int,int); FILE *fp; int main (){ fp = fopen ("MathPic.ppm","wb"); fprintf (fp, "P6\n%d %d\n255\n", DIM, DIM); for(int j=0;j<DIM;j++) for(int i=0;i<DIM;i++) pixel_write (i,j); fclose (fp); return 0; } void pixel_write (int i, int j){ static unsigned char color[3]; color[0] = RD (i,j)&255; color[1] = GR (i,j)&255; color[2] = BL (i,j)&255; fwrite (color, 1, 3, fp); }
相关文章推荐
- 几行代码生成漂亮图片,数学家就是牛!
- PCL1.8创建深度图并保存成png格式图片代码(生成深度图的原理一般就是透视投影或者正交投影)
- 一段将多张图片生成AVI视频的C++代码
- C++生成Bmp图片底层代码
- 上传图片并生成缩略图的ASP.NET2.0代码!
- ASP.NET中常用功能代码总结——生成验证图片
- ASP.NET中常用功能代码总结(2)——生成验证图片
- jsp中生成图片缩略图的代码
- ASP.NET中常用功能代码总结(2)——生成验证图片
- asp.net(c#)上传图片并生成缩略图的代码
- 特漂亮的JS图片排列旋转效果代码
- 在ASP.NET中上传图片并生成缩略图的C#代码
- php生成SessionID和图片校验码的思路和实现代码
- C++的GUID的生成代码
- 生成登录页面随机数图片的代码范例
- PHP生成IP地址图片的代码
- 让C++代码与C#代码一起生成一个单一的Assembly
- 生成图片代码
- C#的图片处理类,生成高质量缩略图,代码已测试过,效果很好
- 几行代码搞定一棵漂亮的树!