C/C++ Program to draw a circle with stars without graphics.h

The following program first creates an empty 2-dimensional array, then stores star('*') character into all the elements that would fall on or near the circumference of the circle.

Categories | About |     |  

Parveen,

The Desired Output

The output as it appears on a console window is a bit distorted, but almost a circle.

                * * * * *
            *               *
        *                       *
      *                           *
    *                               *

  *                                   *

*                                       *
*                                       *
*                                       *
*                                       *
*                                       *

  *                                   *

    *                               *
      *                           *
        *                       *
            *               *
                * * * * *

The Equation to be Plotted

A circle is represented by the well-known equation x2 + y2 = R2. A console window is sufficient enough to display a page of about 24x24 points. We can, therefore, set to draw a circle of radius, about, 10.

So the equation to be drawn is: x2 + y2 = 102

Getting the Canvas Ready

A two dimensional array of, say, 24x24 chars can be viewed as a canvas. Our plan would be to fill star (*) character into each of the elements of this array that roughly satisfy the above equation.

But wait, one problem must be solved before that.

The numbering of the elements of an array starts at the top left corner [0][0], so we can say that the "origin" is at the top left corner. If we draw a circle there, then only one-fourth of it would be visible. We must shift the center to the mid-point of our 24x24 canvas.

A transformation mapping of each point of our 2D array has to be done in such a way that the element [11][11] becomes the center (0, 0) of our drawing. Examine the code below to see how we can do that!

The Source Code

You can copy paste this directly into your cpp file and compile

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{

  const int DIMEN = 24;

  // fill the canvas with NULLs 
  char canvas [DIMEN][DIMEN] = {{0}};

  // the origin will be at (12, 12) 
  // so that our drawing is 
  // well positioned 
  // all the points that satisfy the 
  // equation x^2 + y^2 = 10^2 will be 
  // filled as * 
  for(int row = 0; row < DIMEN; row++)
  {

    for(int col = 0; col < DIMEN; col++)
    {

      // origin is at the center, 
      // so obtain the x and y coord- 
      // -inates of each element with 
      // respect to the origin 
      int x = col - DIMEN/2;

      int y = DIMEN/2 - row;

      // x^2 + y^2 = 10^2 
      // since we are dealing with 
      // integers, we can keep 
      // +/-5 tolerance for nicer looks 
      int sumsq = x*x + y*y;

      if((95 < sumsq) && (sumsq < 105))
      {

        canvas[row][col] = '*';

      }

    }

  }

  // print 
  for(int row = 0; row < DIMEN; row++)
  {

    for(int col = 0; col < DIMEN; col++)
    {

      // THERE IS A SPACE AFTER %c 
      // ... done for nicer looks 
      printf("%c ", canvas[row][col]);

    }

    cout << endl;

  }

  return 0;

}

Alternate approach

Instead of creating a char array, we could directly print to the console.

To understand the following code imagine you have a square graph paper on your table. The paper extends from x = -RAD to x = +RAD, and from y = -RAD to y = +RAD. Let a circle be drawn with its center at (0, 0).

Start scanning with your eye from the lower left corner (to do this we have run a nested for-loop below). You have to raster through each point, examining it. If the point lies on the circle, execute a printf("*"), otherwise execute a printf(" "). In this way, a corresponding circle will get drawn on the console.

#include <cstdio>
#include <math.h>
using namespace std;

int main ( )  {
// radius 
const int RAD = 10;

// tolerance 
const int TOL = 5;

for(int x = -RAD; x <= RAD; x++)
{

  for(int y = -RAD; y <= RAD; y++)
  {

    int eq = x*x + y*y - RAD * RAD;

    printf(abs(eq) < TOL ? "*" : " ");

  }

  printf("\r\n");

}

return 0;

}


This Blog Post/Article "C/C++ Program to draw a circle with stars without graphics.h" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Comments and Discussion