Day 6

MP2 Launch

We’ll go over the next assignment together.

Submission Logistics

To clear up confusion about what to submit to Canvas, a URL for a Mini-Project may look like this: https://github.com/sd2020spring/ComputationalArt-yourname. We can find the file we’re looking to grade from there.

Similarly, A URL for a reading journal may look like: https://github.com/sd2020spring/reading-journal-yourname. And we can select the specific journal we’re looking to grade.

Reminder: the course policy page has a note that shows we understand that sickness happens. “At the end of the semester we will eliminate the largest late penalty that you have incurred on any single mini-project or toolbox (note that this doesn’t apply to things that need to be prepared specifically for a particular class such as project proposals)”

Select a story

We’ll take a poll to determine whether the class would like to hear about

MP2 Kickoff Survey

Please fill out the Reflection and Teaming Survey #1. This survey will help us get a sense of how SoftDes is going for you and help prepare you for the team-based projects that will be coming later in the course.

Reading Journal Review and Twisting Turtles

Find a person to pair with to discuss solutions to reading journal exercises, focusing on finding a person who did at least one of the exercises that you chose not to complete from section 4.12. First, discuss your approaches to exercises 1-4 in Chapter 4.3. Make sure to clear up any misconceptions that you may have. Practice refactoring by discussing the approaches taken in the book, then modify (refactor) the book’s code. Make sure that you and your partner are clear on the advantages of the refactored code.

For the exercises in 4.12, one partner should present a solution to one of the problems (that the other partner didn’t attempt), conveying both a high-level approach to the problem as well as the specific implementation in Python. The partner listening can offer suggestions for alternate code approaches or possible improvements (in efficiency, style, etc) that the other partner might consider, being sure to provide such feedback in a constructive manner.

With your RJ partner or a new nearby classmate, find a cool line drawing and try to replicate it with Python code (or at least part of it, using your own artistic license)!

Teleportation, Cloning, and Other Unethical Experiments on Turtles

A Turtle is a Python object, which we will learn more about in coming class sessions. Turtles have methods, which we can call to inspect or change their behavior. One trick that will be useful here, which you saw in shapes.py but may not have thought about much, is the speed() function. The speed() function can be used to speed up slowpoke Turtles. While it seems weird that a speedy turtle would have a speed of 0, in this case the input 0 is reserved for having the turtle go as fast as possible (remember, when in doubt, check the documentation).

import turtle
speedy = turtle.Turtle()
speedy.speed(0)

Other important Turtle methods include xcor() and ycor() position, and heading().

Read more about turtles here.

Because Turtles are simple creatures, mainly defined by their current position and heading, we can “clone” them by reading these values and using them to direct a new Turtle.

leo = turtle.Turtle()
# leo does some arbitrary drawing (e.g., makes a 45 degree angle)
leo.fd(100)
leo.lt(45)
leo.fd(100)

# Create a new Turtle with the same attributes as the first
don = turtle.Turtle()
don.penup()
don.setx(leo.xcor())
don.sety(leo.ycor())
don.setheading(leo.heading())
if leo.isdown():
    don.pendown()
# don.bandana_color = "purple" # TODO: Ninja functionality not yet implemented

Exercise: encapsulate this functionality in a clone function that takes a Turtle argument and returns a new Turtle with the same position and heading, leaving the original Turtle untouched.

Draw Parametric Curves

A parametric curve in 2D is defined by two functions, and (where is some parameter, which we can think of as time). If we imagine our turtle traveling along this curve, we can use calculus to compute various properties of our turtle’s motion. For instance, the speed of the turtle can be calculated as follows.

Further you can calculate, your turtle’s heading as follows.

Using these ideas you can program your turtle to draw arbitrary parametric curves. Suppose we want our turtle to draw a sine wave with amplitude of 10 pixels and a period of pixels. This path can be defined as follows.

Additionally, the heading and speed functions are defined as follows.

Challenge: write a Python program that takes as input a parametric curve and uses the turtle module to draw it.

Hint: the method presented above to compute the turtle’s heading has some numerical issues (e.g., when ). A better way to compute the heading is using the math.atan2 function. Suppose we have computed and stored it in a variable dx and we have computed and stored it in a variable dy, the heading (in radians) can be computed as math.atan2(dy, dx).

Once you can draw a parametric curve, you can draw some really cool stuff like Spirographs!