1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| #define size 51
float cal_r(float x,float y,float z){ return sqrt(x*x+y*y+z*z); } char shade[]=".,;!o*m0&%@"; int out_i[size][size]; float dyn=4.0; float a=8.0; float delta=0.75; float move=(size-1)/2; float dis[size][size];
void cal_shade(float *n,int n_i,int n_j,float *ro_a,float *ro_b,float *ro_c,float *light,float rotate,int *out_i){ for(int i=0;i<3;i++)n[i]=i==n_i?n_j:0; float n_a[3]={n[0]*ro_a[0]+n[1]*ro_a[3]+n[2]*ro_a[6], n[0]*ro_a[1]+n[1]*ro_a[4]+n[2]*ro_a[7], n[0]*ro_a[2]+n[1]*ro_a[5]+n[2]*ro_a[8]}; float n_b[3]={n_a[0]*ro_b[0]+n_a[1]*ro_b[3]+n_a[2]*ro_b[6], n_a[0]*ro_b[1]+n_a[1]*ro_b[4]+n_a[2]*ro_b[7], n_a[0]*ro_b[2]+n_a[1]*ro_b[5]+n_a[2]*ro_b[8]}; float n_r[3]={n_b[0]*ro_c[0]+n_b[1]*ro_c[3]+n_b[2]*ro_c[6], n_b[0]*ro_c[1]+n_b[1]*ro_c[4]+n_b[2]*ro_c[7], n_b[0]*ro_c[2]+n_b[1]*ro_c[5]+n_b[2]*ro_c[8]}; float result=(n_r[0]*light[0]+n_r[1]*light[1]+n_r[2]*light[2]+1.0)*0.5; float co[3]; co[n_i]=dyn*cos(rotate*2); int co_a=n_i==0?1:n_i==1?0:0; int co_b=n_i==0?2:n_i==1?2:1; co[n_i]+=move/2; for(co[co_a]=-a;co[co_a]<=a;co[co_a]+=delta) for(co[co_b]=-a;co[co_b]<=a;co[co_b]+=delta){ int final=(int)(result*10.0); final=(final>10||final)<0?final>10?10:0:final; float x_a=co[0]*ro_a[0]+co[1]*ro_a[3]+co[2]*ro_a[6]; float y_a=co[0]*ro_a[1]+co[1]*ro_a[4]+co[2]*ro_a[7]; float z_a=co[0]*ro_a[2]+co[1]*ro_a[5]+co[2]*ro_a[8]; float x_b=x_a*ro_b[0]+y_a*ro_b[3]+z_a*ro_b[6]; float y_b=x_a*ro_b[1]+y_a*ro_b[4]+z_a*ro_b[7]; float z_b=x_a*ro_b[2]+y_a*ro_b[5]+z_a*ro_b[8]; float x_r=x_b*ro_c[0]+y_b*ro_c[3]+z_b*ro_c[6]; float y_r=x_b*ro_c[1]+y_b*ro_c[4]+z_b*ro_c[7]; float z_r=x_b*ro_c[2]+y_b*ro_c[5]+z_b*ro_c[8]; int cox=(int)(n_j*x_r+move); int coy=(int)(n_j*z_r+move); if(n_j*y_r<dis[cox][coy]){ out_i[cox*size+coy]=final; dis[cox][coy]=n_j*y_r; } } }
int main(){ float n[3]; float light[3]={-1,-1,1}; float length=cal_r(light[0],light[1],light[2]); for(int i=0;i<3;i++)light[i]/=length; for(float rotate=0.0;;rotate+=0.05){ for(int i=0;i<size;i++) for(int j=0;j<size;j++){ out_i[i][j]=-1; dis[i][j]=(float)(4.0*size); } float cos_ro=cos(rotate),sin_ro=sin(rotate); float ro_a[3][3]={{1.0,0,0},{0,cos_ro,-sin_ro},{0,sin_ro,cos_ro}}; float ro_b[3][3]={{cos_ro,0,sin_ro},{0,1.0,0},{-sin_ro,0,cos_ro}}; float ro_c[3][3]={{cos_ro,-sin_ro,0},{sin_ro,cos_ro,0},{0,0,1.0}}; for(int i=0;i<3;i++){ cal_shade(&n[0],i,1,&ro_a[0][0],&ro_b[0][0],&ro_c[0][0],&light[0],rotate,&out_i[0][0]); cal_shade(&n[0],i,-1,&ro_a[0][0],&ro_b[0][0],&ro_c[0][0],&light[0],rotate,&out_i[0][0]); } for(int i=0;i<size;i++){ for(int j=0;j<size;j++) out_i[i][j]!=-1?printf("%c%c",shade[out_i[i][j]],shade[out_i[i][j]]):printf(" "); printf("\n"); } printf("\x1b[2J"); printf("\x1b[H"); usleep(30000); } }
|