I21 Answer — Ripples


int main ( int argc, char* argv[] )
{
  image img ;
  int r, c;
  int nrows, ncols, nripples ;
  double dist, halfDiag;
  int gray;
  int rc, cc; /* coordinates of center */

  if ( argc != 5)
  {
    printf("ripples fileName nrows ncols nripples\n");
    system( "pause" );
    return EXIT_SUCCESS;
  }

  nrows =    atoi( argv[2] );
  ncols =    atoi( argv[3] );
  nripples = atoi( argv[4] );

  if ( newImage( &img, nrows, ncols ) == NULL )
  {
    printf(">>error<< can't allocate memory\n");
    system( "pause" );
    return;
  }

  /* calculate the coordinates of the center */
  rc = nrows/2;
  cc = ncols/2;

  /* determine half the length of the diagonal */
  halfDiag = sqrt( (double)(nrows*nrows + ncols*ncols) )/2.0;

  for ( r = 0; r<img.nrows; r++ )
    for ( c = 0; c<img.ncols; c++ )
    {
      dist = sqrt( (double)((r-rc)*(r-rc)+(c-cc)*(c-cc)) );
      gray = 128 + 127*cos( 2*M_PI*nripples*dist/halfDiag );
      setPixel( img, r, c, gray );
    }

  writePGMimage( img, argv[1]);
  freeImage( &img );
}
   

back