{ "metadata": { "name": "ipythonblocks_fun" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Playing with ipythonblocks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For more on `ipythonblocks` see the home page at [https://github.com/jiffyclub/ipythonblocks](https://github.com/jiffyclub/ipythonblocks)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from ipythonblocks import BlockGrid" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "import math" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "w = 100\n", "h = 100\n", "r = 35\n", "\n", "grid = BlockGrid(w, h, block_size=4)\n", "\n", "for block in grid:\n", " val = math.fabs(math.sqrt((w / 2. - block.col) ** 2 + (h / 2. - block.row) ** 2) - r) ** 1.5 / r * 255\n", " block.red = val\n", " block.green = val\n", " block.blue = val\n", "\n", "grid.show()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "grid = BlockGrid(w, h, block_size=4)\n", "\n", "for block in grid:\n", " block.red = 255 * float(w - block.col) / w\n", " block.green = 255 * float(h - block.row) / h\n", " block.blue = 255 * block.col / w\n", "\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "output_type": "pyout", "prompt_number": 4, "text": [ "" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "w = 40\n", "h = 40\n", "\n", "grid = BlockGrid(w, h)\n", "\n", "for block in grid:\n", " block.size = min(abs(w / 2 - block.col), abs(h / 2 - block.row))\n", "\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "output_type": "pyout", "prompt_number": 5, "text": [ "" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "w = 40\n", "h = 40\n", "\n", "grid = BlockGrid(w, h)\n", "\n", "for block in grid:\n", " block.size = min(abs(w / 2 - block.col), abs(h / 2 - block.row))\n", " \n", " if block.row < h / 2 and block.col < w / 2:\n", " val = max(block.row, block.col) / (w / 2.) * 255\n", " block.set_colors(val, val, val)\n", " \n", " elif block.row < h / 2 and block.col >= w / 2:\n", " val = max(block.row, h - block.col - 1) / (w / 2.) * 255\n", " block.set_colors(val, val, 255)\n", " \n", " elif block.row >= h / 2 and block.col < w / 2:\n", " val = max(w - block.row - 1, block.col) / (w / 2.) * 255\n", " block.set_colors(255, val, val)\n", " \n", " elif block.row >= h / 2 and block.col >= w / 2:\n", " val = max(w - block.row - 1, h - block.col - 1) / (w / 2.) * 255\n", " block.set_colors(val, 255, val)\n", "\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "output_type": "pyout", "prompt_number": 6, "text": [ "" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "w = 40\n", "h = 40\n", "\n", "grid = BlockGrid(w, h)\n", "\n", "for block in grid:\n", " block.size = min(abs(w / 2 - block.col), abs(h / 2 - block.row))\n", " \n", " if block.row < h / 2 and block.col < w / 2:\n", " val = 255 * (1 - max(block.row, block.col) / (w / 2.))\n", " block.set_colors(val, val, val)\n", " \n", " elif block.row < h / 2 and block.col >= w / 2:\n", " val = max(block.row, h - block.col - 1) / (w / 2.) * 255\n", " block.set_colors(0, 0, val)\n", " \n", " elif block.row >= h / 2 and block.col < w / 2:\n", " val = max(w - block.row - 1, block.col) / (w / 2.) * 255\n", " block.set_colors(val, 0, 0)\n", " \n", " elif block.row >= h / 2 and block.col >= w / 2:\n", " val = max(w - block.row - 1, h - block.col - 1) / (w / 2.) * 255\n", " block.set_colors(0, val, 0)\n", "\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "output_type": "pyout", "prompt_number": 7, "text": [ "" ] } ], "prompt_number": 7 } ], "metadata": {} } ] }