### 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:

 ``123456`` `````` * * * * * * * * * * * *``````

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.

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:

 ``1234567`` ``````[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

 ``123456789101112131415`` ``````for (int i=0; i

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

 ``12345`` ``````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:

 ``123456789101112131415161718192021222324252627282930313233`` ``````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

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:

 ``123456789101112131415161718192021222324252627282930313233`` ``````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; icentre_radius_squared && distance_squared

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.