Skip to content

Instantly share code, notes, and snippets.

@codegod100
Created November 9, 2023 03:18
Show Gist options
  • Select an option

  • Save codegod100/34fd74f5a056926f2652c6359ae4d271 to your computer and use it in GitHub Desktop.

Select an option

Save codegod100/34fd74f5a056926f2652c6359ae4d271 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import turtle\n",
"\n",
"def koch_curve(t, length, depth):\n",
" if depth == 0:\n",
" t.forward(length)\n",
" else:\n",
" koch_curve(t, length / 3, depth - 1)\n",
" t.left(60)\n",
" koch_curve(t, length / 3, depth - 1)\n",
" t.right(120)\n",
" koch_curve(t, length / 3, depth - 1)\n",
" t.left(60)\n",
" koch_curve(t, length / 3, depth - 1)\n",
"\n",
"def koch_snowflake(t, length, depth):\n",
" for _ in range(3):\n",
" koch_curve(t, length, depth)\n",
" t.right(120)\n",
"\n",
"# Create a turtle object\n",
"t = turtle.Turtle()\n",
"t.speed(0) # Set the drawing speed (0 is the fastest)\n",
"\n",
"# Move the turtle to the starting position\n",
"t.penup()\n",
"t.goto(-200, 100)\n",
"t.pendown()\n",
"\n",
"# Draw the Koch snowflake\n",
"koch_snowflake(t, 400, 8)\n",
"\n",
"# Hide the turtle\n",
"t.hideturtle()\n",
"\n",
"# Keep the window open until it is closed manually\n",
"turtle.done()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "sol-pOKshOQH-py3.11",
"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.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment