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


Python


C++
JAVA
PHP
SQL
Assignments

Turtle


drawMethods.py

# CAC, 3/2023
import random


def drawForwardSlash(turtle,x,y,cellSize):
    """
    Draw a slash from (x,y) in the lower left
    to the upper right in a square of size cellSize
    :param turtle: The object to draw on
    :param x: x-coordinate of lower left corner of slash
    :param y: y-coordinate of lower left corner of slash
    :param cellSize: The size of the box
    """
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    turtle.goto(x + cellSize, y + cellSize)

def drawBackwardSlash(turtle, x, y, cellSize):
    """
    Draw a slash from (x+cellSize,y) in the lower right
    to the upper left in a square of size cellSize
    :param turtle: The object to draw on
    :param x: x-coordinate of lower left corner of slash
    :param y: y-coordinate of lower left corner of slash
    :param cellSize: The size of the box
    """
    turtle.penup()
    turtle.goto(x + cellSize, y)
    turtle.pendown()
    turtle.goto(x, y + cellSize)

def drawCircle(turtle,x,y,radius,lineWidth=1,extent=360,start=0):
    """
    Draw a circle centered at (x,y) with overall radius of radius,
    line width of lineWidth, going around extent degrees starting
    at start degrees
    :param turtle: The object to draw on
    :param x: x-coordinate of the center of the circle
    :param y: y-coordinate of the center of the circle
    :param radius: radius of the circle
    :param lineWidth: width of the line around the circle
    :param extent: how many degrees of the circle to draw
    :param start: the starting angle of the circle.
    :return:
    """
    turtle.penup()
    turtle.goto(x,y-radius+lineWidth//2)
    turtle.setheading(0)
    turtle.circle(radius-lineWidth//2,extent=start)
    turtle.pendown()
    lw = turtle.pensize()
    turtle.pensize(lineWidth)
    turtle.circle(radius-lineWidth//2,extent=extent)
    turtle.pensize(lw)

# Added 3/15/23, CAC
# This version was replaced by the next one.
# This is the simple one. We did this first and then expanded it
# to add features that the next one has.
# def fillCircle(turtle, x, y, radius):
#     """
#     Draw a filled circle centered at (x,y) of radius radius.
#     :param turtle:
#     :param x:
#     :param y:
#     :param radius:
#     :return:
#     """
#     ps = turtle.pensize()
#     turtle.pensize(1)
#     turtle.penup()
#     turtle.goto(x, y - radius)
#     turtle.pendown()
#     turtle.setheading(0)
#     turtle.begin_fill()
#     turtle.circle(radius)
#     turtle.end_fill()
#     turtle.pensize(ps)

# Added 3/15/23, CAC
def fillCircle(turtle, x, y, radius,extent=360,start=0):
    """
    Draw a filled (partial) circle centered at (x,y)
    of radius radius.
    :param turtle:
    :param x:
    :param y:
    :param radius:
    :param extent: how many degrees of the circle to draw
    :param start: starting angle of the circle.
    :return:
    """
    ps = turtle.pensize()
    turtle.pensize(1)
    turtle.penup()
    turtle.goto(x+radius, y)
    turtle.setheading(90)
    turtle.circle(radius,extent=start)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(radius,extent=extent)
    turtle.goto(x,y)
    turtle.end_fill()
    turtle.pensize(ps)

# Added 3/15/23, CAC
def drawConcentricCircles(turtle, x, y, radius, number, colors,
                          extent=360, start=0,forceDifferentColors=False):
    """
    Draw number concentric (partial) circles centered at (x,y)
    of radius radius.
    :param turtle:
    :param x:
    :param y:
    :param radius:
    :param number: How many concentric circles to draw
    :param colors: What color palette to use
    :param extent: How many degrees of the circle
    :param start: Starting angle of the circle
    :param forceDifferentColors: Should each circle be a different color
    than the ones around it?
    :return:
    """
    color = random.choice(colors)
    for i in range(0,number):
        r =(number-i)*radius/number
        turtle.color(color)
        fillCircle(turtle,x,y,r,extent=extent,start=start)
        new_color = random.choice(colors)
        if(forceDifferentColors):
            while(color==new_color):
                new_color = random.choice(colors)
        color=new_color

def ithGradient(turtle,color1,color2,n,i):
    """
    :param turtle:
    :param color1:
    :param color2:
    :param n: number of shades of the colors
    :param i: a number between 0 and n-1 inclusive.
    :return:
    """
    turtle.pencolor(color1)
    r,g,b=turtle.pencolor()
    turtle.pencolor(color2)
    r2,g2,b2=turtle.pencolor()
    rr=r*(n-i-1)/(n-1)+r2*i/(n-1)
    gg=g*(n-i-1)/(n-1)+g2*i/(n-1)
    bb=b*(n-i-1)/(n-1)+b2*i/(n-1)
    return int(rr),int(gg),int(bb)



def drawGradientCircle(turtle, x, y, radius, number, color1, color2,
                          extent=360, start=0,forceDifferentColors=False):
    """
    Draw a gradient of circles from color1 to color2 centered at (x,y)
    of radius radius.
    :param turtle:
    :param x:
    :param y:
    :param radius:
    :param number: How many concentric circles to draw
    :param colors: What color palette to use
    :param extent: How many degrees of the circle
    :param start: Starting angle of the circle
    :param forceDifferentColors: Should each circle be a different color
    than the ones around it?
    :return:
    """
    for i in range(0,number):
        color = ithGradient(turtle, color1, color2, number, i)
        turtle.color(color)
        r =(number-i)*radius/number
        fillCircle(turtle,x,y,r,extent=extent,start=start)