+ 8

How to print a circle?

i don't know.but i think there are many master-hand can help me. if i know radius of circle,how can print a circle. language is not important,i need some idea how to get it. JAVA ,C OR CPP eg: ** * * * * * * * *

21st Mar 2018, 2:40 AM
wang
wang - avatar
6 Antworten
+ 11
Someone else may explain this way better, and due to time this is just the coordinates. Maybe someone can help with the graphing (or my related code will help). The circle formula: x² + y² = r² Assuming radius "r" is 3 (so r² is 9), we know that the far left side of the circle must be at -r and the far right side must be at +r ... For simplicity, here's what we want above the x axis: . . . . . . -3 -2 -1 0 1 2 3 Knowing "x" ranges from -3 to 3, this leaves just one variable to calculate, y. Rearrange the formula for y, then take the square root of each side: y² = r² - x² .... y = sqrt(9-x²) Now if you loop "x" from -3 to 3 you can easily calculate "y" (notice how at x=-3 and 3, y = 0 and at x=0, y=3). Since you're drawing a circle and dealing with squared values, it's fine to mirror every "+y" the same distance below the x axis too (you get the bottom arc automatically, for a similar reason you don't need to loop over y -- you get the left/right arcs automatically) And that's your coordinates. Drawing thoughts: ----------------------------------- You might imagine printing a 10x10 square array filled only with spaces. This big empty square is your "canvas", and you need to convert your coordinates (which range from -3 to 3 and are floats) to the array coordinates (which are all positive values starting at 0 or 1 and integers). Basically you shift your coordinates right and up (no more negatives), then put a "*" at that location in the array, then print your rectangular "canvas" like you printed the blank one. Here's a code (I'll replace it with one from CodePlayground if I find something more clear). https://code.sololearn.com/cFAi8v2r9Nd6/?ref=app
21st Mar 2018, 4:16 AM
Kirk Schafer
Kirk Schafer - avatar
+ 6
Wang, it looks like you took the calculation a step further and draw 4 points for each calculation, cutting the sweep in half. This is a nice optimization (and for any readers, that's this): ** ** ** +-------** -3 -2 -1 0 1 2 3 ... then for any (x,y) iterate all the sign combinations and automatically have the other three arcs: (-x, y) (x, -y) and (-x, -y) by sweeping the 90° from 0...3. There's another doubling optimization (8 points for 1 calc) by swapping (x,y) for (y,x) and then only sweeping 45° (where x and y cross each other): calculate (x,y) --> generate (x, -y) (-x, y) (-x, -y) --> generate (y, x) (y, -x) (-y, x) (-y, -x) Put another way, you optimized calculating from 0...diameter into 0...radius; swapping reduces this to 0...radius/2.
22nd Mar 2018, 12:08 AM
Kirk Schafer
Kirk Schafer - avatar
+ 3
language is not important,i need some idea how to get it.
21st Mar 2018, 2:46 AM
wang
wang - avatar
+ 3
Thank you,i learn a lot from your comments.In particular,I have referenced Mr Krik's idea to print a circle.This is code of mine which was implemented by Java. https://code.sololearn.com/cUNRl4IGnQE1/?ref=app
21st Mar 2018, 5:19 PM
wang
wang - avatar
+ 2
https://code.sololearn.com/WHQOyAwALS79/?ref=app Look there.
21st Mar 2018, 2:49 AM
Jax
Jax - avatar
+ 2
You can use Python Turtle Graphics, but not here.
21st Mar 2018, 2:52 AM
Dread
Dread - avatar