+ 3

How to create a circle

Can I create a circle using loop just like other patterns which are made in a similar way

27th Feb 2017, 1:08 AM
Yash Raghava
Yash Raghava - avatar
4 ответов
+ 7
def circle(r, p1, p2): r = int(r) if p1 == '': p1 = '::' if p2 == '': p2 = '##' side = r * 2 + 1 r1 = ( r - 0.5 ) ** 2 r2 = ( r + 0.5 ) ** 2 for row in range(side): out = '' for col in range(side): d = ( col - r ) ** 2 + ( row - r ) ** 2 if r1 < d < r2: out += p2 else: out += p1 print(out) r = input('Radius size of circle? ') p1 = input('Pattern for empty text-pixel ( default "::" )? ') p2 = input('Pattern for plain text-pixel ( default "##" )? ') print() circle(r,p1,p2)
27th Feb 2017, 12:14 PM
visph
visph - avatar
+ 4
Yeah... you can. What I would doing for that goal, would be to set an two dimentional array representing a bit-map initialized with a default value, and set the cells crossing the perimeter of the circle with another ( using property of circle that all dot of circle are so distance from center to dot equals radius ). Next, I simply print my concatenated lines... This should render a not perfect circle, as char aren't draw on a square pattern, but you can get near perfect by using monospaced font ( generally the case in console mode ) and two chars for a 'text-pixel' ( correct almost the deformation ). Else, you may improve your algorithm for drawing circle in your custom bit-map array by kept in account the pixel ratio ^^
27th Feb 2017, 8:59 AM
visph
visph - avatar
+ 3
If you don't want use a bit-map buffer, and do the trick just using loop and drawing line after line, you can also... You just may adapt the algoritm for drawing the 2 dots that cross circle with horizontal line: a 2D ray tracing ( known as rastering ), but you can also keep the radius algorithm...
27th Feb 2017, 9:02 AM
visph
visph - avatar
21st Jul 2020, 1:14 AM
shubham kumar
shubham kumar - avatar