Note: The instructions are a bit lengthy and picky. Please read all requirements, and do some thinking before you start coding. It’s also good practice to see if you can do some coding with pencil-and-paper before typing anything. (After all, exams will be pencil-and-paper, without any fancy software.) Once you understand exactly what you want to do, implement your code in piece-by-piece and test each piece as you go.
In this short assignment, you will write a function that allows you to draw “string art”: art made out of, you know, string. If you’ve never seen string art, a quick search of Google images will give some inspirational examples. Here’s what I came up with in my solution:
Here’s how real string art (in the physical world) works. You take two long thin pieces of wood. Let’s say they are yardsticks, with 36 inch marks on them. On each yardstick, push in 37 thumbtacks, at one-inch intervals along the stick (at inch 0, inch 1, inch 2, and so on, up through inch 36). Lay the sticks on the ground with the tacks facing up. Call the sticks A and B. Attach a taut piece of string from the tack 0 inches along stick A, to the tack 36 inches along stick B.
Then attach another string, this time from 1 inch along stick A, to 35 inches along stick B. And then another, from 2 inches along stick A, to 34 inches along stick A. Keep following this pattern until every tack on stick A is attached to a tack stick B by a piece of taut string.
Write a function that takes nine input parameters, in the following order:
You may name these parameters whatever you like. The function should draw string art using those two sticks as described above. For example, in my program, the actual parameters I passed to the function were 25, 50, 50, 200, 350, 180, 200, 350, 25. You may use whatever colors you like, but the strings and the sticks should not have the same color.
Draw everything on a black background, as I have done. You should draw the sticks with a stroke width of 3 pixels, but draw the strings with a stroke width of 1 pixel.
You do not have to make the string colors vary from string to string, but you are welcome to do so. If you want to change the string colors, a nice way to do it is to gradually change the red/green/blue values as you progress from one string to the next. (In my string art, I changed only the green and blue values.)
As you know, when you call start_graphics
, you pass it the name of a function as a parameter, and start_graphics
runs that function. One problem: the function passed to start_graphics
must take no parameters. In other words, if you were to call start_graphics(do_stuff)
, then do_stuff
must be a function that takes no parameters. But the function that draws the string art does not take no parameters; in fact, it takes nine of them. So you cannot pass this function’s name as a parameter when you call start_graphics
.
Here’s how you can solve this problem. Write a function main
that takes no parameters. All that main
does is call your nine-parameter function. Then pass main
as the parameter when you call start_graphics
.
You may optionally change the colors in some creative way as the string art is being drawn, by incrementally changing the red, green, and blue components of the current drawing color.
Do not forget to use beautiful coding style, with good comments, good use of whitespace, and meaningful variable names.
Simplify the problem and solve a small piece first.
It’s easy to draw the two sticks, right? Do that first and test your program.
Now you need to draw a line for a string. And then another line. Your goal is to write a while loop that computes the location of each line in sequence, and then draws that line. Before creating a while loop, you might see if you can draw the first two strings without a loop. Then remove that code and put it somewhere safe. (That was just for practice.)
For each piece of string, you’ll have to figure out the x- and y-coordinates of the line endpoints, where they are attached to the “tacks” on the sticks. Here’s how.
Let x1a
and y1a
be the coordinates of one end of stick A, and let x2a
and y2a
be the coordinates of the other end. For stick B, let the coordinates be x1b
, y1b
and x2b
, y2b
. The first string will go from (x1a, y1a)
to (x2b, y2b)
, and the last string will go from (x2a, y2a)
to (x1b, y1b)
.
Now suppose that you want to draw a string that is a fraction f
of the way from the first string, where f
is between 0 and 1, inclusive. In other words, where it hits stick A should be f
of the way from (x1a, y1a)
toward (x2a, y2a)
, and where it hits stick B should be f
of the way from (x1b, y1b)
toward (x2b, y2b)
. Then the x-coordinate of where it should hit stick A would be x1a + f * (x2a - x1a)
, and the x-coordinate of where it should hit stick B would be x1b + (1.0 - f) * (x2b - x1b)
. I’ll let you figure out the y-coordinates, but they’re a lot like the x-coordinates.
The fraction f
depends on which string you’re drawing. Let’s take a simple example. Suppose that you are to draw five strings. Then you want to compute coordinates five times, first for f
= 0, then for f = 0.25
, then for f
= 0.5, then for f
= 0.75, then for f
= 1.0. If instead you are drawing nine strings, you’ll use successive values of f
as 0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, and 1.0. I’ll let you figure out how to perform this calculuation, but notice that the change in f
from one string to the next is not exactly 1 over the number of strings. If it were 1 over the number of strings, then for five strings, f
would change by 0.2 from string to string, but the right change is 0.25.
Instead of going right to drawing the strings after drawing the sticks, you might want to first draw tacks at the attachment points on the sticks.
You should be able to write this program with one while-loop. I don’t mean that your program should consist of just one while-loop and nothing more. I mean that the number of while-loops in your program should be one. You also shouldn’t need any if-statements.
If you want to vary the colors of the strings, as I did, that fraction f
is useful when you calculate the red, green, and blue values.
The Python program for your string art.
A screenshot to show the output.
You may submit several programs and their corresponding outputs for different string arts. But only one program and its output are required.
Please remember in all assignments that editing the output of the program before printing it is a violation of the Academic Honor Principle. Make sure that the output you turn in comes from the program that you turn in. If you make any change to the code, no matter how insignificant you think it might be, rerun your program to produce new output.
Submit your code and output via Canvas.
The consequences of violating the Honor Code can be severe. Please always keep in mind the word and spirit of the Honor Code.