Assignment 4: String art

In this assignment, you will be creating a piece of virtual string art using functions in the cs1lib.py library.

To make real string art, you need a piece of thick cardboard, some thread, and some thumbtacks. You start by drawing the outline of the string art on the cardboard. You will then pin the thumbtacks on the outline at fixed intervals. The final art is created by using the thread to connect pairs of thumbtacks on the outline to form straight lines. Here is a video on how to make a string art that looks very similar to what you will be creating in this assignment: YouTube link

What to do and how to do it

Here is a link to the video that shows the output of my solution for this assignment: Panopto link

Here is how you will create your virtual string art:

  1. You will be using a square outline for your string art in this assignment. To create a square outline, call the start_graphics function to create a square graphics window. The four edges of the graphics window will be the outline of your string art.

  2. To simulate placing the thumbtacks along the outline, you will mark them along the outline using the draw_point function in cs1lib. The size of each point is defined by the set stroke width. To be able to see the points clearly, it is a good idea to increase the stroke width to 3 or 4 pixels before you draw the point.

  3. The distance between neighboring thumbtacks is defined by a constant value OFFSET defined in your program. For this value OFFSET, I recommend choosing a value that is a factor of your square window size. For example, in my video linked above for a square window size of 400 pixels, I have selected OFFSET as 20 pixels. Why do you think it matters?

  4. In place of a thread connecting a pair of thumbtacks, you will be using the draw_line function to draw a line between a pair of points on the outline.

  5. To select a pair of points, you first select a point (x, y). This point (x, y) is always paired with the point (y, WIN_SIZE - x), where WIN_SIZE is the length of the side of the square.

There is a small difference in the order in which you do things in real-world string art and virtual string art. Instead of placing all the points (thumbtacks) and then connecting the pairs of points (thumbtacks), you will draw a pair of points and draw the line between them. Also, we do not wrap the thread around the points.

Special instructions about the implementation

There are two methods to draw all the lines in your virtual string art:

  1. You can use a while loop and draw lines between all pairs of points in the art in one call to your main draw function (DO NOT DO THIS) or You can draw one line between one pair of points in each call to your main draw function.

  2. You will be implementing the second method. Here are some things you need to consider while drawing using the second method:

To keep the lines drawn in previous calls to the main draw function, you need to clear your background only the first time your main draw function is called. In each call to the main draw function, you will move the point (x, y) by OFFSET value, find the point to pair it with, and draw the line between the pair. How you move the point by OFFSET distance depends on which edge of the square the point is on. For example, if you are on the top edge of the square, you will add OFFSET to the x coordinate of the point, and if you are on the right edge of the square, you will add OFFSET to the y coordinate of the point. [Hint: Every time you turn around the corner of the square, the way you find the next point will change.]

Coding style

Pay attention to your coding style. Comment when required, use good names for variables and functions, and define constants when applicable. You are allowed to use helper functions for your main draw function.

What to submit:

Submit a zip file containing the following files:

  1. Your .py file with the Python program to draw the string art.
  2. Screenshot of the final output of your program.

Honor code reminder:

Please remember in all assignments that editing the output of the program before submitting it is a violation of the Academic Honor Principle. Make sure that the picture 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 a new picture!! The consequences of violating the Honor Principle can be severe. Please always keep in mind the word and spirit of the Honor Principle.