{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Python for Everyone!
[Oregon Curriculum Network](http://4dsolutions.net/ocn/)\n", "\n", "## VPython inside Jupyter Notebooks\n", "\n", "### The Vector, Edge and Polyhedron types\n", "\n", "The Vector class below is but a thin wrapper around VPython's built-in vector type. One might wonder, why bother? Why not just use vpython.vector and be done with it? Also, if wanting to reimplement, why not just subclass instead of wrap? All good questions.\n", "\n", "A primary motivation is to keep the Vector and Edge types somewhat aloof from vpython's vector and more welded to vpython's cylinder instead. We want vectors and edges to materialize as cylinders quite easily.\n", "\n", "So whether we subclass, or wrap, we want our vectors to have the ability to self-draw." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The three basis vectors must be negated to give all six spokes of the XYZ apparatus. Here's an opportunity to test our \\_\\_neg\\_\\_ operator then. \n", "\n", "The overall plan is to have an XYZ \"jack\" floating in space, around which two tetrahedrons will be drawn, with a common center, as twins. \n", "\n", "Their edges will intersect as at the respective face centers of the six-faced, twelve-edged hexahedron, our \"duo-tet\" cube (implied, but could be hard-wired as a next Polyhedron instance, just give it the six faces).\n", "\n", "A lot of this wrapper code is about turning vpython.vectors into lists for feeding to Vector, which expects three separate arguments. A star in front of an iterable accomplishes the feat of exploding it into the separate arguments required.\n", "\n", "Note that vector operations, including negation, always return fresh vectors. Even color has not been made a mutable property, but maybe could be." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [ { "data": { "application/javascript": [ "require.undef(\"nbextensions/jquery-ui.custom.min\");" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require.undef(\"nbextensions/glow.2.1.min\");" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require.undef(\"nbextensions/glowcomm\");" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require.undef(\"nbextensions/pako.min\");" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require.undef(\"nbextensions/pako_deflate.min\");" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require.undef(\"nbextensions/pako_inflate.min\");" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require([\"nbextensions/glowcomm\"], function(){console.log(\"glowcomm loaded\");})" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "window.__context = { glowscript_container: $(\"#glowscript\").removeAttr(\"id\")}" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from vpython import *\n", "\n", "class Vector:\n", " \n", " def __init__(self, x, y, z):\n", " self.v = vector(x, y, z)\n", " \n", " def __add__(self, other):\n", " v_sum = self.v + other.v\n", " return Vector(*v_sum.value)\n", " \n", " def __neg__(self):\n", " return Vector(*((-self.v).value))\n", " \n", " def __sub__(self, other):\n", " V = (self + (-other))\n", " return Vector(*V.v.value)\n", " \n", " def __mul__(self, scalar):\n", " V = scalar * self.v\n", " return Vector(*V.value)\n", " \n", " def norm(self):\n", " v = norm(self.v)\n", " return Vector(*v.value)\n", " \n", " def length(self):\n", " return mag(self.v)\n", " \n", " def draw(self):\n", " self.the_cyl = cylinder(pos=vector(0,0,0), axis=self.v, radius=0.1)\n", " self.the_cyl.color = color.cyan\n", " \n", "XBASIS = Vector(1,0,0)\n", "YBASIS = Vector(0,1,0)\n", "ZBASIS = Vector(0,0,1)\n", "XNEG = -XBASIS\n", "YNEG = -YBASIS\n", "ZNEG = -ZBASIS\n", "XYZ = [XBASIS, XNEG, YBASIS, YNEG, ZBASIS, ZNEG]\n", "\n", "sphere(pos=vector(0,0,0), color = color.orange, radius=0.2)\n", "for radial in XYZ:\n", " radial.draw()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![Vpython Output](https://c5.staticflickr.com/6/5586/30491542972_1c2c899c2d.jpg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Even though the top code cell contains no instructions to draw, Vpython's way of integrating into Jupyter Notebook seems to be by adding a scene right after the first code cell. Look below for the code that made all of the above happen. Yes, that's a bit strange." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(('B', 'C'), ('A', 'D'), ('A', 'B'), ('A', 'C'), ('B', 'D'), ('C', 'D'))\n", "(('F', 'G'), ('G', 'H'), ('E', 'H'), ('F', 'H'), ('E', 'G'), ('E', 'F'))\n" ] } ], "source": [ "class Edge:\n", " \n", " def __init__(self, v0, v1):\n", " self.v0 = v0\n", " self.v1 = v1\n", " \n", " def draw(self):\n", " \"\"\"cylinder wants a starting point, and a direction vector\"\"\"\n", " pointer = (self.v1 - self.v0)\n", " direction_v = norm(pointer) * pointer.length() # normalize then stretch\n", " self.the_cyl = cylinder(pos = self.v0.v, axis=direction_v.v, radius=0.1)\n", " self.the_cyl.color = color.green\n", " \n", "class Polyhedron:\n", " \n", " def __init__(self, faces, corners):\n", " self.faces = faces\n", " self.corners = corners\n", " self.edges = self._get_edges()\n", " \n", " def _get_edges(self):\n", " \"\"\"\n", " take a list of face-tuples and distill\n", " all the unique edges,\n", " e.g. ((1,2,3)) => ((1,2),(2,3),(1,3))\n", " e.g. icosahedron has 20 faces and 30 unique edges\n", " ( = cubocta 24 + tetra's 6 edges to squares per\n", " jitterbug)\n", " \"\"\"\n", " uniqueset = set()\n", " for f in self.faces:\n", " edgetries = zip(f, f[1:]+ (f[0],))\n", " for e in edgetries:\n", " e = tuple(sorted(e)) # keeps out dupes\n", " uniqueset.add(e) \n", " return tuple(uniqueset)\n", " \n", " def draw(self):\n", " for edge in self.edges:\n", " the_edge = Edge(Vector(*self.corners[edge[0]]), \n", " Vector(*self.corners[edge[1]]))\n", " the_edge.draw()\n", "\n", "the_verts = \\\n", "{ 'A': (0.35355339059327373, 0.35355339059327373, 0.35355339059327373),\n", " 'B': (-0.35355339059327373, -0.35355339059327373, 0.35355339059327373),\n", " 'C': (-0.35355339059327373, 0.35355339059327373, -0.35355339059327373),\n", " 'D': (0.35355339059327373, -0.35355339059327373, -0.35355339059327373),\n", " 'E': (-0.35355339059327373, -0.35355339059327373, -0.35355339059327373),\n", " 'F': (0.35355339059327373, 0.35355339059327373, -0.35355339059327373),\n", " 'G': (0.35355339059327373, -0.35355339059327373, 0.35355339059327373),\n", " 'H': (-0.35355339059327373, 0.35355339059327373, 0.35355339059327373)} \n", "\n", "the_faces = (('A','B','C'),('A','C','D'),('A','D','B'),('B','C','D'))\n", "\n", "other_faces = (('E','F','G'), ('E','G','H'),('E','H','F'),('F','G','H'))\n", "tetrahedron = Polyhedron(the_faces, the_verts)\n", "inv_tetrahedron = Polyhedron(other_faces, the_verts)\n", "\n", "print(tetrahedron._get_edges())\n", "print(inv_tetrahedron._get_edges())\n", "\n", "tetrahedron.draw()\n", "inv_tetrahedron.draw()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The code above shows how we might capture an Edge as the endpoints of two Vectors, setting the stage for a Polyhedron as a set of such edges. These edges are derived from faces, which are simply clockwise or counterclockwise circuits of named vertices.\n", "\n", "Pass in a dict of vertices or corners you'll need, named by letter, along with the tuple of faces, and you're set. The Polyhedron will distill the edges for you, and render them as vpython.cylinder objects." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember to scroll up, to the scene right after the first code cell, to find the actual output of the preceding code cell.\n", "\n", "At Oregon Curriculum Network (OCN) you will find material on Quadrays, oft used to generate 26 points of interest A-Z, the A-H above the beginning of the sequence. From the duo-tet cube we move to its dual, the octahedron, and then the 12 vertices of the cuboctahedron. 8 + 6 + 12 = 26. \n", "\n", "When studying Synergetics (a namespace) you will encounter [canonical volume numbers](https://github.com/4dsolutions/Python5/blob/master/Computing%20Volumes.ipynb) for these as well: (Tetrahedron: 1, Cube: 3, Octahedron: 4, Rhombic Dodecahedron 6, Cuboctahedron 20).\n", "\n", "![Points of Interest](https://upload.wikimedia.org/wikipedia/commons/d/dc/Povlabels.gif)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For Further Reading:\n", "\n", "[Polyhedrons 101](https://github.com/4dsolutions/Python5/blob/master/Polyhedrons%20101.ipynb)
\n", "[STEM Mathematics](http://nbviewer.jupyter.org/github/4dsolutions/Python5/blob/master/STEM%20Mathematics.ipynb) -- with nbviewer" ] } ], "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.6.8" }, "widgets": { "state": {}, "version": "1.1.2" } }, "nbformat": 4, "nbformat_minor": 1 }