代码之美-2[转]

太极图。


 // 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
#define LEFT (i < DIM/2)
#define C1 C(DIM/2, DIM/2, DIM/2)
#define C2 C(DIM/2, DIM/4, DIM/4)
     return BL(i, j);
 }
 unsigned char GR(int i,int j){
     // YOUR CODE HERE
#define C3 C(DIM/2, 3*DIM/4, DIM/4)
#define C4 C(DIM/2, DIM/4, DIM/16)
#define C5 C(DIM/2, 3*DIM/4, DIM/16)
     return BL(i, j);
 }
 unsigned char BL(int i,int j){
     // YOUR CODE HERE
#define C(x, y, r) (_sq(i - (x)) + _sq(j - (y)) < _sq((r)))
     return !C1 ? 127 : C2 ? C4 ? 255 : 0 : C3 ? C5 ? 0: 255 : LEFT ? 0 :255;
 }
   
 void pixel_write(int,int);
 FILE *fp;
 int main(){
     fp = fopen("taiji.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);
 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值