{"id":6026,"date":"2024-11-15T06:03:46","date_gmt":"2024-11-15T14:03:46","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=6026"},"modified":"2024-11-15T06:07:30","modified_gmt":"2024-11-15T14:07:30","slug":"449-filled-polygon-with-koch-snowflake-in-python","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2024\/11\/449-filled-polygon-with-koch-snowflake-in-python\/","title":{"rendered":"#449 Filled polygon with Koch snowflake in python"},"content":{"rendered":"<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2024\/11\/449-filled-polygon-with-koch-snowflake-in-python\/ex499\/\" rel=\"attachment wp-att-6038\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/11\/ex499.png?resize=1080%2C1080&#038;ssl=1\" alt=\"\" width=\"1080\" height=\"1080\" class=\"alignnone size-full wp-image-6038\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/11\/ex499.png 1200w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/11\/ex499-980x980.png 980w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/11\/ex499-480x480.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1200px, 100vw\" \/><\/a><\/p>\n<pre class=\"lang:python decode:true\">import matplotlib.pyplot as plt\nimport numpy as np\nimport os\n\n\ndef koch_snowflake(order, scale=10):\n#    Return two lists x, y of point coordinates of the Koch snowflake.\n    def _koch_snowflake_complex(order):\n        if order == 0:\n            # initial triangle\n            angles = np.array([0, 120, 240]) + 90\n            return scale \/ np.sqrt(3) * np.exp(np.deg2rad(angles) * 1j)\n        else:\n            ZR = 0.5 - 0.5j * np.sqrt(3) \/ 3\n\n            p1 = _koch_snowflake_complex(order - 1)  # start points\n            p2 = np.roll(p1, shift=-1)  # end points\n            dp = p2 - p1  # connection vectors\n\n            new_points = np.empty(len(p1) * 4, dtype=np.complex128)\n            new_points[::4] = p1\n            new_points[1::4] = p1 + dp \/ 3\n            new_points[2::4] = p1 + dp * ZR\n            new_points[3::4] = p1 + dp \/ 3 * 2\n            return new_points\n\n    points = _koch_snowflake_complex(order)\n    x, y = points.real, points.imag\n    return x, y\n\ncurrent_dir = os.getcwd()\nprint(current_dir)\nos.path.dirname(os.path.abspath(__file__))\nscript_dir = os.path.dirname(os.path.abspath(__file__))\nfile_path = os.path.join(script_dir, \"ex449.png\")\n\nx, y = koch_snowflake(order=5)\nplt.figure(figsize=(8, 8))\nplt.axis('equal')\nplt.fill(x, y)\nplt.savefig(file_path, dpi=150)\nplt.show()<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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) else: ZR = 0.5 &#8211; [&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":[56,69,2],"tags":[70],"class_list":["post-6026","post","type-post","status-publish","format-standard","hentry","category-math","category-matplotlib","category-python","tag-matplotlib"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-1zc","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":990,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/176-the-koch-snowflake-using-python-turtle\/","url_meta":{"origin":6026,"position":0},"title":"#176 The Koch snowflake using python turtle","author":"gantovnik","date":"2021-10-05","format":false,"excerpt":"#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='white', screen_size_x=640, screen_size_y=320, tracer_size=800): print('Set up Screen') turtle.title(title) turtle.setup(screen_size_x, screen_size_y) turtle.hideturtle() turtle.penup() turtle.backward(240) # Batch drawing to the screen\u2026","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\/ex176.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":151,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/direction-fields\/","url_meta":{"origin":6026,"position":1},"title":"Direction fields","author":"gantovnik","date":"2019-01-07","format":false,"excerpt":"","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example32","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example32.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example32.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example32.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2154,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/420-histogram-plot-of-10000-random-samples-from-the-normal-distribution\/","url_meta":{"origin":6026,"position":2},"title":"#420 Histogram plot of 10000 random samples from the normal distribution","author":"gantovnik","date":"2024-05-03","format":false,"excerpt":"","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex420.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex420.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex420.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1182,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/204-mandelbrot-fractal-using-python-2-2\/","url_meta":{"origin":6026,"position":3},"title":"#206 Triangulation using matplotlib","author":"gantovnik","date":"2021-11-26","format":false,"excerpt":"","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\/11\/ex206.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2065,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/408-animated-line-plot-in-python\/","url_meta":{"origin":6026,"position":4},"title":"#408 Animated line plot in python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"","rel":"","context":"In &quot;animation&quot;","block_context":{"text":"animation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/animation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex408-1.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex408-1.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex408-1.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":154,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/inexact-solutions-to-odes\/","url_meta":{"origin":6026,"position":5},"title":"Inexact solutions to ODEs","author":"gantovnik","date":"2019-01-08","format":false,"excerpt":"\u00a0 import os import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl import sympy from IPython.display import display sympy.init_printing() mpl.rcParams['text.usetex'] = True import sympy os.chdir(r'D:\\projects\\wordpress\\ex33') os.getcwd() def plot_direction_field(x, y_x, f_xy, x_lim=(-5, 5), y_lim=(-5, 5), ax=None): f_np = sympy.lambdify((x, y_x), f_xy, 'numpy') x_vec = np.linspace(x_lim[0], x_lim[1], 20) y_vec\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example33","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example33.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example33.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example33.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/6026","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=6026"}],"version-history":[{"count":4,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/6026\/revisions"}],"predecessor-version":[{"id":6036,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/6026\/revisions\/6036"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=6026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=6026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=6026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}