I09 Answer


makeCircleChecks( FILE *image, int nrows, int ncols )
{
  const int levelINa  = 220; /* gray levels inside and */
  const int levelINb  = 110;
  const int levelOUTa = 170;   /* outside of the circle  */
  const int levelOUTb = 90;

  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 */
  int radius;       /* radius of circle */
  int distSq;       /* distance from center, squared */
  int radiusSq;     /* radius squared */

  int sideIn;       /* side of inside checkerboard square */
  int sideOut;      /* side of outside checkerboard square */
  int rk, ck;       /* row and column of checkerboard */

  /* calculate center of image, size of radius */
  rc = nrows/2;
  cc = ncols/2;

  if ( nrows<ncols )
  {
    radius  = nrows/3;
    sideIn  = nrows/16;
    sideOut = nrows/12;
  }
  else               
  {
    radius  = ncols/3;
    sideIn  = ncols/16;
    sideOut = ncols/12;
  }
  radiusSq = radius*radius;

  /* write out the pixel data */
  for ( r=0; r<nrows; r++ )
  {
    rd = r - rc;

    for ( c=0; c<ncols; c++ )
    {
      cd   = c - cc;
      distSq =  rd*rd + cd*cd ;

      if ( distSq > radius*radius )
      {
        /* determine row and col of outer checkerboard */
        rk = r/sideIn;
        ck = c/sideIn;
        if ( (rk+ck)%2 == 0 )
          fputc( levelOUTa, image );
        else
          fputc( levelOUTb, image );
      }
      else
      {
        /* determine row and col of inner checkerboard */
        rk = r/sideOut;
        ck = c/sideOut;
        if ( (rk+ck)%2 == 0 )
          fputc( levelINa, image );
        else
          fputc( levelINb, image );
      }
    }
  }
}

Comments: Integer math has been used in this program for determining if pixels are inside or outside of the circle.

back