#include <stdlib.h> #include <stdio.h> #include <math.h> int main(int argc, char *argv[]) { int r, nrows, c, ncols; int red, grn, blu; FILE *image; /* check the command line parameters */ if ( argc != 4 ) { printf("spiral fileName.ppm nrows ncols \n"); return 0; } /* open the image file for writing in binary mode */ if ( (image = fopen( argv[1], "wb") ) == NULL ) { printf("file %s could not be created\n", argv[1]); return 0; } nrows = atoi( argv[2] ); ncols = atoi( argv[3] ); /* write out the PPM Header information */ fprintf( image, "P6 "); fprintf( image, "%d %d %d ", ncols, nrows, 255 ); /* find the maximum distance from the center of the image */ double radius = sqrt( nrows*nrows + ncols*ncols )/2.0; double dist, thetaCC ; double cntR = nrows/2.0, cntC = ncols/2.0; /* write out the pixel data */ for ( r=0; r<nrows; r++ ) for ( c=0; c<ncols; c++ ) { dist = sqrt( (r-cntR)*(r-cntR) + (c-cntC)*(c-cntC)) ; thetaCC = atan2( (c-cntC), (r-cntR) ); red = 125 + 125*cos( 2*M_PI*(dist/radius) + thetaCC); grn = 125 + 125*cos( 2*M_PI*(dist/radius) + thetaCC); blu = 125 + 125*cos( 2*M_PI*(dist/radius) + thetaCC); fputc( red, image ); fputc( grn, image ); fputc( blu, image ); } /* close the file */ fclose ( image ); return 1; }
Comments:
The hardest part of this program is calculating thetaCC
.
As usual, the programming is not hard, but the math may take some thought.
This might be an opportunity for you to do that review of trigonometry
you keep putting off.