# Turtle Graphics For this assignment, you will explore the python graphics package `turtle`, and experiment with this package by writing functions to draw different shapes. You will then use these functions (and more loops!) to draw a picture of your own design! | Part | Section | |---------------|-----------------------------------------------| | 1 (in-lab) | [Part 1: Exploring turtle](#part1) | | 1 (in-lab) | [Check-in](#checkin) | | 2 (lab/home) | [Part 2: Drawing pictures using turtle](#part2) | | 2 (lab/home) | [Submission Instructions](#submission) | ## Getting Started Create a new project named `TurtleGraphics` in the `CSCI051p-Workspace` folder that you created on your Desktop. *Double check that you are creating the project in the right place, or you will likely have trouble finding your files later.* Then download the [starter code](turtle_graphics.py). Copy the python file `turtle_graphics.py` from your starter code into the (recently created) `CSCI051p-Workspace/TurtleGraphics` folder. If you don't see it, ask PyCharm to rescan that folder by clicking the triangle next to that folder (on the left-side list) to close and re-open it. The newly added file (`turtle_graphics.py`) should now be visible. <a name="part1"></a> ## Part 1: Exploring turtle #### Turtle graphics Turtle graphics are a way of drawing an image by moving a turtle around the Cartesian plane. We can do this in Python by using the turtle module. There's online documentation available [here](http://docs.python.org/3/library/turtle.html). **Note:** Turtle module is usually installed along with the python package that you installed in the first lab. If it is not installed on your machine, please ask the lab instructor to help you install it manually. Take a look at section 24.1.2.1, titled "Turtle methods". Note that the word "method" is often used when doing object-oriented programming. We'll talk about object-oriented programming after Thanksgiving. At the moment that's not what we're doing, so you can think of these as functions. The following are a few that you might find useful (there are many others; feel free to explore!): `position` `setposition` `forward` `heading` `setheading` `left` `right` `penup` `pendown` `pencolor` `dot` `circle` `speed` `fillcolor` `begin_fill` `end_fill` Take a look at the descriptions of some of these functions online (note: being able to read and make sense of online documentation is a very useful skill! [python.org](https://docs.python.org/3.7/) will be your friend!). Figure out what the functions take as paramters, what they do as a side-effect, and what they return. As an alternative to looking at the online documentation, the help function takes as an argument the name of a function and outputs the docstring (i.e. function description). You can try using help in the Python console to get the documentation for some of the above functions. **Note:** At the top of your file, you must include the import statement for the turtle module: ``` from turtle import * ``` If you try to use help function to explore the `window_width`, the console output will look as follows: ``` >>> from turtle import * >>> help(window_width) Help on function window_width in module turtle: window_width() Return the width of the turtle window. Example: >>> window_width() 640 ``` #### Turtle example Consider the function `draw_spirograph` (defined below). What arguments does this function expect? How are they used? What does each line of code do? Try running this function with several different arguments (As a start, you could try n1 = 200, n2 = 170, pc = "red", and fc = "yellow", but you should try it with a variety of different values to make sure you understand what it does). ``` def draw_spirograph(n1,n2, pc, fc): pencolor(pc) fillcolor(fc) begin_fill() while True: forward(n1) left(n2) if abs(pos()) < 1: break end_fill() done() ``` #### Basic shapes Ok, now for some coding! `draw_triangle(length, color)` Write a method called `draw_triangle` that draws an equilateral triangle (i.e. a triangle with all three sides the same length) using the turtle's current position and orientation. Your function should take two parameters: `length` (an `int`) and `color` (a `str`). Your function should draw a triangle with sides of length `length` and color `color`. At the end of the function the turtle should be in the same position and orientation as when it started. You may assume the string for `color` is a valid string that can be passed to `pencolor` without error. For those rusty on geometry, the interior angles of an equilateral triangle are all 60 degrees, which means the angle between a straight line drawn from one side and the adjacent side is 120 degrees. `draw_hexagon_triangle(length, color)` Write a method called `draw_hexagon_triangle` that draws a hexagon using six equilateral triangles. Your function should take two parameters: the side length (`length`, an `int`) and a color (`color`, a `str`). It should then draw one of the hexagons shown as below (the exact picture comes from calling `draw_hexagon_triangle` three times with some other code in between). <img src="figures/hexagons3.png" alt="hexagons3" style="height:150px;"> At the start of the function the turtle should be at the center of the eventual hexagon. At the end of the function the turtle should be in the same position and orientation as when it started. Your function must call your `draw_triangle` function and it must do so within a for-loop. You may assume the string for `color` is a valid string that can be passed to `pencolor` without error. Test your functions by drawing the same picture (e.g., three hexagon triangles with different colors) as shown in the example above. You can do it by calling the `draw_hexagon_triangle` function in the `main_part1` function. After calling the `draw_hexagon_triangle` function, then hide the turtle, and finally call `done()`. <a name="checkin"></a> #### Checking In We'll double check your code, ask you a few questions about `turtle` and about the function `draw_spirograph`, and answer any questions you have. Then we'll giveyou your lab point(s). After that you should start working on Part 2. <a name="part2"></a> ## Part 2: Drawing Pictures using Turtle In part 2, you will implement the following four functions as described below, and the `main` functino which draws a picture of your own. #### 1. `draw_polygon(n,length)` This function takes two parameters: the number of sides (`n`, an `int`) and the length of each side (`length`, an `int`). It then draws a polygon of that size and shape by first going forward `length` amount before turning to the right (clockwise). The turtle should end facing in the same direction as when it started. Note that an n-sided polygon has n equal length edges and that the angle between a straight line drawn from one side and the adjacent side is 360/n. This function can be thought of as a generalization of the `draw_triangle` function you wrote in Part 1. Below are some polygons with differing numbers of sides and different side lengths. All can be drawn with the `draw_polygon` function. <img src="figures/polygons.png" alt="polygons" style="height:150px;"> #### 2. `draw_spiral(increment, deg, n)` This function takes three integers as parameters (`increment`, `deg`, `n`) and draws a spiral with `n` sides where each side is `increment` longer than the previous one and `deg` is the angle between succesive lines. In other words, the turtle starts at the center of the spiral and draws a line with a length `increment`. The turtle then turns `deg` degrees to the right and draws a line of length `2*increment`. It then turns another `deg` degrees to the right and draws a line of length `3*increment`. It continues until drawing a final line of length `nincrement`. The following image was generated by calling `draw_spiral(10,90,15)` <img src="figures/spiral.png" alt="spiral" style="height:150px;"> #### 3. `rotate_repeat(k,n,length,drawing)` This function takes 4 arguments: - `k`: an `int`, which represents the number of times to repeat the overall drawing - `n`: an `int`, which represents the number of sides - `length`: an `int`, which represents side length - `drawing`: this will be one of the other functions that you've written in the above sections, which take two parameters (number of sides and side length). It then repeats the `drawing` function `k` times, each time rotating `360/k` degrees. For example, the following are three images that are drawn by calling: ``` rotate_repeat(3,3,50,draw_polygon) rotate_repeat(6,3,50,draw_polygon) rotate_repeat(12,3,50,draw_polygon) ``` <img src="figures/rotate_repeat.png" alt="rotate_repeat" style="height:150px;"> And yes, this means that you can pass a function as an argument! #### 4: `random_circle()` This function has no parameters. Instead, when called it chooses a random location with coordinates of (x, y) on the screen (use `window_width` and `window_height`). It then draws a single filled circle of radius `10` at that location. The fill color of the circle must depend in some way on its location. For example, circles generated randomly on the right side of the screen might all be red and circles randomly generatd on the left side might all be blue. Alternatively, each of the 4 quadrants could have its own color. *Hint: you can create helper functions to set the random location for drawing the circles* You may want to take a look at the `color`, `pencolor` and `fillcolor` functions in the turtle module. If using a string to input the color, there's a list of accepted names [here](https://www.tcl.tk/man/tcl8.4/TkCmd/colors.htm). Going beyond: you may shade the circles so that they gradually change from one color to another depending on location! You may consider to use the RGB value as input for the `pencolor` and `fillcolor` function. You may consider to use the color mode of 255, e.g., `colormode(255)`. #### 5. `main` To put this all together, your program's `main` function must draw an image which: - contains at least 4 polygons of at least 2 different sizes and 2 different shapes - contains at least 3 spirals of different sizes and shapes - contains at least 20 random circles (generated using `random_circle) In addition, - your `main` function must call each of the 4 functions you defined at least once. - the 20 (at least) random circles must be generated inside a for loop. If you have developed a helper function for setting random positions, you can use it here in `main` to draw different components at random places. In the end, don't forget to hide turtle and call `done()` in your main. #### Details and Suggestions When you're all done you should have a working program that draws your picture. Incremental development in this case likely just means working on, and testing, one function at a time. #### Coding Style Make sure that your program is properly commented: * You should have comments at the very beginning of the file stating your name, course, assignment number and the date. * Each function should have an appropriate docstring, describing: - the purpose of the function - the types and meanings of each parameter - the type and meaning of the return value(s) * Include other comments as necessary to make your code clear In addition, make sure that you have used good style. This includes: * Following naming conventions, e.g. all variables and functions should be lowercase. * Using good (mnemonic) variable names. * Proper use of whitespace, including indenting and use of blank lines to separate chunks of code that belong together. For more detailed descriptions, please review the [Python Coding Style Guidelines](../../python_style.html). ## Part 3: Ethical Reflection ## Part 4: Feedback Create a file named `feedback.txt` that answers the usual questions: 1. How long did you spend on this assignment? 2. Any comments or feedback? Things you found interesting? Things you found challenging? Things you found boring? <a name="submission"></a> ## Submission For this lab you are required to submit two files: - `turtle_graphics.py` a python file that contains the implementation of all the required functions. - `ethics.txt` your short ethical reflection - `feedback.txt` a text file containing your feedback for this assignment. These should be submitted using [Gradescope](https://www.gradescope.com). Note that we reserve the right to give you no more than half credit if your files are named incorrectly and/or your function headers do not match the specifications (including names, parameter order, etc). Please double check this before submitting! ## Grade Point Allocations | Part | Feature | Value | |-----------|-------------------------------------------|-----| | Lab | Check-in | 3 | | | | | | Execution | `draw_polygon` | 4 | | Execution | `draw_spiral` | 4 | | Execution | `rotate_repeat` | 4 | | Execution | `random_circles` | 4 | | Execution | `main` contains at least 4 polygons | 2 | | Execution | `main` polygons have at least 2 sizes/shapes | 2 | | Execution | `main` contains at least 3 spirals | 2 | | Execution | `main` spirals are different size/shapes | 2 | | Execution | `main` contains at least 20 circles | 2 | | Execution | `main` calls each of the four functions at least once | 4 | | | | | | Style | Docstrings accurate, relevant, appropriate| 3 | | Style | Other comments accurate, relevant, appropriate | 2 | | Style | Good use of loops and conditionals. | 2 | | Style | Other good programming style (e.g., variables, whitespaces) | 2 | | Style | Misc | 2 | | | | | | Ethics | Completed ethical reflection file | 2 | | Feedback | Completed feedback file submitted | 2 |