[C++] 几行代码生成漂亮图片,数学家就是牛!
信息获得处: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 17 return (char)(_sq(cos(atan2(j-512,i-512)/2))*255); 18 #endif 19 20 #ifdef B 21 #define r(n) (rand ()%n) 22 static char c[1024][1024]; 23 return!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 27 float x=0,y=0; 28 int k; 29 for(k=0;k++<256;){ 30 float a=x*x-y*y+ (i-768.0)/512; 31 y=2*x*y+ (j-512.0)/512; 32 x=a; 33 if(x*x+y*y>4)break; 34 } 35 return log (k)*47; 36 #endif 37 38 #ifdef D 39 double a=0,b=0,c,d,n=0; 40 while((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 } 44 return 255*pow ((n-80)/800,3.); 45 #endif 46 47 #ifdef E 48 static double k; 49 k+=rand ()/1./RAND_MAX; 50 int l=k; 51 l%=512; 52 return l>255?511-l:l; 53 #endif 54 55 #ifdef F 56 float s=3./(j+99); 57 float y=(j+sin ((i*i+_sq (j-700)*5)/100./DIM)*35)*s; 58 return (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 */ 78 return 0; 79 } 80 unsigned char GR (int i,int j){ 81 #ifdef A 82 return (char)(_sq (cos (atan2(j-512,i-512)/2-2*acos (-1)/3))*255); 83 #endif 84 85 #ifdef B 86 static char c[1024][1024]; 87 return!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 91 float x=0,y=0; 92 int k; 93 for(k=0;k++<256;){ 94 float a=x*x-y*y+ (i-768.0)/512; 95 y=2*x*y+ (j-512.0)/512; 96 x=a; 97 if(x*x+y*y>4)break; 98 } 99 return log (k)*47; 100 #endif 101 102 #ifdef D 103 double a=0,b=0,c,d,n=0; 104 while((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 } 108 return 255*pow ((n-80)/800,.7); 109 #endif 110 111 #ifdef E 112 static double k; 113 k+=rand ()/1./RAND_MAX; 114 int l=k; 115 l%=512; 116 return l>255?511-l:l; 117 #endif 118 119 #ifdef F 120 float s=3./(j+99); 121 float y=(j+sin ((i*i+_sq (j-700)*5)/100./DIM)*35)*s; 122 return (int(5*((i+DIM)*s+y))%2+int(5*((DIM*2-i)*s+y))%2)*127; 123 #endif 124 /* 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 */ 144 return 0; 145 } 146 unsigned char BL (int i,int j){ 147 #ifdef A 148 return (char)(_sq (cos (atan2(j-512,i-512)/2+2*acos (-1)/3))*255); 149 #endif 150 151 #ifdef B 152 static char c[1024][1024]; 153 return!c[i][j]?c[i][j]=!r(999)?r(256):BL((i+r(2))%1024,(j+r(2))%1024):c[i][j]; 154 #endif 155 156 #ifdef C 157 float x=0,y=0; 158 int k; 159 for(k=0;k++<256;){ 160 float a=x*x-y*y+ (i-768.0)/512; 161 y=2*x*y+ (j-512.0)/512; 162 x=a; 163 if(x*x+y*y>4)break; 164 } 165 return 128-log (k)*23; 166 #endif 167 168 #ifdef D 169 double a=0,b=0,c,d,n=0; 170 while((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 } 174 return 255*pow ((n-80)/800,.5); 175 #endif 176 177 #ifdef E 178 static double k; 179 k+=rand ()/1./RAND_MAX; 180 int l=k; 181 l%=512; 182 return l>255?511-l:l; 183 #endif 184 185 #ifdef F 186 float s=3./(j+99); 187 float y=(j+sin ((i*i+_sq (j-700)*5)/100./DIM)*35)*s; 188 return (int(29*((i+DIM)*s+y))%2+int(29*((DIM*2-i)*s+y))%2)*127; 189 #endif 190 /* 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 */ 218 219 return 0; 220 } 221 222 void pixel_write (int,int); 223 FILE *fp; 224 int main (){ 225 fp = fopen ("MathPic.ppm","wb"); 226 fprintf (fp, "P6\n%d %d\n255\n", DIM, DIM); 227 for(int j=0;j<DIM;j++) 228 for(int i=0;i<DIM;i++) 229 pixel_write (i,j); 230 fclose (fp); 231 return 0; 232 } 233 void pixel_write (int i, int j){ 234 static 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 }