Javascript required
Skip to content Skip to sidebar Skip to footer

Drawing a Circle With Asterisks

Hi at that place, I am VERY new to coding and I am having problem amalgam a basic circle with asterisks. I need to practice it using loops but I am struggling. The circumvolve is meant to expect like this:

                      1
2
3
iv
5
6
                                              *  *     *      *    *        *    *        *     *      *       *  *                    

It would be great for a flake of aid, even if it merely for the first 2 lines so I tin can get a better thought on how to go on. Thank you everyone

I have seen that one, but the fact that information technology prints three stars in the outset line throws me off.
I dont actually understand how I would be able to print a star, two spaces, and then a star using a loop.

I am using the lawmaking in the person's post that prints the kickoff line simply I cant effigy out what to change to go it to look like the circle i want

Last edited on

Sorry I wish I could exist more assistance.. I was just pointing out what I establish. Seems to be an interesting trouble (that I take not yet encountered) and it seems at that place is very simple solutions and very circuitous. Reminds me of when I was looking at random number generation. Information technology seems this is the simpler of the two sides of the complication. promise y'all get a solid solution. And if you do would you exist kind enough to post your solution here?

EDIT: I attempted to deconstruct the problem, and felt lost immediately as well.

Terminal edited on

Thanks for trying, my friend is going to aid me with the solution this afternoon, and I will post it here. I think information technology is i of those issues that is quite like shooting fish in a barrel, I am merely a little clueless with C++ every bit I started my course about vi weeks ago.

Howdy, AdehhRR

If I were going to do this, I would split the logic for defining the circle and the logic for drawing and treat them every bit two unlike problems.

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

                      1
ii
3
4
five
six
vii
                      [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

                      ane
2
three
4
5
6
seven
8
9
10
eleven
12
13
xiv
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; }                    

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

So the problem becomes populating the array in the start place.

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

the distance is simply some maths

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

And so we could create the function for setting all of the elements of the assortment like this:

                      1
2
iii
4
5
6
seven
8
nine
10
11
12
13
fourteen
fifteen
16
17
18
xix
20
21
22
23
24
25
26
27
28
29
thirty
31
32
33
                                              void                        generate_circle(int                        radius_squared) {                        //summate the centre of the circumvolve                        int                        X_centre = X_ELEMENTS/ii;                        int                        Y_centre = Y_ELEMENTS/two;                        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, ten);  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;                        //at present we just bank check to see if they are within range.                        if(distance_squared<radius_squared )        {             shape_elements[i][j] =                        true;        }                        else                        {             shape_elements[i][j] =                        imitation;        }     }  } }                    

That would requite us a solid blackness circle, only we want a ring, so we simply add a second radius, for the heart and add that to the check:

                      i
ii
three
4
five
half dozen
7
8
9
10
11
12
13
14
xv
16
17
xviii
nineteen
xx
21
22
23
24
25
26
27
28
29
30
31
32
33
                                              void                        generate_circle(int                        radius_squared,                        int                        centre_radius_squared) {                        //summate the centre of the circle                        int                        X_centre = X_ELEMENTS/ii;                        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 full distance between them squared                        int                        distance_squared = dist_squared_x + dist_squared_y;                        //now nosotros simply cheque to see if they are inside 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 information technology helps.

Thanks -NGangst

thorpebefookeery.blogspot.com

Source: http://www.cplusplus.com/forum/beginner/110133/