{ "cells": [ { "cell_type": "markdown", "id": "2937b253", "metadata": {}, "source": [ "# Turtle Graphics (optional)" ] }, { "cell_type": "markdown", "id": "1640271a", "metadata": { "slideshow": { "slide_type": "-" }, "tags": [ "remove-cell" ] }, "source": [ "**CS1302 Introduction to Computer Programming**\n", "___" ] }, { "cell_type": "markdown", "id": "8cb93038", "metadata": {}, "source": [ "In this notebok, we will demonstrate the benefit of code reuse with [`turtle`](https://docs.python.org/3/library/p.t.html), which is a module for drawing graphics." ] }, { "cell_type": "markdown", "id": "460df66f", "metadata": {}, "source": [ "## Start to move the turtle" ] }, { "cell_type": "markdown", "id": "637e297b", "metadata": {}, "source": [ "Because Jupyter notebook does not fully support `turtle` (the [`Tkinter` GUI toolkit](https://docs.python.org/3/library/tkinter.html)), we will use `Thonny` under the Linux `Desktop` instead." ] }, { "cell_type": "markdown", "id": "68f53182", "metadata": {}, "source": [ "**How to start a python script in `Thonny`?**" ] }, { "cell_type": "markdown", "id": "e2e13cc7", "metadata": {}, "source": [ "- Open a `Desktop` from the Launcher.\n", "- Open `Thonny` via `Application->Development->Thonny` from the menu at the top left corner.\n", "- Create a new Python script via `File->New`.\n", "- Save the file using `File->Save as...` to `cs1302/Lab3b/myscript.py` under your home directory." ] }, { "cell_type": "markdown", "id": "a4d824f9", "metadata": {}, "source": [ "````{note}\n", "\n", "- You may also use Xpra on Windows/Mac/Linux, in which case you can run Thonny from the menu item `Start->Applications->Thonny`. For mobile devices or devices with limited memory, `Desktop` is recommended.\n", "- A python script ends with the extension `.py`.\n", "- `myscript.py` should be placed in the same folder as your Lab3b for the later code to work.\n", "\n", "````" ] }, { "cell_type": "markdown", "id": "42b4f5a8", "metadata": {}, "source": [ "**How to move the turtle?**" ] }, { "cell_type": "markdown", "id": "e8cca3e5", "metadata": {}, "source": [ "Type the following in your python script to import a turle and instruct it to move forward `100` steps:\n", "\n", "```Python\n", "import turtle as t\n", "\n", "t.forward(100)\n", "t.exitonclick()\n", "```" ] }, { "cell_type": "markdown", "id": "9f56d714", "metadata": {}, "source": [ "To get the turtle moving, press `F5` or click the green play button in the toolbar:\n", "\n", "![Forward](images/forward.dio.svg)" ] }, { "cell_type": "markdown", "id": "6397575f", "metadata": {}, "source": [ "To exit, simply click on the display window." ] }, { "cell_type": "markdown", "id": "7a2ee45a", "metadata": {}, "source": [ "## Draw polygons" ] }, { "cell_type": "markdown", "id": "b5b0624c", "metadata": {}, "source": [ "**How to have the turtle draw a triangle?**" ] }, { "cell_type": "markdown", "id": "65f7da26", "metadata": {}, "source": [ "The following code draws a red equilateral triangle:" ] }, { "cell_type": "markdown", "id": "2c7076a7", "metadata": {}, "source": [ "```Python\n", "t.fillcolor('red')\n", "t.begin_fill()\n", " \n", "t.forward(100)\n", "t.left(120)\n", "t.forward(100)\n", "t.left(120)\n", "t.forward(100)\n", "t.left(120)\n", " \n", "t.end_fill()\n", "t.exitonclick()\n", "```" ] }, { "cell_type": "markdown", "id": "4cd3f06d", "metadata": {}, "source": [ "`t.left(120)` rotates the turtle to the left by `120` degrees." ] }, { "cell_type": "markdown", "id": "7c80df51", "metadata": {}, "source": [ "**Exercise** (Optional) The code above is quite *repetitive*. Shorten the code using a for loop and run it using your python script." ] }, { "cell_type": "markdown", "id": "137dcc93", "metadata": {}, "source": [ "**How to draw a square, a regular pentagon, or even any regular polygon?**" ] }, { "cell_type": "markdown", "id": "d55a62e2", "metadata": {}, "source": [ "Instead of writing a new script for each polygon, we can write a function:\n", "\n", "```Python\n", "def polygon(n, edge, color):\n", " \"\"\"Draw a colored polygon.\n", "\n", " A function using turtle to draw a polygon \n", " with arbitrary color and number of edges.\n", "\n", " Parameters\n", " ----------\n", " n: int\n", " number of edges.\n", " edge: int\n", " length of each edge.\n", " color: string\n", " color of the polygon\n", " \"\"\"\n", " ...\n", "```" ] }, { "cell_type": "markdown", "id": "812b281c", "metadata": {}, "source": [ "**Exercise** (Optional) Complete the function `polygon` and call it to draw a `'blue'` pentagon with length `50`." ] }, { "cell_type": "markdown", "id": "036f7158", "metadata": {}, "source": [ "````{hint}\n", "\n", "For a $n$-sided polygon, the turtle should turn by an angle of $\\frac{360}{n}$ at each corner of the polygon. E.g., for the equilateral triangle with `n=3`, the turning angle should be $\\frac{360}3=120$.\n", "\n", "````" ] }, { "cell_type": "markdown", "id": "68527e15", "metadata": {}, "source": [ "## Tesselation" ] }, { "cell_type": "markdown", "id": "cdd2ea14", "metadata": {}, "source": [ "**Tessellation problem**" ] }, { "cell_type": "markdown", "id": "32dbb3c2", "metadata": {}, "source": [ "[Tessellation](https://en.wikipedia.org/wiki/Tessellation) refers to the problem of using geometric shapes to tile a flat plane. E.g., [rhombitrihexagonal tiling](https://en.wikipedia.org/wiki/Rhombitrihexagonal_tiling) is a beautiful tessellation often used by temples and museums. Each hexagon is surrounded by 6 triangles and 6 squares." ] }, { "cell_type": "markdown", "id": "a8ec0e4f", "metadata": {}, "source": [ "
\n", "\"Rhombitrihexagonal\n", "
Rhombitrihexagonal tiling
\n", "
\n", "
" ] }, { "cell_type": "markdown", "id": "355a03f8", "metadata": {}, "source": [ "The following code draws:\n", "\n", "![Rhombitrihexagon](images/rhombitrihexagon.dio.svg)\n", " \n", "``` Python\n", "from polygons import *\n", "\n", "t.speed('fast')\n", "edge = 50\n", "\n", "t.left(180)\n", "hexagon(edge, 'red')\n", "t.right(180)\n", "\n", "for _ in range(6):\n", " t.left(30)\n", " triangle(edge, 'yellow')\n", " t.right(90)\n", " square(edge, 'purple')\n", " t.forward(edge)\n", " \n", "t.hideturtle()\n", "t.exitonclick()\n", "```" ] }, { "cell_type": "markdown", "id": "04bfa472", "metadata": {}, "source": [ "````{tip}\n", "\n", "- The module `polygons` implements the functions `triangle`, `square`, `hexagon`, as well as the more general function `polygon`.\n", "- `t.speed('fast')` speeds up the turle. You can also use `'fastest'`.\n", "- `t.hideturtle()` hides the turtle.\n", "\n", "````" ] }, { "cell_type": "markdown", "id": "1da5720f", "metadata": {}, "source": [ "**Exercise** Modify the above code to draw the following pattern:\n", "\n", "![Rhombitrihexagons](images/rhombitrihexagons.dio.svg)\n", "\n", "Challenge yourself making the code as efficient as possible, e.g., by avoiding double drawing." ] } ], "metadata": { "jupytext": { "text_representation": { "extension": ".md", "format_name": "myst", "format_version": 0.13, "jupytext_version": "1.10.3" } }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" }, "source_map": [ 12, 16, 21, 25, 29, 33, 37, 44, 54, 58, 69, 75, 79, 83, 87, 91, 108, 112, 116, 120, 143, 147, 155, 159, 163, 167, 175, 202, 212 ] }, "nbformat": 4, "nbformat_minor": 5 }