home *** CD-ROM | disk | FTP | other *** search
- /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- Name : Chernikov
- Version: 1.0
- Written on : 24-Apr-92 By : A. Etemadi
- Modified on :
- Directory : ~atae/Space
-
- ==============================================================================
-
-
- Usage:
- Chernikov -[AKUVhsnrc] > OutputPGMImage
-
- Output result :
-
- 0 = successful,
- -1 = error,
-
- Functionality:
-
- Iterates through the Chernikov equations (Nature Vol. 326, April 1987)
- and produces an image based on occupancy of cells.
-
- ----------------------------------------------------------------------------*/
-
- #include <stdio.h> /* Standard C I/O library */
- #include <math.h> /* Standard C mathematics library */
-
- #define pi 2.0*acos(0.0)
-
- main(argc,argv)
-
- int argc;
- char **argv;
-
- {
-
- /*
- =======================================================================
- ===================== START OF DECLARATION ============================
- =======================================================================
- */
-
- /*
- Indecies
- */
-
- register int i,j;
- register int row,col;
-
- int Height,Width;
-
- FILE *p_OutFile; /* Pointer to start of output file */
-
- /*
- Create buffer for image
- */
- char OutImage[512][512];
-
- /*
- Chernikov parameters
- */
- float Alpha;
- float K;
- float U0,U1;
- float V0,V1;
- float Scale;
-
- int NIter;
-
- float KoverAlpha;
- float cosAlpha;
- float sinAlpha;
- float Beta;
- /*
- =======================================================================
- ===================== START OF INITIALISATION =========================
- =======================================================================
- */
-
- /*
- Set defaults, process command line options
- */
- Alpha = 2.0*pi/5.0;
- K = 0.9;
- U0 = 0.0;
- V0 = 15.0;
- Scale = 1.5;
- NIter = 10000;
- Height = Width = 256;
-
- for (i=1;i<argc;i++) {
- if (argv[i][0] == '-') {
- switch (argv[i][1]) {
- case 'A': Alpha = atof(argv[++i]); continue;
- case 'K': K = atof(argv[++i]); continue;
- case 'U': U0 = atof(argv[++i]); continue;
- case 'V': V0 = atof(argv[++i]); continue;
- case 's': Scale = atof(argv[++i]); continue;
- case 'n': NIter = atoi(argv[++i]); continue;
- case 'r': Height = atoi(argv[++i]); continue;
- case 'c': Width = atoi(argv[++i]); continue;
- default : fprintf(stderr,"unrecognized option: %s",argv[i]); return(-1);
- case 'h':
- fprintf(stderr,"\nUSAGE :\n");
- fprintf(stderr," Chernikov -[AKUVhsnrc] > OutImage\n\n");
- fprintf(stderr,"OPTION: DEFAULT:\n");
- fprintf(stderr,"-----------------------------------\n");
- fprintf(stderr,"-A Alpha 2pi/15\n");
- fprintf(stderr,"-K Kappa 0.9\n");
- fprintf(stderr,"-U Initial pahse 0.0\n");
- fprintf(stderr,"-V Initial Velocity 15.0\n");
- fprintf(stderr,"-s Scale 2.0\n");
- fprintf(stderr,"-n Interations 10000\n");
- fprintf(stderr,"-r Image Height 256\n");
- fprintf(stderr,"-c Image Width 256\n");
- return(-1);
- }
- }
- }
-
- /*
- Use standard output
- */
- p_OutFile = stdout;
- /*
- Initialise OutImage pixels and starting parameters
- */
- for (i = 0; i < Height; i++) {
- for (j = 0; j < Width; j++) {
- OutImage[i][j] = (char) 0;
- }
- }
-
- cosAlpha = cos(Alpha);
- sinAlpha = sin(Alpha);
- KoverAlpha = K/Alpha;
- fprintf(stderr,"\n");
-
- /*
- Now iterate through the Chernikov equation
- */
- for (i = 0; i < NIter; i++) {
-
- row = (int) (U0*Scale) + Height/2;
- col = (int) (V0*Scale) + Width/2;
-
- if ( (i+1)%10000 == 0)
- fprintf(stderr,"Iteration = %d\r",i+1);
-
- if (row < Height && col < Width && row > 0 && col > 0 &&
- (OutImage[row][col] & 0377) < 255 )
- OutImage[row][col] += (char) 1;
-
- Beta = U0 + KoverAlpha*sin(V0);
- U1 = Beta*cosAlpha + V0*sinAlpha;
- V1 = -Beta*sinAlpha + V0*cosAlpha;
- U0 = U1; V0 = V1;
- }
- /*
- Write resultant image with PGM header
- */
-
- fprintf(p_OutFile,"P5\n%d %d\n255\n",Height,Width);
- for (i = 0; i < Height; i++) {
- for (j = 0; j < Width; j++) {
- putc(OutImage[i][j],p_OutFile);
- }
- }
-
- /*
- Close file
- */
- fprintf(stderr,"\n");
- fclose(p_OutFile);
-
- return(0);
- }
-