{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 43)", "output_type": "error", "traceback": [ "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m43\u001b[0m\n\u001b[1;33m quad(v0=verts[y][x], v1=verts[y][x+1], v2=verts[y+1][x+1], v3=verts[y+1][x]], texture=textures.rug)\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "from vpython import *\n", "\n", "scene.width = scene.height = 600\n", "scene.range = 0.6\n", "\n", "# A pulse ripples along a rug, demonstrating dynamic changea of shape\n", "# Bruce Sherwood, May 2012\n", "\n", "def display_instructions():\n", " s = \"Click to halt or run. In VPython programs:\\n\"\n", " s += \" Rotate the camera by dragging with the right mouse button,\\n\"\n", " s += \" or hold down the Ctrl key and drag.\\n\"\n", " s += \" To zoom, drag with the left+right mouse buttons,\\n\"\n", " s += \" or hold down the Alt/Option key and drag,\\n\"\n", " s += \" or use the mouse wheel.\\n\"\n", " s += \"Touch screen: pinch/extend to zoom, swipe or two-finger rotate.\"\n", " scene.caption = s\n", " \n", "display_instructions()\n", "\n", "# Construct a square WxH divided into little squares\n", "# There are (w+1)x(h+1) vertices\n", "# Center of rug is at 0,0,0\n", "\n", "H = W = 1\n", "w = 1\n", "h = 50\n", "dx = W/w\n", "dy = H/h\n", "\n", "# Create a grid of vertex objects covering the rug\n", "verts = []\n", "for y in range(h+1): # from 0 to h inclusive, to include both bottom and top edges\n", " verts.append([])\n", " for x in range(w+1): # from 0 to w inclusive, to include both left and right edges\n", " verts[y].append(vertex(pos=vector(-0.5+x*dx,-0.5+y*dy,0), normal=vector(0,0,1), texpos=vector(x/w,y/h,0), shininess= 0))\n", "\n", "# Create quads (equivalent to two triangles) based on the vertex objects just created.\n", "# Note that a particular vertex may be shared by as many as 4 neighboring quads, and\n", "# changing one vertex affects all of the quads that use that vertex.\n", "for y in range(h): # from 0 to h, not including h\n", " for x in range(w): # from 0 to w, not including w\n", " quad(v0=verts[y][x], v1=verts[y][x+1], v2=verts[y+1][x+1], v3=verts[y+1][x], texture=textures.rug)\n", "\n", "#scene.waitfor('textures') # wait until the rug texture has been loaded\n", "\n", "Lpulse = 0.4 # length of half sine wave\n", "dy_pulse = Lpulse/50\n", "k = pi/(0.6*Lpulse)\n", "A = 0.05\n", "\n", "def pulse(z): # return the pulse height and normal\n", " if z < 0.2*Lpulse: return 0\n", " if z > 0.8*Lpulse: return 0\n", " z -= 0.2*Lpulse\n", " return A*sin(k*z)\n", "\n", "run = True\n", "\n", "def down(ev):\n", " global run\n", " run = not run\n", "\n", "scene.bind(\"mousedown\", down)\n", "\n", "y = -0.5-Lpulse-dy_pulse # bottom of pulse (starts below rug)\n", "while True:\n", " rate(50)\n", " while not run:\n", " scene.waitfor('redraw')\n", " y += dy_pulse\n", " if y+Lpulse <= -0.5:\n", " continue\n", " if y >= 0.5:\n", " y = -0.5-Lpulse\n", " continue\n", " \n", " # Note: floor and ceil are floats in Python 2 but ints in Python 3\n", " start = int(floor((y+0.5)/dy)) # lowest row of vertices in pulse\n", " end = int(ceil((y+0.5+Lpulse)/dy)) # highest row of vertices in pulse\n", " if start < 0:\n", " if end <= 0: continue\n", " start = 0\n", " if end > h:\n", " end = h\n", " \n", " yp = -0.5+start*dy\n", " for s in range(start,end):\n", " z0 = pulse(yp-y-dy_pulse)\n", " z1 = pulse(yp-y)\n", " z2 = pulse(yp+dy_pulse-y)\n", " yp += dy # advance to next row\n", " \n", " # If slope of a line is dy/dz, normal to the line is in direction < -dz, +dy >\n", " n1y0 = -(z1-z0)\n", " n2y0 = -(z2-z1)\n", " n1y = .5*(n1y0+n2y0) # average adjacent normals to smooth the lighting\n", " n1z = dy\n", " \n", " vy = verts[s]\n", " for vx in range(w+1):\n", " vy[vx].pos.z = z1\n", " vy[vx].normal = vector(0,n1y,n1z) " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "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 }