circles in text in python

did this using a textbuffer (made from a list of strings) so it would play nice with idle:

# circle routine
# circle of circles routine
# 2016 mn
#### license: creative commons cc0 1.0 (public domain) 
#### http://creativecommons.org/publicdomain/zero/1.0/

# create a buffer of 40 x 20 (20 lines, 40 characters wide)
textarray = [" " * 40,] * 20

# set x, y position in buffer to s
def xyset(arr, x, y, s):
    buf = ""
    if y >= len(arr): return 1 
    try:
        for p in range(len(arr[y])):
            if p == x: 
                buf += s
            else:
                buf += arr[y][p]
        arr[y] = buf
    except:
        pass

from math import cos, sin
locx = 20 # horiz location of center of circle
locy = 10 # vert location of center of circle
r = 20    # radius

def cir(arr, locx, locy, r, depth):
    # not using for loop, as it only does integer steps 
    st = -3.14159
    fn = 3.14159

    step = 3.14159 / (r * 4)
    if depth > 1: step = 3.14159 * 2 / r * 4
    count = st
    while 1:
        x = int(cos(count) * r + locx)
        # r / 2 adjusts height for textmode; remove / 2 for gfx
        y = int(sin(count) * r / 2 + locy)
        try:
            if depth > 1:
                cir(arr, x, y, (r / 4), depth -1)
            else:    
                xyset(arr, x, y, "@")
        except: 
            pass
        count += step
        if count >= fn: break

# plot circle to text array
cir(textarray, locx, locy, r, 1)

# print array (idle needs a fixed-width font)
for x in textarray: print x

 

output:

           @@@@@@@@@@@@@@@@@@           
        @@@                  @@@        
     @@@                        @@@     
    @@                            @@    
  @@                                @@  
 @@                                  @@ 
@@                                    @@
@                                      @
@                                      @
@                                      @
@                                      @
@                                      @
@                                      @
@@                                    @@
 @@                                  @@ 
  @@                                @@  
    @@                            @@    
     @@@                        @@@     
        @@@                  @@@        
           @@@@@@@@@@@@@@@@@@
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s