/* Koch Curve */ #include#include #include /*Random Number*/ #define IA 16807 #define IM 2147483647 #define AM (1.0/IM) #define IQ 127773 #define IR 2836 #define NTAB 32 #define NDIV (1+(IM-1)/NTAB) #define EPS 1.2e-7 #define RNMX (1.0-EPS) unsigned long int idum =1024; float ran1(long *); main() { int j; /*j;step number*/ double x=1.0,nx=0.0; double y=0.0,ny=0.0; double r=0.0; for(j=0;j<=100000;j++) { r=ran1(&idum); if (r >= 0.5){ nx=0.5*x+y*1.7320501/6.0; ny=x*1.7320501/6.0-0.5*y; } else if (r < 0.5){ nx=0.5*x-y*1.7320501/6.0+0.5; ny=-x*1.7320501/6.0-0.5*y+1.7320501/6.0; } printf("%f %f\n",nx, ny); /*printf("%d %f\n",j, nx);*/ x=nx; y=ny; } return 0; } float ran1(long *idum) { int j; long k; static long iy=0; static long iv[NTAB]; float temp; if(*idum <= 0 || !iy){ if(-(*idum) < 1) *idum =1; else *idum = -(*idum); for (j=NTAB +7;j>=0; j--){ k=(*idum)/IQ; *idum=IA*(*idum-k*IQ)-IR*k; if (*idum < 0) *idum += IM; if( j < NTAB) iv[j]=*idum; } iy=iv[0]; } k=(*idum)/IQ; *idum=IA*(*idum-k*IQ)-IR*k; if (*idum < 0) *idum += IM; j=iy/NDIV; iy=iv[j]; iv[j]= *idum; if((temp=AM*iy) > RNMX) return RNMX; else return temp; }
出力結果
Back to C Language