My overall assignment is to write a program that draws 4 concentric circles, editing an example program that makes 1 circle. I've gotten it done almost but 1 part is causing me trouble. I need it to be something like this: (trouble area marked)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* Program 9
|
|
*/
makeCircles( FILE *image, int nrows, int ncols )
{
const int level1 = 68; /* inner most to outer most */
const int level2 = 130;
const int level3 = 193;
const int level4 = 255;
const int levelOUT = 40; /* outside of the circle */
int r, c ; /* row and col of current pixel */
int rc, cc ; /* row and col of center of image */
int rd, cd ; /* row and col difference from center */
double radius, dist ; /* radius of circle, distance from center */
/* calculate center of image, size of radius */
rc = nrows/2;
cc = ncols/2;
if ( nrows<ncols ) radius = nrows/2;
else radius = ncols/2;
/* write out the pixel data */
for ( r=0; r<nrows; r++ )
{
rd = r - rc;
for ( c=0; c<ncols; c++ )
{
cd = c - cc;
dist = sqrt( (double)(rd*rd + cd*cd) );
if ( dist < radius*0.2 )
fputc( level1, image );
else
if(dist < radius*0.4)
fputc( level2, image );
else
if(dist < radius*0.6)
fputc(level3, image);
else
if(dist < radius*0.8)
fputc(level4, image);
else
fputc(levelOUT, image);
}
}
}
/* write out the PGM Header information
|
| The number of gray levels is assumed to be 255.
|
*/
void writeHeader( FILE *image, int nrows, int ncols )
{
fprintf( image, "P5 ");
fprintf( image, "# Created by makeCircles\n");
/* width, height, number of gray levels */
fprintf( image, "%d %d %d ", ncols, nrows, 255 );
}
void openImage( char *argv[], FILE **image, int *nrows, int *ncols )/*TROUBLE*/
{
/* open the image file for writing */
if ( (*image = fopen( argv[1], "wb") ) == NULL )
{
printf("file %s could not be created\n", argv[1] );
exit( EXIT_FAILURE );
}
*nrows = atoi( argv[2] );
if ( *nrows < 1 )
{
printf("number of rows must be positive\n");
exit( EXIT_FAILURE );
}
*ncols = atoi( argv[3] );
if ( *ncols < 1 )
{
printf("number of columns must be positive\n");
exit( EXIT_FAILURE );
}
}
int main( int argc, char *argv[] )
{
/* check the command line parameters */
if ( argc != 4)
{
printf("concircles fileName.pgm nrows ncols\n");
return 0;
}
int nrows = *argv[2];
int ncols = *argv[3] ;
FILE *image; /*trouble here maybe */
openImage(argv, , &nrows, &ncols);/*trouble here*/
/* write header information */
writeHeader( image, nrows, ncols );
/* Create the Image */
makeCircles( image, nrows, ncols );
/* close the file */
fclose ( image );
return 1;
}
I can get it to work just fine when I put the contents of openImage into main though, can someone help me out with how to call openImage so It'll run?