1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <math.h> 5 #include <complex.h> 6 #include <stdbool.h> 7 8 #define XDIM 1536 //此项必须为8的倍数,否则有BUG 9 #define YDIM 1536 10 11 #define NUM 1000 //迭代次数 12 13 // z = z*z + c 14 15 bool tof_complex(int i, int j) //复数版本 (1536*1536->1000(1 core):15.128s) 16 { 17 int n = NUM; 18 //坐标转换 19 double complex c = (i-0.75*XDIM)/XDIM*2+(j-0.5*YDIM)*2/YDIM*I; 20 double complex z = 0; 21 while(n>0){ 22 z = z*z+c; 23 if(cabs(z)>2) 24 return false; 25 n--; 26 } 27 return true; 28 } 29 30 struct COMPLEX 31 { 32 double re; 33 double im; 34 }; 35 36 bool tof_real(int i,int j) //纯实数版本 (1536*1536->1000(1 core):4.991s) 37 { 38 int n = NUM; 39 struct COMPLEX z = {0,0}; 40 struct COMPLEX c = { 41 (i-0.75*XDIM)/XDIM*2, 42 (j-0.5*YDIM)*2/YDIM 43 }; 44 while(n>0){ 45 double z_re_bak = z.re; 46 z.re = z.re*z.re-z.im*z.im+c.re; 47 z.im = 2*z_re_bak*z.im+c.im; 48 if(z.re>2||z.im>2) 49 return false; 50 n--; 51 } 52 return true; 53 } 54 55 void writeBit(bool bit, FILE * fp) 56 { 57 static int bitNum = 0; 58 static unsigned char Byte = 0; 59 if(bitNum==8) 60 { 61 fputc(Byte, fp); 62 Byte = 0; 63 bitNum = 0; 64 } 65 Byte ^= bit<<(7-bitNum); 66 bitNum++; 67 } 68 69 int main() 70 { 71 FILE *fp; 72 fp = fopen("picture.pbm","wb"); 73 fprintf(fp, "P4 %d %d\n", XDIM, YDIM); 74 for(int j=0; j<YDIM; j++) 75 for(int i=0; i<XDIM; i++) 76 writeBit(tof_complex(i, j), fp); 77 fclose(fp); 78 return 0; 79 }
DEMO
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <math.h> 5 #include <complex.h> 6 #include <stdbool.h> 7 8 #define XDIM 1024 9 #define YDIM 1024 10 11 #define NUM 512 //迭代次数 12 13 // z = z*z + c 14 15 bool tof_complex(int i, int j) 16 { 17 double complex c = (i-0.75*XDIM)/XDIM*2+(j-0.5*YDIM)*2/YDIM*I; 18 double complex z = 0; 19 for(int t=1;t<=NUM;t++){ 20 z = z*z+c; 21 if(cabs(z)>2) 22 return t; 23 } 24 return 0; 25 } 26 27 struct COMPLEX 28 { 29 double re; 30 double im; 31 }; 32 33 int tof_real(int i, int j) 34 { 35 struct COMPLEX z = {0,0}; 36 struct COMPLEX c = { 37 (i-0.75*XDIM)/XDIM*2, 38 (j-0.5*YDIM)*2/YDIM 39 }; 40 for(int t=1;t<=NUM;t++){ 41 double z_re_bak = z.re; 42 z.re = z.re*z.re-z.im*z.im+c.re; 43 z.im = 2*z_re_bak*z.im+c.im; 44 if(z.re>2||z.im>2) 45 return t; 46 } 47 return 0; 48 } 49 50 struct RGB 51 { 52 unsigned char R; 53 unsigned char G; 54 unsigned char B; 55 }; 56 57 struct RGB translate2RGB(int code) //code: 0 - 511 58 { 59 struct RGB ret = {0, 0, 0};//返回局部变量,危险 60 if(code){ 61 ret.R = (int)(sqrt((double)code/NUM)*256); 62 ret.G = (int)(((double)code/NUM*(1.0-(double)code/NUM))*NUM); 63 ret.B = (int)(pow((1.0-(double)code/NUM), 108)*256); 64 } 65 return ret; 66 } 67 68 int main() 69 { 70 FILE *fp = fopen("out.ppm", "wb"); 71 fprintf(fp, "P6\n%d %d\n255\n", XDIM, YDIM); 72 for(int j=0;j<YDIM;j++) 73 for(int i=0;i<XDIM;i++) 74 { 75 struct RGB tof_ret = translate2RGB(tof_real(i, j)); 76 fprintf(fp, "%c%c%c", tof_ret.R, tof_ret.G, tof_ret.B); 77 } 78 79 fclose(fp); 80 return 0; 81 }
DEMO
标签:Visualization,Set,YDIM,int,double,Mandelbrot,XDIM,re,include From: https://www.cnblogs.com/YHFBlogs/p/16626196.html