How to Draw a Circle in C++

I want to create a circle using nested for-loops just like someone would create a diamond. And then far, I was able to get the post-obit using lots of for-loops:

                                  ***      *         *    *             *   *               *  *                 *  *                 *  *                 *  *                 *   *               *    *             *      *         *          *** Press any key to continue . . .              

My lawmaking is long but unproblematic. I couldn't figure out the algorithm for nested loops to create each line or amend yet, each half of the circle if its fifty-fifty possible. I would like some help/hint on how I can reduced my code. Here's office of my lawmaking, this is for the beginning line of the circle. The others are similar.

                        one
ii
iii
4
5
six
7
8
ix
10
11
12
13
fourteen
fifteen
16
17
18
xix
twenty
21
22
                                                  int                          main () {                          int                          i,j,k;                          for(i=0;i<9;i++) 	{ 		cout<<" "; 	}                          for(j=0;j<3;j++) 	{ 		cout<<"*"; 	}                          for(k=0;one thousand<nine;1000++) 	{  		cout<<" "; 	} 	 	cout<<endl; . . . .                      

Last edited on

I don't desire you lot guys to give me the answer but a hint. Also, is it even possible to create one-half the circle I made with a nested for-loops? Cheers!

allow'southward see... of the top of my head.

                        1
2
3
4
5
half-dozen
seven
eight
nine
ten
eleven
12
13
fourteen
xv
16
17
18
                                                  int                          circle_radius = x;                          // or whatever you want                          for                          (int                          i = 0; i <= two*circle_radius; i++) {                          for                          (int                          j = 0; j <= 2*circle_radius; j++)     {                          float                          distance_to_centre = sqrt((i - circle_radius)*(i - circle_radius) + (j - circle_radius)*(j - circle_radius));                          if                          (distance_to_centre > circle_radius-0.5 && distance_to_centre < circle_radius+0.5)         {              cout <<                          "*";         }                          else                          {              cout <<                          " ";         }     }     cout << endl; }                      

not run it but give that a go.

Edit: Tested information technology, seems to work.

Last edited on

Here'southward a better idea based on the fact that the console windows entries aren't square (i.e. 1 character occupies a non-foursquare infinite)

My one (and I suspect about peoples), is gear up to a height/width ratio of 4/3 so if y'all want a circumvolve you might want to describe an ellipse instead with semi-major axis = 4/3*semi-small-scale axis = iv/iii*circle_radius

                        1
2
three
4
5
vi
7
8
9
x
11
12
xiii
14
15
16
17
eighteen
19
20
21
                                                  int                          circle_radius = 10;                          // or any you want                          float                          console_ratio = 4.0/3.0;                          float                          a = console_ratio*circle_radius;                          float                          b = circle_radius;                          for                          (int                          y = -circle_radius; y <= circle_radius; y++)     {                          for                          (int                          x = -console_ratio*circle_radius; x <= console_ratio*circle_radius; x++)         {                          float                          d = (x/a)*(x/a) + (y/b)*(y/b);                          if                          (d > 0.90 && d < 1.1)             {                 cout <<                          "*";             }                          else                          {                  cout <<                          " ";             }         }         cout << endl;     }                      

Concluding edited on

Astonishing, thank you for your reply. As of correct now, I'm going over your solutions and trying to understand them first before I use them. First of all, in your get-go solution, I empathize everything upward until hither:

                        1
ii
3
4
5
6
                                                  int                          circle_radius = 10;                          // or whatever you want                          for                          (int                          i = 0; i <= ii*circle_radius; i++) {                          for                          (int                          j = 0; j <= ii*circle_radius; j++)     {                      

When I was trying to create a nested for-loop for the circumvolve, it would never occur to me to multiply the circle_radius past 2. More than importantly, it wouldn't occur to me to employ the circle_radius in the condition in the first place. I thought I had to do something like to this:

                        1
ii
3
4
v
6
7
8
ix
10
11
12
13
14
15
16
                                                  int                          i,j,chiliad;                          for( i = x; i >= 1; i-- ) 	{                          for( j = one; j < i; j++ ) 		{ 			cout <<                          " "; 		}                          for( k=10; chiliad >= j*ii; 1000-- ) 		{ 			cout <<                          "*"; 		}   		cout <<                          "\n"; 	}                      

This creates the upper function of a diamond I think. I idea I had to put weather condition like

i >= 1, j < i, and k >= j*2

I barely understand why those conditions were used for the upper part of the diamond so yous can imagine how I feel nearly the circle_radius. If you could explain a bit on why you utilize the circle_radius in the weather condition I would really appreciate it (I'one thousand debugging it using the Step Over function in VB so I can sympathize it). At present for the rest of that code, the if else statement surprised me. How did y'all go it?

                        1
two
3
4
5
6
vii
8
                                                  if                          (distance_to_centre > circle_radius-0.5 && distance_to_centre < circle_radius+0.5)         {              cout <<                          "*";         }                          else                          {              cout <<                          " ";         }                      

Pitiful if I'k asking a lot of questions, I merely really desire to empathise how to get a circle out of for-loops. Thanks again!

Sure.

Any point on a circumvolve is circle_radius abroad from the centre of the circle.

I just fabricated a loop than created a box effectually the circle and set the centre of the circle to exist the 'mid point' of the loop. Eastward.1000

- - - - -
- - - - -
- - c - -
- - - - -
- - - - -

where c is the middle and here radius would be 2. The loop goes row by row and check every entry.

I simply let the middle of the circle exist at (circle_radius, circle_radius) and the loop values represent the x and y values to check. (in the in a higher place example, centre of circle is at (2,ii) as you can see)

Calculate the distance to the centre of the circle using pythagoras formula.

Since we are using integers for the loop information technology won't be perfectly equal to the radius (which is what we want) so I set a tolerance of 0.5 either side.

EDIT:::::

You don't really need to practice that. Since we are working with ints we tin can make it and so that our distance to centre DOES equal exactly the radius.

Replace the float and if part with

                        1
2
3
4
5
6
7
8
9
                                                  int                          distance_to_centre = sqrt((i - circle_radius)*(i - circle_radius) + (j - circle_radius)*(j - circle_radius));                          if                          (distance_to_centre == circle_radius)             {                 cout <<                          "*";             }                          else                          {                  cout <<                          " ";             }                      

which does the same chore.

Last edited on

Equally for the second solution, I empathize the purpose of the code simply things like:

                        1
2
iii
four
v
6
                                                  float                          console_ratio = 4.0/3.0;                          float                          a = console_ratio*circle_radius;                          for                          (int                          y = -circle_radius; y <= circle_radius; y++)     {                          for                          (int                          ten = -console_ratio*circle_radius; 10 <= console_ratio*circle_radius; x++)                      

take me confuse. What'due south the purpose of the console_ratio? And, if you can, could yous explicate to me lines 6,eight,ten in your 2d solution?

                        1
2
3
4
5
6
7
8
nine
10
eleven
12
13
fourteen
15
16
17
18
nineteen
20
21
                                                  int                          circle_radius = 10;                          // or whatever you want                          bladder                          console_ratio = 4.0/three.0;                          float                          a = console_ratio*circle_radius;                          float                          b = circle_radius;                          for                          (int                          y = -circle_radius; y <= circle_radius; y++)     {                          for                          (int                          x = -console_ratio*circle_radius; 10 <= console_ratio*circle_radius; x++)         {                          float                          d = (x/a)*(x/a) + (y/b)*(y/b);                          if                          (d > 0.xc && d < ane.1)             {                 cout <<                          "*";             }                          else                          {                  cout <<                          " ";             }         }         cout << endl;     }                      

Also, why does the if-else statement has

status? Distressing once over again if I'm asking also many questions. Similar I said I really desire to understand this. Thank you!

Print this in the panel.

                        1
two
three
4
v
half-dozen
7
eight
                                                  for                          (int                          y = 0 y < 10; y++)     {                          for                          (int                          10 = 0; 10 < ten; x++)         {             cout <<                          'X';         }         cout << endl;     }                      

It's a 10x10 loop merely information technology prints a rectangle rather than a square (at least in my one) since my console is set to print characters that are 12 high and 8 broad (four/3 ratio).

Therefore if we print an ellipse with the same ratio the console character ratio should abolish with the ellipses ratio and it will look like a circle. Hard to explain but y'all're basically squeezing an ellipse to make information technology wait similar a circumvolve.

Every bit for the loops, I just inverse my coordinate system to be centred at (0,0), line ten is the equation of an ellipse and the d > 0.9... is the approximation to the equation for an ellipse shown hither...

http://www.mathopenref.com/coordgeneralellipse.html

Last edited on

Thank y'all for your time on explaining this in more than detail. I now accept a better understanding of how information technology works. Thanks a lot, and sorry for taking so long to answer been busy with school.

Topic archived. No new replies allowed.

knoxwasere.blogspot.com

Source: https://www.cplusplus.com/forum/beginner/62634/

0 Response to "How to Draw a Circle in C++"

Publicar un comentario

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel