发几幅由计算机生成的图像,以展示算法之美.并提供生成图像的算法代码.代码中,一部分是由C++实现,另一部分是由一种我定义的脚本语言实现.
相关软件见:Why数学图像生成工具.
(1)树
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 void CPixelIFSTree::BuildPixelsMap() 2 { 3 float m[5][7]; 4 5 //'IFS码赋值 6 m[0][0] = 0.195f; m[0][1] =-0.488f; m[0][2] = 0.344f; m[0][3] = 0.433f; m[0][4] = 0.4431f; m[0][5] = 0.2452f; m[0][6] = 0.25f; 7 m[1][0] = 0.462f; m[1][1] = 0.414f; m[1][2] =-0.252f; m[1][3] = 0.361f; m[1][4] = 0.2511f; m[1][5] = 0.5692f; m[1][6] = 0.25f; 8 m[2][0] =-0.058f; m[2][1] =-0.07f; m[2][2] = 0.453f; m[2][3] =-0.111f; m[2][4] = 0.5976f; m[2][5] = 0.0969f; m[2][6] = 0.25f; 9 m[3][0] =-0.035f; m[3][1] = 0.07f; m[3][2] =-0.469f; m[3][3] =-0.022f; m[3][4] = 0.4884f; m[3][5] = 0.5069f; m[3][6] = 0.2f; 10 m[4][0] =-0.637f; m[4][1] = 0.0f; m[4][2] = 0.0f; m[4][3] = 0.501f; m[4][4] = 0.8562f; m[4][5] = 0.2513f; m[4][6] = 0.05f; 11 12 float a, b, c, d, e, f; //'仿射变幻中的系数 13 14 float x = 0.0f; 15 float y = 0.0f; 16 float t; 17 18 for (int i = 0; i < 100000000; i++) 19 { 20 float R = (float)rand()/RAND_MAX; 21 22 if (R <= m[0][6]) 23 { 24 a = m[0][0]; b = m[0][1]; c = m[0][2]; d = m[0][3]; e = m[0][4]; f = m[0][5]; 25 } 26 else if (R <= m[0][6] + m[1][6]) 27 { 28 a = m[1][0]; b = m[1][1]; c = m[1][2]; d = m[1][3]; e = m[1][4]; f = m[1][5]; 29 } 30 else if (R <= m[0][6] + m[1][6] + m[2][6]) 31 { 32 a = m[2][0]; b = m[2][1]; c = m[2][2]; d = m[2][3]; e = m[2][4]; f = m[2][5]; 33 } 34 else if (R <= m[0][6] + m[1][6] + m[2][6] + m[3][6]) 35 { 36 a = m[3][0]; b = m[3][1]; c = m[3][2]; d = m[3][3]; e = m[3][4]; f = m[3][5]; 37 } 38 else 39 { 40 a = m[4][0]; b = m[4][1]; c = m[4][2]; d = m[4][3]; e = m[4][4]; f = m[4][5]; 41 } 42 43 t = a*x + b*y + e; 44 y = c*x + d*y + f; 45 x = t; 46 47 int ix = ((int)(x*1024.0f))&0x3ff; 48 int iy = 1024 - ((int)(y*1024.0f))&0x3ff; 49 m_pixels_map[iy][ix]+=1; 50 } 51 52 if (!m_pixels_map[0][0]) 53 { 54 m_pixels_map[0][0] = 1; 55 } 56 } 57 58 unsigned int CPixelIFSTree::CalculatePixel(unsigned int x, unsigned int y) 59 { 60 if (!m_pixels_map[0][0]) 61 { 62 BuildPixelsMap(); 63 } 64 65 float fr = logf((float)m_pixels_map[y][x])*32.0f; 66 unsigned int r = FLOAT_255_TO_BYTE(fr); 67 float fg = logf((float)m_pixels_map[y][x]*1.2f)*56.0f; 68 unsigned int g = FLOAT_255_TO_BYTE(fg); 69 70 unsigned b = m_pixels_map[y][x] & 0xff; 71 72 return MAKE_RGB(r,g,b); 73 }
(2)晕
这是由Sin函数生成的图像,让人看着眼花
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
pixels = W:1024 H:1024 x = from (-8*PI) to (8*PI) W y = from (-8*PI) to (8*PI) H r = sin(x+y) g = sin(x-y) b = sin(x*y) r = r*0.5 + 0.5 g = g*0.5 + 0.5 b = b*0.5 + 0.5
(3)格
这是由Tan函数生成的图像,密集恐惧症者可以拿它来练胆.
pixels = W:1024 H:1024 x = from 0 to 1023 W y = from 0 to 1023 H r = tan(x*y) g = r*0.5 b = r*0.2
(4)JuliaSets
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 unsigned int CPixelJuliaSets3::CalculatePixel(unsigned int x, unsigned int y) 2 { 3 int K=100; 4 int m=500; 5 float xs = -1.5f; 6 float xl = 1.5f; 7 float ys = -1.5f; 8 float yl = 1.5f; 9 float p = 0.32f; 10 float q = 0.043f; 11 float xb = (xl - xs) / m_width; 12 float yb = (yl - ys) / m_height; 13 14 float x0=xs+x*xb; 15 float y0=ys+y*yb; 16 int k=0; 17 int H; 18 loop1: 19 float xk=x0*x0-y0*y0+p; 20 float yk=2*x0*y0+q; 21 k=k+1; 22 float r=xk*xk+yk*yk; 23 x0=xk; 24 y0=yk; 25 if(r>m) 26 { 27 H=k; 28 goto loop2; 29 } 30 if(k==K) 31 { 32 H=int(r*10); 33 goto loop2; 34 } 35 if(r<=m && k<K) 36 { 37 goto loop1; 38 } 39 loop2: 40 41 float fr = logf((float)H)*256.0f; 42 unsigned int br = (unsigned char)fr; 43 44 float fg = logf((float)H)*128.0f; 45 unsigned int bg = (unsigned char)fg; 46 47 unsigned int bb = (H*100)&0xff; 48 49 return MAKE_RGB(br,bg,bb); 50 }
(5)Mandlbrot
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 unsigned int CPixelMandlbrot1::CalculatePixel(unsigned int x, unsigned int y) 2 { 3 int K=100; 4 int m=500; 5 float pl=0.9f; 6 float ps=-2.3f; 7 float ql=1.2f; 8 float qs=-1.2f; 9 10 float p=(pl-ps)/m_width; 11 float q=(ql-qs)/m_height; 12 13 float p0=ps+x*p; 14 float q0=qs+y*q; 15 int k=0; 16 float x0=0.0f; 17 float y0=0.0f; 18 int H; 19 20 float xk; 21 float yk; 22 float r; 23 24 loop1: 25 xk=x0*x0-y0*y0+p0; 26 yk=2*x0*y0+q0; 27 k=k+1; 28 r=xk*xk+yk*yk; 29 x0=xk; 30 y0=yk; 31 if(r>m) 32 { 33 H=k; 34 goto loop2; 35 } 36 if(k==K) 37 { 38 H=int(r*1); 39 goto loop2; 40 } 41 if(r<=m && k<K) 42 { 43 goto loop1; 44 } 45 46 loop2: 47 float fr = logf((float)H)*256.0f; 48 unsigned int br = (unsigned char)fr; 49 50 float fg = logf((float)H)*128.0f; 51 unsigned int bg = (unsigned char)fg; 52 53 unsigned int bb = (H*100)&0xff; 54 55 return MAKE_RGB(br,bg,bb); 56 }
(6)棋盘
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
pixels = W:1024 H:1024 u = from 0 to 16 W v = from 0 to 16 H m = floor(u%2) n = floor(v%2) r = if(xor_bool(m,n), 0.0, 1.0) g = r b = r
(7)红
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 unsigned short RD(int i,int j){ 3 double r=i/256.-2,s=j/256.-2,q=r*r+s*s,n=hypot(r+(.866-r/2)/q,s+(r*.866+s/2)/q), 4 d=.5/log(n);if(d<0||d>1)d=1;return d*(sin(n*10)*511+512); 5 } 6 unsigned short GR(int i,int j){ 7 return 0; 8 } 9 unsigned short BL(int i,int j){ 10 double r=i/256.-2,s=j/256.-2,q=r*r+s*s;return RD(i,j)*sqrt(q/40); 11 } 12 */ 13 14 // -------------------------------------------------------------------------------------- 15 16 unsigned int CPixelJoukowsky::CalculatePixel(unsigned int i, unsigned int j) 17 { 18 double r=i/256.0-2; 19 double s=j/256.0-2; 20 double q=r*r+s*s; 21 double n=hypot(r+(0.866-r/2)/q,s+(r*.866+s/2)/q); 22 double d=0.5/log(n); 23 24 if(d<0||d>1) 25 { 26 d=1; 27 } 28 29 float fr = (float)(d*(sin(n*10)*511+512)); 30 31 unsigned int br = FLOAT_255_TO_BYTE(fr); 32 unsigned int bg = 0; 33 34 float fb = fr*sqrtf((float)q/40); 35 unsigned int bb = FLOAT_255_TO_BYTE(fb); 36 37 return MAKE_RGB(br,bg,bb); 38 }