Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

C: Printing A Circle

This program will demonstrate how a circle (text based) can be printed in the C console application.


We start with the headers.
//WinterNurf Productions

#include<stdio.h>
#include<conio.h>

#include<math.h>

Next, we begin the main() function and declare two loop control integer variables.
int main()
{
      intnX = 0;

      intnY = 0;

We now declare a character variable and initialize it with the ASCII value 219. I've used this value because this prints a solid block on the screen. Feel free to use any other character in its place.
      charchBlock = 219;

Now, to print the circle, we will create a nested loop. We first create an outer loop for the y - axis.
      for(nY = 18; nY > 5; nY--)

Next comes the inner loop for the x - axis. Be sure to loop 80 times here because the width of the console application is 80 characters, and so we won't need to print a newline character every time a row ends.
            for(nX = -39; nX <= 40; nX++)

To print the circle, we utilize the equation of the circle, and print the graph. So, if the equation is true, we print our character, otherwise a space.
                  if(nY == (int)sqrt(300 - (nX * nX)))
                        printf ("%c", chBlock);
                  else

                        printf (" ");

This will print only the upper half of the circle. To print the lower half, we follow the exact same procedure, except this time, we add the unary minus operator in the equation of the circle.
      for(nY = -6; nY > -18; nY--)
            for(nX = -39; nX <= 40; nX++)
                  if(nY == -(int)(sqrt(300 - (nX * nX))))
                        printf ("%c", chBlock);
                  else

                        printf (" ");

Note that we could have created this in one go. However, since the characters in the console application are not perfectly square but a bit rectangular, we have done it this way. That also explains the loop values we have taken for the outer loops.

And that's it! We end the program.
      _getch();
      return 0;

}

Here is the full code.
//WinterNurf Productions

#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
      intnX = 0;
      intnY = 0;
      charchBlock = 219;

      for(nY = 18; nY > 5; nY--)
            for(nX = -39; nX <= 40; nX++)
                  if(nY == (int)sqrt(300 - (nX * nX)))
                        printf ("%c", chBlock);
                  else
                        printf (" ");

      for(nY = -6; nY > -18; nY--)
            for(nX = -39; nX <= 40; nX++)
                  if(nY == -(int)(sqrt(300 - (nX * nX))))
                        printf ("%c", chBlock);
                  else
                        printf (" ");

      _getch();
      return 0;

}

This program has been written in Microsoft Visual Studio for Desktop. It may not work in older compilers. However, simple editing will make the code compatible.


This post first appeared on Code Walk, please read the originial post: here

Share the post

C: Printing A Circle

×

Subscribe to Code Walk

Get updates delivered right to your inbox!

Thank you for your subscription

×