{"id":990,"date":"2021-10-05T23:50:02","date_gmt":"2021-10-06T06:50:02","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=990"},"modified":"2024-11-15T09:08:14","modified_gmt":"2024-11-15T17:08:14","slug":"176-the-koch-snowflake-using-python-turtle","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/176-the-koch-snowflake-using-python-turtle\/","title":{"rendered":"#176 The Koch snowflake using python turtle"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"alignnone size-full wp-image-991\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex176.png?resize=422%2C452&#038;ssl=1\" alt=\"\" width=\"422\" height=\"452\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex176.png?w=422&amp;ssl=1 422w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex176.png?resize=280%2C300&amp;ssl=1 280w\" sizes=\"(max-width: 422px) 100vw, 422px\" \/><\/p>\n<p>#176 The Koch snowflake using python turtle<\/p>\n<pre class=\"lang:python decode:true \">import turtle\n# Set up Constants\nANGLES = [60, -120, 60, 0]\nSIZE_OF_SNOWFLAKE = 300\n\ndef get_input_depth():\n    depth = 9\n    return depth\n\ndef setup_screen(title, background='white', screen_size_x=640, screen_size_y=320, tracer_size=800):\n    print('Set up Screen')\n    turtle.title(title)\n    turtle.setup(screen_size_x, screen_size_y)\n    turtle.hideturtle()\n    turtle.penup()\n    turtle.backward(240)\n    # Batch drawing to the screen for faster rendering\n    turtle.tracer(tracer_size)\n    turtle.bgcolor(background) # Set the background colour of the screen\n\ndef draw_koch(size, depth):\n    if depth &gt; 0:\n        for angle in ANGLES:\n            draw_koch(size \/ 3, depth - 1)\n            turtle.left(angle)\n    else:\n        turtle.forward(size)\n\ndepth = get_input_depth()\nsetup_screen('Koch Snowflake (depth ' + str(depth) + ')', background='white', screen_size_x=420, screen_size_y=420)\n\n# Set foreground colours\nturtle.color('red')\n# Ensure snowflake is centred\nturtle.penup()\nturtle.setposition(-180,0)\nturtle.left(30)\nturtle.pendown()\n# Draw three sides of snowflake\nfor _ in range(3):\n    draw_koch(SIZE_OF_SNOWFLAKE, depth)\n    turtle.right(120)\n\n# Ensure that all the drawing is rendered\nturtle.update()\nprint('Done')\nturtle.done()\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>#176 The Koch snowflake using python turtle import turtle # Set up Constants ANGLES = [60, -120, 60, 0] SIZE_OF_SNOWFLAKE = 300 def get_input_depth(): depth = 9 return depth def setup_screen(title, background=&#8217;white&#8217;, screen_size_x=640, screen_size_y=320, tracer_size=800): print(&#8216;Set up Screen&#8217;) turtle.title(title) turtle.setup(screen_size_x, screen_size_y) turtle.hideturtle() turtle.penup() turtle.backward(240) # Batch drawing to the screen for faster rendering turtle.tracer(tracer_size) turtle.bgcolor(background) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2,13],"tags":[],"class_list":["post-990","post","type-post","status-publish","format-standard","hentry","category-python","category-turtle"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-fY","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":993,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/177-random-snowflakes-using-python-turtle\/","url_meta":{"origin":990,"position":0},"title":"#177 Random snowflakes using python turtle","author":"gantovnik","date":"2021-10-06","format":false,"excerpt":"#177 Random snowflakes using python turtle","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex177.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex177.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex177.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":996,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/178-fractal-tree-using-python-turtle\/","url_meta":{"origin":990,"position":1},"title":"#178 Fractal tree using python turtle","author":"gantovnik","date":"2021-10-06","format":false,"excerpt":"#178 Fractal tree using python turtle","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex178.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":987,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/175-turtle-graphics-in-python\/","url_meta":{"origin":990,"position":2},"title":"#175 Turtle graphics in python","author":"gantovnik","date":"2021-10-05","format":false,"excerpt":"#175 Turtle graphics in python","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex175.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex175.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex175.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":6026,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/11\/449-filled-polygon-with-koch-snowflake-in-python\/","url_meta":{"origin":990,"position":3},"title":"#449 Filled polygon with Koch snowflake in python","author":"gantovnik","date":"2024-11-15","format":false,"excerpt":"import matplotlib.pyplot as plt import numpy as np import os def koch_snowflake(order, scale=10): # Return two lists x, y of point coordinates of the Koch snowflake. def _koch_snowflake_complex(order): if order == 0: # initial triangle angles = np.array([0, 120, 240]) + 90 return scale \/ np.sqrt(3) * np.exp(np.deg2rad(angles) * 1j)\u2026","rel":"","context":"In &quot;math&quot;","block_context":{"text":"math","link":"https:\/\/gantovnik.com\/bio-tips\/category\/math\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/11\/ex499.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/11\/ex499.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/11\/ex499.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/11\/ex499.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/11\/ex499.png?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":349,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/circle-of-diamonds-using-turtle\/","url_meta":{"origin":990,"position":4},"title":"Circle of diamonds using turtle","author":"gantovnik","date":"2019-01-26","format":false,"excerpt":"\u00a0 \u00a0 \u00a0","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example56.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example56.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example56.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example56.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":269,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/circle-of-squares-using-turtle\/","url_meta":{"origin":990,"position":5},"title":"Circle of squares using turtle","author":"gantovnik","date":"2019-01-20","format":false,"excerpt":"\u00a0 \u00a0","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"figure51","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/figure51.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/figure51.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/figure51.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/990","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=990"}],"version-history":[{"count":2,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/990\/revisions"}],"predecessor-version":[{"id":6042,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/990\/revisions\/6042"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}