Making a circle.

Hi there, I am VERY new to coding and I am having trouble constructing a basic circle with asterisks. I need to do it using loops but I am struggling. The circle is meant to look like this:

1
2
3
4
5
6
     *  * 
   *      * 
  *        * 
  *        * 
   *      * 
     *  *


It would be great for a bit of help, even if it just for the first two lines so I can get a better idea on how to continue. Thanks everyone
I have seen that one, but the fact that it prints 3 stars in the first line throws me off.
I dont really understand how I would be able to print a star, two spaces, then a star using a loop.

I am using the code in the person's post that prints the first line but I cant figure out what to change to get it to look like the circle i want
Last edited on
Sorry I wish I could be more help.. I was just pointing out what I found. Seems to be an interesting problem (that I have not yet encountered) and it seems there is very simple solutions and very complex. Reminds me of when I was looking at random number generation. It seems this is the simpler of the two sides of the complication. hope you get a solid solution. And if you do would you be kind enough to post your solution here?

EDIT: I attempted to deconstruct the problem, and felt lost immediately also.
Last edited on
Thanks for trying, my friend is going to help me with the solution this afternoon, and I will post it here. I think it is one of those problems that is quite easy, I am just a little clueless with C++ as I started my course about 6 weeks ago.
Hi, AdehhRR

If I were going to do this, I would separate the logic for defining the circle and the logic for drawing and treat them as two different problems.

if we have a 2 dimensional 7x7 array of Boolean values all set to false called shape_elements:

1
2
3
4
5
6
7
[0][0][0][0][0][0][0]
[0][0][0][0][0][0][0]
[0][0][0][0][0][0][0]
[0][0][0][0][0][0][0]
[0][0][0][0][0][0][0]
[0][0][0][0][0][0][0]
[0][0][0][0][0][0][0]


we essentially have a very similar concept to a pixel buffer.

if in our draw_to_screen() code we loop over all of the elements of this array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (int i=0; i<Y_ELEMENTS;i++)
{
  for (int j=0; j<X_ELEMENTS;j++)
  {
    if(shape_elements[i][j])
    {
        std::cout<<"*";
     }
    else
    {
        std::cout<<" ";
     }
   }
  std::cout<<std::endl;
}


we have a standardized method for "rendering" the buffer out to the screen.

Then the problem becomes populating the array in the first place.

for every "pixel" we want to calculate the distance to the centre of the circle, if this value is less than the circles maximum radius we know that the point is within the circle.

the distance is just some maths

1
2
3
4
5
int dist_squared(int p1, int p2)
{
 int dif = p1 - p2;
 return abs((dif*dif));
}



Then we could create the function for setting all of the elements of the array like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void generate_circle(int radius_squared)
{

//calculate the centre of the circle
      int X_centre = X_ELEMENTS/2;
      int Y_centre = Y_ELEMENTS/2;

 for (int i=0; i<Y_ELEMENTS;i++)
 {
   for (int j=0; j<X_ELEMENTS;j++)
   {
      int x = j;
      int y = i;

 dist_squared_x =  dist_squared(X_centre, x);
 dist_squared_y =  dist_squared(Y_centre, y);

//now we can add them together to get the total distance between them squared
      int distance_squared = dist_squared_x + dist_squared_y;

//now we just check to see if they are within range.
       if(distance_squared<radius_squared )
       {
            shape_elements[i][j] = true;
       }
       else
      {
            shape_elements[i][j] = false;
       }

   }
 }
}



That would give us a solid black circle, but we want a ring, so we simply add a second radius, for the center and add that to the check:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void generate_circle(int radius_squared, int centre_radius_squared)
{

//calculate the centre of the circle
      int X_centre = X_ELEMENTS/2;
      int Y_centre = Y_ELEMENTS/2;

 for (int i=0; i<Y_ELEMENTS;i++)
 {
   for (int j=0; j<X_ELEMENTS;j++)
   {
      int x = j;
      int y = i;

 dist_squared_x =  dist_squared(X_centre, x);
 dist_squared_y =  dist_squared(Y_centre, y);

//now we can add them together to get the total distance between them squared
      int distance_squared = dist_squared_x + dist_squared_y;

//now we just check to see if they are within range.
       if(distance_squared>centre_radius_squared && distance_squared<radius_squared )
       {
            shape_elements[i][j] =true;
       }
       else
      {
            shape_elements[i][j] =false;
       }

   }
 }
}


That would just be my way of doing it, this code is completely untested but I really hope it helps.

Thanks -NGangst





Topic archived. No new replies allowed.