{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "

Sierpinski Gasket

\n", "

\n", " The fractal \"Sierpinski Gasket\" is generated according to the rules: \n", "

    \n", "
  1. \n", "Draw an equilateral triangle with:\n", "$\n", "\\mbox{vertex } 1\\colon (a_1,b_1);\\enspace \\mbox{vertex } 2\\colon\n", "(a_2,b_2);\\enspace \\mbox{vertex } 3\\colon (a_3,b_3).\n", "$\n", "
  2. \n", "
  3. Place a dot at random point $P=(x_0,y_0)$ within this\n", "triangle.
  4. \n", "
  5. Find next point by randomly selecting either $1$,\n", "$2$, or $3$:\n", "
  6. \n", "
      \n", "
    1. If $1$, place a dot halfway between $P$ and vertex 1.
    2. \n", "\n", "
    3. If $2$, place a dot halfway between $P$ and vertex 2.
    4. \n", "\n", "
    5. If $3$, place a dot halfway between $P$ and vertex 3.
    6. \n", "
    \n", "
  7. Repeat the process using the last dot as the new $P$.
  8. \n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "if (typeof Jupyter !== \"undefined\") { window.__context = { glowscript_container: $(\"#glowscript\").removeAttr(\"id\")};}else{ element.textContent = ' ';}" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "if (typeof Jupyter !== \"undefined\") {require.undef(\"nbextensions/vpython_libraries/glow.min\");}else{element.textContent = ' ';}" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "if (typeof Jupyter !== \"undefined\") {require.undef(\"nbextensions/vpython_libraries/glowcomm\");}else{element.textContent = ' ';}" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "if (typeof Jupyter !== \"undefined\") {require.undef(\"nbextensions/vpython_libraries/jquery-ui.custom.min\");}else{element.textContent = ' ';}" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "if (typeof Jupyter !== \"undefined\") {require([\"nbextensions/vpython_libraries/glow.min\"], function(){console.log(\"GLOW LOADED\");});}else{element.textContent = ' ';}" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "if (typeof Jupyter !== \"undefined\") {require([\"nbextensions/vpython_libraries/glowcomm\"], function(){console.log(\"GLOWCOMM LOADED\");});}else{element.textContent = ' ';}" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "if (typeof Jupyter !== \"undefined\") {require([\"nbextensions/vpython_libraries/jquery-ui.custom.min\"], function(){console.log(\"JQUERY LOADED\");});}else{element.textContent = ' ';}" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "\"\"\" From \"COMPUTATIONAL PHYSICS\" & \"COMPUTER PROBLEMS in PHYSICS\"\n", " by RH Landau, MJ Paez, and CC Bordeianu (deceased)\n", " Copyright R Landau, Oregon State Unv, MJ Paez, Univ Antioquia, \n", " C Bordeianu, Univ Bucharest, 2020. \n", " Please respect copyright & acknowledge our work.\"\"\"\n", "\n", "# Sierpin.py: Sierpinski gasket\n", "\n", "from vpython import * \n", "import numpy as np\n", "import random\n", "\n", "imax = 15000\n", "i = 0\n", "a1 = - 0.5 # (a3, b3)\n", "b1 = - 0.433 # /\\\n", "a2 = 0.5 # / \\\n", "b2 = - 0.433 # / \\\n", "a3 = 0.0 # / \\\n", "b3 = 0.433 # /________\\\n", "x = 0.0 # (a1, b1) (a2, b2)\n", "y = 0.3 # \n", "graph1 = display(width = 500, height = 500, title = 'Sierpinski Gasket', \n", " range = 0.5, background = vec(0.87, 0.93, 0.87) )\n", "\n", "for i in range(1, imax):\n", " r = random.random();\n", " if (r <= 1.0/3.0):\n", " x = 0.5 * (x + a1)\n", " y = 0.5 * (y + b1)\n", " else:\n", " if( r > 1.0/3.0 and r <= 2.0/3.0):\n", " x = 0.5*(x + a2)\n", " y = 0.5*(y + b2)\n", " else:\n", " x = 0.5 * (x + a3)\n", " y = 0.5 * (y + b3)\n", " xc = x\n", " yc = y\n", " curve(pos = [vec(xc,yc,0), vec(xc+0.002, yc,0)], color=color.red) # Thin line" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" } }, "nbformat": 4, "nbformat_minor": 2 }