Exercise: Cut-out (Armin Hofmann)

Exercise: Cut-out (Armin Hofmann)

Create a program that generates forms following the rules that you can derive from these forms by Armin Hofmann

Form

As always in programming there are multiple solutions. Possible solutions are presented below but hidden. Please try on your own first. Of course there are a lot of things were many decisions are correct, for e.g. which document size to choose.

Rules

What rules can you identify?

  • the base form is a black circle

  • 1 or 2 cutouts are performed on every circle

  • if we imagine a grid of 3x3 cells, the cutouts are performed on the outer rows and cols, the center of the circle is untouched

  • the cutouts have a size of 1, 2 or 3 cells

Desription of the program

Describe the steps that your program should perform to apply the rules and generate a form.

  • draw a background

  • draw a black circle

  • decide randomly to do 1 or 2 cutouts

  • for each cut-out, decide if it’s on the left/right or top/bottom

  • define the widht and height of the cut-out

  • draw the cut-out on top of the circle (so it’s not really a cut-out, but that’s also possible)

Code

Try to write the code to perform the task. Remember that this may take some iterations of writing - executing - evaluating.

from flat import document, shape, rgb
from IPython.display import display, SVG
import random

def show(page):
    display(SVG(page.svg()))


size = 200
margin = 10
doc_size = size + margin*2
black = rgb(51, 47, 50)
bg = rgb(228, 230, 232)

ratio_cutout = 0.27

d = document(doc_size, doc_size, 'pt') # of course this is up to you
p = d.addpage()

# fill the whole document with a background color
background = shape().fill(bg).stroke(bg).rectangle(0, 0, doc_size, doc_size)
p.place(background)

# place the black circle in the center
c = shape().fill(black).nostroke().circle(size//2+margin, size//2+margin, size//2)
p.place(c)


# create one or 2 cutouts:
for i in range(random.choice([1, 2, 2])): # 2/3 chance for 2
    
    # pick row or col
    if random.choice([True, False]):
        # # row (x-axis)
        # start point
        x = random.choice([0, ratio_cutout, 1-ratio_cutout]) + margin
        # choose between top or bottom row
        y = random.choice([0, 1-ratio_cutout]) * size + margin
        # width and height
        width = random.choice([ratio_cutout, 1-ratio_cutout, 1]) * size
        height = ratio_cutout*size
        
    else:
        # col (y-axis)
        # choose between left or right col
        x = random.choice([0, 1-ratio_cutout]) * size + margin
        # start point
        y = random.choice([0, ratio_cutout, 1-ratio_cutout]) + margin
        # height and width
        height = random.choice([ratio_cutout, 1-ratio_cutout, 1]) * size
        width = ratio_cutout*size
        
    rect = shape().fill(bg).stroke(bg).rectangle(x, y, width, height)
    p.place(rect)

show(p)
_images/cut_out_9_0.svg