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.
| |
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.
| |
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
| |
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:
| |
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:
| |
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?
| |
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
| |
which does the same chore.
Last edited on
Equally for the second solution, I empathize the purpose of the code simply things like:
| |
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?
| |
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.
| |
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.
Source: https://www.cplusplus.com/forum/beginner/62634/
0 Response to "How to Draw a Circle in C++"
Publicar un comentario