{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def distance(A, B):\n", " '''Taxicab distance between two tuples'''\n", " return abs(A[0]-B[0])+abs(A[1]-B[1])\n", "distance((0,0),(3,5))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# http://www.ipythonblocks.org/\n", "from ipythonblocks import BlockGrid\n", "import math" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "adaYellow=(245,225,52)\n", "adaPurple=(149,96,159)\n", "adaGreen=(161,200,84)\n", "adaCoral=(236,98,113)\n", "adaBlue=(108,184,231)\n", "adaOrange=(246,131,82)\n", "\n", "adaBlack=(9,20,8)\n", "adaGrey=(134,136,140)\n", "adaLightGrey=(211,212,211)\n", "adaWhite=(255,255,255)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import interact" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An interactive. For playing with the 'eccentricity' $e$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grid = BlockGrid(31,31,block_size=5,fill=(255,255,255))\n", "# Directrix is col=0\n", "d = 0\n", "# Focus is at row 0, col 1\n", "F = (5,16)\n", "def draw(e):\n", " for block in grid:\n", " distance_from_edge = block.col\n", " distance_from_focus = distance((block.col, block.row), F)\n", " if distance_from_edge == round(e*distance_from_focus):\n", " block.set_colors(*adaBlack)\n", " else:\n", " block.set_colors(*adaWhite)\n", " if block.col == d:\n", " block.set_colors(*adaPurple)\n", " if (block.col, block.row) == F:\n", " block.set_colors(*adaBlue)\n", " return grid\n", "interact(draw, e=(0,3,0.1));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To save a bunch of images" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grid = BlockGrid(31,31,block_size=5,fill=(255,255,255))\n", "# Directrix is col=0\n", "d = 0\n", "# Focus is at row 0, col 1\n", "F = (5,16)\n", "for i in range(0,21,1):\n", " e = i/10\n", " for block in grid:\n", " distance_from_edge = block.col\n", " distance_from_focus = distance((block.col, block.row), F)\n", " if distance_from_focus == round(e*distance_from_edge):\n", " block.set_colors(*adaBlack)\n", " else:\n", " block.set_colors(*adaWhite)\n", " if block.col == d:\n", " block.set_colors(*adaPurple)\n", " if (block.col, block.row) == F:\n", " block.set_colors(*adaBlue)\n", " grid.save_image(\"eccentricity_{}.png\".format(e))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.5.2" } }, "nbformat": 4, "nbformat_minor": 2 }