import math
import random
import os
from palettes import randomPalette
from util import getTurtleAndScreen
# Rough Circles
# CAC, 4/26/2023
def roughCircle(turtle,x,y,radius):
    startAngle = random.randint(0,359)
    turtle.penup()
    turtle.goto(x,y)
    turtle.setheading(startAngle)
    turtle.forward(radius)
    turtle.pendown()
    c = 2*math.pi*radius
    dist = c/360
    turtle.begin_fill()
    for i in range(0,360):
        maxAngle = 20
        angle = random.randrange(-maxAngle,maxAngle)
        turtle.setheading(startAngle+90+i+angle)
        turtle.forward(dist)
    turtle.end_fill()


def Draw():
    title = "Rough Circles"
    width = 800
    height = 600
    cellSize = 200
    numCols = width // cellSize
    numRows = height // cellSize

    filename = os.path.basename(__file__)[0:-3]
    turtle, screen = getTurtleAndScreen(title,width,height,filename,moveWorld=True)

    colors = randomPalette()
    bg = random.choice(colors)
    screen.bgcolor(bg)
    if(random.randint(0,1)==1):
        colors.remove(bg)

    lineWidth = 2
    turtle.pensize(lineWidth)

    for row in range (0,numRows):
        for col in range(0,numCols):
            radius = cellSize//2
            x = col*cellSize + radius
            y = row*cellSize + radius
            color=random.choice(colors)
            turtle.color(color)
            roughCircle(turtle,x,y,radius)

    turtle.penup()
    turtle.goto(0,0)

    screen.mainloop()
# ----------------------------------------------------------------
if __name__ == '__main__':
   Draw()
