


import turtle
import random as rand

def fillRectangle(t, x1, y1, x2, y2):

  turtle.setup(width=300, height= 300, startx=0, starty=0)
  ts = turtle.Screen() 
  ts.colormode(255)
  red = random.randint(0, 255)
  green = random.randint(0, 255)
  blue = random.randint(0, 255)
  t.pencolor(red, green, blue)
  t.fillcolor(red, green, blue)
  t.begin_fill()
  t.up()
  t.goto(x1, y1)
  t.down()
  t.goto(x2, y1)
  t.goto(x2, y2)
  t.goto(x1, y2)
  t.goto(x1, y1)
  t.end_fill()

def mondrian(t, x1, y1, x2, y2, level):
  if level == 0:
  	return



  """Draws a Mondrian-like painting at the given level."""
  if level > 0:
  	fillRectangle(t, x1, y1, x2, y2)
    
    splitX = rand.uniform(x1, x2)
    splitY = rand.uniform(y1, y2)
    
    mondrian(t, x1, y1, splitX, splitY, level - 1)
    mondrian(t, splitX, y1, x2, splitY, level - 1)
    mondrian(t, x1, splitY, splitX, y2, level - 1)
    mondrian(t, splitX, splitY, x2, y2, level - 1)
    
    


def main():
  level = int(input("Enter a level: "))
  t = turtle
  width = 200
  height = 200 
  mondrian(t, -width, height, width, -height, level)
  turtle.done()

if __name__ == "__main__":
main()

