Comp 110 Recursion with Turtle Graphics

Recursion with Turtle Graphics

Recursion enhances our ability to make more nuanced and intricate art! Basically the idea is that you draw the same thing over and over again and use only a few simple lines of code to create something beautiful. Let's take a look at some examples!

Special spirals!

In lecture we went over the examples of drawing trees and a square spiral, but what if you wanted your spiral to be customizable? Here's a function that lets you make any sort of special spiral you want:

import { forward, left, right } from "introcs/turtle";
 
let specialSpiral = (sideLength: number, angle: number, scaleFactor: number, minLength: number): void => {
    if (sideLength <= minLength) {
        return;
    } 
    forward(sideLength);
    left(angle);
    specialSpiral(sideLength*scaleFactor, angle, scaleFactor, minLength);
};

It takes in 4 parameters: the initial sideLength of your spiral, the angle by which the spiral will be turning left, the factor by which the sideLength will decrease with every recursive call, and our minimum sideLength which acts as our base case. Here a few example calls to specialSpiral and the resulting art!

specialSpiral(150, 45, 0.95, 5);
specialSpiral(400, 15, 0.95, 5);
specialSpiral(20, 25, 0.99, 0.05);