{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "require.undef(\"nbextensions/data/jquery-ui.custom.min\");" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require.undef(\"nbextensions/data/glow.2.1.min\");" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require.undef(\"nbextensions/data/glowcomm\");" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require([\"nbextensions/data/jquery-ui.custom.min\"], function(){console.log(\"JQUERY LOADED\");})" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require([\"nbextensions/data/glow.2.1.min\"], function(){console.log(\"GLOW LOADED\");})" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require([\"nbextensions/data/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", "scale = 4e-14/1e17\n", "ec = 1.6e-19 # electron charge\n", "kel = 9e9 # Coulomb constant\n", "scene.range = 2e-13\n", "\n", "charges = [ sphere( pos=vector(-1e-13,0,0), Q=ec, color=color.red, size=1.2e-14*vector(1,1,1) ),\n", " sphere( pos=vector( 1e-13,0,0), Q=-ec, color=color.blue, size=1.2e-14*vector(1,1,1) )]\n", "\n", "s = \"Click or drag to plot an electric field vector produced by the two charges.\\n\"\n", "s += \"On a touch screen, tap, or press and hold, then drag.\\n\"\n", "s += \"Arrows representing the field are bluer if low magnitude, redder if high.\"\n", "scene.caption = s\n", "\n", "def getfield(p):\n", " f = vec(0,0,0)\n", " for c in charges:\n", " f = f + (p-c.pos) * kel * c.Q / mag(p-c.pos)**3\n", " return f\n", "\n", "def mouse_to_field(a):\n", " p = scene.mouse.pos\n", " f = getfield(p)\n", " m = mag(f)\n", " red = max( 1-1e17/m, 0 )\n", " blue = min( 1e17/m, 1 )\n", " if red >= blue:\n", " blue = blue/red\n", " red = 1.0\n", " else:\n", " red = red/blue\n", " blue = 1.0\n", " a.pos = p\n", " a.axis = scale*f\n", " a.color = vector(red,0,blue)\n", " a.visible = True\n", "\n", "drag = False\n", "a = None\n", "\n", "def down(ev):\n", " global a, drag\n", " a = arrow(shaftwidth=6e-15, visible=False)\n", " mouse_to_field(a)\n", " drag = True\n", "\n", "def move(ev):\n", " global a, drag\n", " if not drag: return\n", " mouse_to_field(a)\n", " \n", "def up(ev):\n", " global a, drag\n", " mouse_to_field(a)\n", " drag = False\n", "\n", "scene.bind(\"mousedown\", down)\n", "\n", "scene.bind(\"mousemove\", move)\n", "\n", "scene.bind(\"mouseup\", up)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "VPython", "language": "python", "name": "vpython" }, "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.1" } }, "nbformat": 4, "nbformat_minor": 0 }