Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main


Python


C++
JAVA
PHP
SQL
Assignments

Turtle


048_Rough_Circles3.py

import random
import os
from palettes import randomPalette
from util import getTurtleAndScreen
# Rough Circles Version 2
# CAC, 4/26/2023

numPoints = 50
percent = .03

def roughCircle(turtle,x,y,radius):
    global points, percent
    turtle.penup()
    margin = radius*percent
    points = []
    for i in range(0,numPoints-1):
        turtle.goto(x,y)
        turtle.setheading(i*360/numPoints)
        turtle.forward(radius+random.uniform(-margin,margin))
        points.append(turtle.pos())

    turtle.goto(points[0])
    turtle.pendown()
    turtle.begin_fill()
    for p in points:
        turtle.goto(p)
    turtle.end_fill()


def Draw():
    global points, percent
    points = random.randint(20,180)
    percent = random.randint(1,200)/1000
    title = "Rough Circles 3 ("+str(points)+" "+str(percent)+")"
    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()