-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathVPython-vs-GlowScript.html
More file actions
88 lines (87 loc) · 11.3 KB
/
Copy pathVPython-vs-GlowScript.html
File metadata and controls
88 lines (87 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>VPython vs GlowScript</title>
<link href="../css/help.css" rel="stylesheet" type="text/css" />
<link href="css/help.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h3>GlowScript for those who have used VPython</h3>
<p>This document is intended for those who may wish to write their GlowScript programs directly in RapydScript rather than using the VPython definitions.</p>
<p>The capabilities and syntax of GlowScript are based on those of VPython. For those familiar with VPython, this is a summary of the differences between the two environments. For those familiar with Python, it offers a summary of the main differences between the programming languages Python and RapydScript.</p>
<p>VPython uses the OpenGL 3D graphics library; it does not run in a browser. GlowScript uses the closely related WebGL 3D graphics library that is included in major web browsers. See the <a href="index.html">first page</a> of the GlowScript Help for browser details.</p>
<h4>Using the editor in GlowScript</h4>
<p>GlowScript uses the ACE text editor. Here is a list of <a href="https://github.com/ajaxorg/ace/wiki/Default-Keyboard-Shortcuts" target="_blank">keyboard shortcuts</a> for find, replace, etc.</p>
<h4>for loops</h4>
<p>In Python, when a for-loop increment is a fraction, you must use the arange() function, like this: "for x in arange(0, 10, 0.1)". In RapydScript you simply say "for x in range(0, 10, 0.1)".</p>
<h4>vec instead of vector; vec is required</h4>
<p>In VPython you can say<span class="program"> b.pos = (1,2,0)</span>, and the <span class="program">(1,2,0)</span> is converted to <span class="program">vector(1,2,0)</span>. This is not possible in GlowScript, where you must be specific about vector quantitities. Here are the possibilities:</p>
<blockquote>
<p class="Courier"><span class="program">b.pos = vec(1,2,0) # okay<br />
b.pos = (1,2,0) # error</span></p>
</blockquote>
<p>A related issue is that in VPython <span class="program">b.x</span> is a shorthand for <span class="program">b.pos.x</span>. The shorthand <span class="program">b.x</span> is not available in GlowScript; you must refer to <span class="program">b.pos.x</span>. Because GlowScript requires that all vectors be explicitly stated to be vectors, the name "vector" has been shortened to "vec".</p>
<p>In VPython, <span class="program">pos</span>, <span class="program">axis</span>, and <span class="program">up</span> are vectors, but <span class="program">size</span> and <span class="program">color</span> are not. In GlowScript <span class="program">size</span> and <span class="program">color</span> are also vector quantities. Here is how to create a non-cubic box with a special color:</p>
<blockquote>
<p class="program">box( pos=vec(2,1,0), size=vec(4,2,0.5), color=vec(1,0.6,0)} )</p>
</blockquote>
<p>An interesting capability that results from treating color as a vector is that you can blend two colors <span class="program">c1</span> and <span class="program">c2</span> as <span class="program">(a*c1 + b*c2)/(a+b)</span>.</p>
<h4>axis, size, radius, and length</h4>
<p>In VPython <span class="program">axis</span> and <span class="program">length</span> (<span class="program">size.x</span>) are tied to each other. When you change <span class="program">axis</span> of a VPython object, <span class="program">length</span> is set to the magnitude of <span class="program">axis</span>. Similarly, when you set <span class="program">length</span>, you also set the magnitude of <span class="program">axis</span>.</p>
<p>In GlowScript <span class="program">axis</span> and <span class="program">size</span> are not tied to each other. You can set <span class="program">axis</span> without affecting <span class="program">size</span>, and you can set <span class="program">size</span> without affecting <span class="program">axis</span>. Only in the case of the arrow object is there a special attribute, <span class="program">axis_and_length</span>, for changing both direction and length at the same time.</p>
<p>All basic GlowScript objects (box, cylinder, etc., but not arrow) have <span class="program">size</span> attributes, which is not the case in VPython, and these objects fit into a bounding box given by <span class="program">size</span>. By default, <span class="program">size = vec(1,1,1) </span>except for the ring object where <span class="program">size</span> is vec(0.1,1,1) to give a thickness of 0.1. This means that you can change a box to a sphere or vice versa and they'll fit into the same cube of 1 unit on a side (so the default diameter of a sphere is 1 rather than the default radius being 1). Similarly, you can interchange cones and pyramids and cylinders and helices. </p>
<p>There is no <span class="program">radius</span> or <span class="program">length</span> or <span class="program">height</span> or <span class="program">width</span> attribute for these objects. Rather, you specify the bounding box with <span class="program">size</span>. A convenient way to specify the diameter D of a sphere is by setting size to D*vec(1,1,1). Note that you can make an ellipsoid from a sphere, or make a cylinder with an elliptical cross section, by making<span class="program"> size.y</span> different from <span class="program">size.z</span>.</p>
<h4>curve</h4>
<p>A GlowScript curve is an array of point objects, each of which has attributes of <span class="program">pos</span>, <span class="program">color</span>, and <span class="program">radius</span>.</p>
<p>See the curve documentation for ways to manipulate the list of points in a curve.</p>
<p>Unlike VPython, you can vary the radius of the cross section along the curve by setting the <span class="program">radius</span> attribute of the point objects. Also, the curve object itself has its own <span class="program">pos</span>, <span class="program">size</span>, <span class="program">axis</span>, and <span class="program">up</span> attributes, so that you can move or rotate or resize the entire curve object quickly and easily.</p>
<h4>Animations and rate</h4>
<p>VPython animation loops must contain a rate statement to make the animation run at an appropriate rate. In a web browser environment, one must occasionally pause to permit the screen to be redrawn, and programs must be written to run for short amounts of time and then exit, having specified a time in the future when they want to run again. This structure is enabled in GlowScript by you marking in your program where such pauses can occur in your program. This special mark is the word "wait":</p>
<table width="377" height="66" border="0">
<tr>
<td width="19"> </td>
<td width="161" height="62" style="font-family: 'Courier New', Courier, monospace"><p align="left" class="program"># VPython</p>
<p align="left" class="program">rate(100)<br />
</p></td>
<td width="183" style="font-family: 'Courier New', Courier, monospace"><p align="left" class="program"># GlowScript</p>
<p align="left" class="program">rate(100,wait) </p></td>
</tr>
</table>
<p>There is also a statement <span class="program">sleep(0.01,wait)</span>, wait for 1/100th of a second. The statement <span class="program">update(wait)</span> gives the browser the opportunity to update the page display.</p>
<h4>VPython objects available in GlowScript</h4>
<p>Currently, all VPython objects have been implemented except for faces (replaced by vertex, triangle, and quad), text, extrusion, frame, and points (which you can implement simply by displaying small spheres). Instead of frame there is a "compound" object, which among other things makes possible very fast displays of objects consisting of several combined objects. There is also a "clone" capability to make copies of existing objects.</p>
<h4>Display regions</h4>
<p>A display region was called a "display" in VPython and is a "canvas" in GlowScript, corresponding to the name by which drawing regions are known on web pages.</p>
<h4>Wait for a mouse event</h4>
<p>You can wait for a mouse event with these statements:</p>
<p class="program"> scene.waitfor('click', wait) # wait for mouse click<br />
scene.waitfor('mousedown', wait) # wait for mouse button to be depressed<br />
scene.waitfor('mouseup', wait) # wait for mouse to be moved (with button depressed)<br />
scene.waitfor('mousemove', wait) # wait for mouse button to be released</p>
<p>As in VPython, <span class="program">scene.mouse.pos</span> gives the current position of the mouse.</p>
<p>The statement scene.<span class="program">waitfor('redraw', wait)</span> will wait for the beginning of the next screen update time, which lets you synchronize your updates with the screen. There is a similar statement <span class="program">scene.waitfor('draw_complete', wait)</span> which waits until the next screen update has been completed.<br />
</p>
<h4>Graphs</h4>
<p>Making graphs in GlowScript is quite similar to making graphs in VPython. See the <a href="graph.html" target="_blank">description</a> in the Help for details. One difference is that each line-graph, scatter-graph, or bar-graph object has its own label, which appears in a legend on the graph. Also, if you don't specify a color, a color will be assigned automatically by the <strong>flot</strong> graphing library, <a href="http://code.google.com/p/flot/" target="_blank">http://code.google.com/p/flot</a>.</p>
<h4>Math functions</h4>
<p>The math functions <span class="program">sqrt, sin, cos, tan, asin, acos, atan, atan2, exp, log, pow, abs</span>, and the constant <span class="program">pi</span> are all available in GlowScript. The function <span class="program">Math.random()</span> provides a random number in the range 0 to 1. For a full discussion, including radian/degree conversion and getting time information, see the <a href="math.html" target="_blank">description</a> in the Help.</p>
<h4>Vector functions</h4>
<p>See the description of GlowScript <a href="vector.html" target="_blank">vector functions</a> for some differences from VPython. </p>
<h4>Miscellaneous</h4>
<p>Here is a name that is different in VPython and GlowScript:</p>
<table width="442" height="71" border="0">
<tr>
<td width="40"> </td>
<td width="196" height="67" style="font-family: 'Courier New', Courier, monospace"><p align="left" class="program"># VPython</p>
<p align="left" class="program">scene.mouse.camera </p></td>
<td width="192" style="font-family: 'Courier New', Courier, monospace"><p align="left" class="program"># GlowScript</p>
<p align="left" class="program">scene.camera.pos </p></td>
</tr>
</table>
<p>In a Python function you can specify that a variable "x" is global rather than local to the function. In RapydScript, as in Python 3, you say "nonlocal x" instead of "global x".</p>
<p><strong>Full RapydScript documentation</strong></p>
<p>Full documentation of RapydScript is available at<a href="http://rapydscript.pyjeon.com" target="_blank"> http://rapydscript.pyjeon.com</a>.</p>
<h4> </h4>
</body>
</html>