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


Python


C++
JAVA
PHP
SQL
Assignments

Summer24


drawMethods.py

# Useful methods that we can call in multiple scripts
# CAC, 8/5/24
from turtle import Turtle


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 with
    :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 with
    :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 + cellSize)
    turtle.pendown()
    turtle.goto(x + cellSize, y)

def drawCircle(turtle,x,y,radius,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 with
    :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)
    turtle.setheading(start)
    turtle.pendown()
    turtle.begin_fill()
    turtle.forward(radius)
    turtle.left(90)
    turtle.circle(radius,extent=extent)
    turtle.goto(x,y)
    turtle.end_fill()

def drawSquare(turtle,x,y,side):
    """
    Draw a square with side length side and lower left corner (x,y)
    :param turtle: The object to draw with
    :param x: x-coordinate of the lower left corner of the square
    :param y: y-coordinate of the lower left corner of the square
    :param side: the length of the sides of the square
    :return: nothing
    """
    turtle.penup()
    turtle.goto(x,y)
    turtle.pendown()
    turtle.begin_fill()
    turtle.setheading(0)
    for i in range(0, 4):
        turtle.forward(side)
        turtle.left(90)
    turtle.end_fill()