{ "cells": [ { "cell_type": "code", "execution_count": 66, "id": "b4ec8634-e16e-46bc-ac56-dbeb4f8c1b36", "metadata": {}, "outputs": [], "source": [ "angle = 270\n", "distance = 10\n", "axiom = \"F\"\n", "rules = {\"F\": \"F+F-F-F+F\"}\n", "iterations = 3\n", "\n", "def generate_l_system(axiom, rules, iterations):\n", " string = axiom\n", " for _ in range(iterations):\n", " new_string = \"\"\n", " for char in string:\n", " new_string += rules.get(char, char)\n", " string = new_string\n", " return string" ] }, { "cell_type": "code", "execution_count": 68, "id": "d24c5675-4949-4818-9813-fcb16fc0e46d", "metadata": {}, "outputs": [], "source": [ "import turtle\n", "\n", "9# Modify turtle movements\n", "def draw_l_system(turtle, instructions, angle, distance):\n", " stack = []\n", " for cmd in instructions:\n", " if cmd == 'F': turtle.forward(distance)\n", " elif cmd == '-':\n", " turtle.left(angle)\n", " elif cmd == '+':\n", " turtle.right(angle)\n", " elif cmd == '[':\n", " stack.append((turtle.position(), turtleheading()))\n", " elif cmd == ']':\n", " position, heading = stack.pop()\n", " turtle.penup()\n", " turtle.goto(position)\n", " turtle.setheading(heading)\n", " turtle.pendown()\n", " " ] }, { "cell_type": "code", "execution_count": 70, "id": "a5ed7c56-c548-404d-b6a5-bcb8ab68db33", "metadata": {}, "outputs": [ { "ename": "Terminator", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTerminator\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[70], line 12\u001b[0m\n\u001b[0;32m 9\u001b[0m pen\u001b[38;5;241m.\u001b[39mreset()\n\u001b[0;32m 10\u001b[0m \u001b[38;5;66;03m#pen.hideturtle()\u001b[39;00m\n\u001b[1;32m---> 12\u001b[0m \u001b[43mdraw_l_system\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpen\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ml_system_string\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mangle\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdistance\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 13\u001b[0m \u001b[38;5;66;03m#pen.update()\u001b[39;00m\n\u001b[0;32m 14\u001b[0m turtle\u001b[38;5;241m.\u001b[39mdone()\n", "Cell \u001b[1;32mIn[68], line 9\u001b[0m, in \u001b[0;36mdraw_l_system\u001b[1;34m(turtle, instructions, angle, distance)\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m cmd \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mF\u001b[39m\u001b[38;5;124m'\u001b[39m: turtle\u001b[38;5;241m.\u001b[39mforward(distance)\n\u001b[0;32m 8\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m cmd \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m-\u001b[39m\u001b[38;5;124m'\u001b[39m:\n\u001b[1;32m----> 9\u001b[0m \u001b[43mturtle\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mleft\u001b[49m\u001b[43m(\u001b[49m\u001b[43mangle\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m cmd \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m+\u001b[39m\u001b[38;5;124m'\u001b[39m:\n\u001b[0;32m 11\u001b[0m turtle\u001b[38;5;241m.\u001b[39mright(angle)\n", "File \u001b[1;32m~\\anaconda3\\lib\\turtle.py:1700\u001b[0m, in \u001b[0;36mTNavigator.left\u001b[1;34m(self, angle)\u001b[0m\n\u001b[0;32m 1681\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mleft\u001b[39m(\u001b[38;5;28mself\u001b[39m, angle):\n\u001b[0;32m 1682\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Turn turtle left by angle units.\u001b[39;00m\n\u001b[0;32m 1683\u001b[0m \n\u001b[0;32m 1684\u001b[0m \u001b[38;5;124;03m Aliases: left | lt\u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 1698\u001b[0m \u001b[38;5;124;03m 67.0\u001b[39;00m\n\u001b[0;32m 1699\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[1;32m-> 1700\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_rotate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mangle\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[1;32m~\\anaconda3\\lib\\turtle.py:3278\u001b[0m, in \u001b[0;36mRawTurtle._rotate\u001b[1;34m(self, angle)\u001b[0m\n\u001b[0;32m 3276\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m _ \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(steps):\n\u001b[0;32m 3277\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_orient \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_orient\u001b[38;5;241m.\u001b[39mrotate(delta)\n\u001b[1;32m-> 3278\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_update\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 3279\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_orient \u001b[38;5;241m=\u001b[39m neworient\n\u001b[0;32m 3280\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_update()\n", "File \u001b[1;32m~\\anaconda3\\lib\\turtle.py:2661\u001b[0m, in \u001b[0;36mRawTurtle._update\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 2659\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[0;32m 2660\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m screen\u001b[38;5;241m.\u001b[39m_tracing \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m-> 2661\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_update_data\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 2662\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_drawturtle()\n\u001b[0;32m 2663\u001b[0m screen\u001b[38;5;241m.\u001b[39m_update() \u001b[38;5;66;03m# TurtleScreenBase\u001b[39;00m\n", "File \u001b[1;32m~\\anaconda3\\lib\\turtle.py:2647\u001b[0m, in \u001b[0;36mRawTurtle._update_data\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 2646\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_update_data\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m-> 2647\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mscreen\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_incrementudc\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 2648\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mscreen\u001b[38;5;241m.\u001b[39m_updatecounter \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m 2649\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m\n", "File \u001b[1;32m~\\anaconda3\\lib\\turtle.py:1293\u001b[0m, in \u001b[0;36mTurtleScreen._incrementudc\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 1291\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m TurtleScreen\u001b[38;5;241m.\u001b[39m_RUNNING:\n\u001b[0;32m 1292\u001b[0m TurtleScreen\u001b[38;5;241m.\u001b[39m_RUNNING \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m-> 1293\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m Terminator\n\u001b[0;32m 1294\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_tracing \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m 1295\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_updatecounter \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n", "\u001b[1;31mTerminator\u001b[0m: " ] } ], "source": [ "l_system_string = generate_l_system(axiom, rules, iterations)\n", "\n", "screen = turtle.Screen()\n", "pen = turtle.Turtle()\n", "turtle.width(1)\n", "screen.clear()\n", "pen.clear()\n", "pen.reset()\n", "\n", "draw_l_system(pen, l_system_string, angle, distance)\n", "turtle.done()" ] }, { "cell_type": "code", "execution_count": null, "id": "b6163fee-4cf1-4d7d-9173-5080c74514c5", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:base] *", "language": "python", "name": "conda-base-py" }, "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.9.20" } }, "nbformat": 4, "nbformat_minor": 5 }