代码之美-1[转]

短短的几行代码就能导出下面这样炫酷的图,这也许就是代码的美吧!


 // 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
   
 unsigned char GR(int,int);
 unsigned char BL(int,int);
   
 unsigned char RD(int i,int j){
    // YOUR CODE HERE
    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;
 }
 unsigned char GR(int i,int j){
    // YOUR CODE HERE
    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;
 }
 unsigned char BL(int i,int j){
    // YOUR CODE HERE
    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;
 }
   
 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);
 }

猜你喜欢

转载自blog.csdn.net/zhongjuelong/article/details/47089113
今日推荐