|
If you're new to Python A VPython tutorial Pictures of 3D objects Choose a 3D object: Work with 3D objects: Windows, Events, & Files: What's new in Visual 5 VPython web site |
Simple 3D Programming Using VPythonI. VPython: the Python/ Visual / IDLE environmentThe interactive development environment you will use is called "IDLE." The Display windowWhen using VPython the display window shows objects in 3D. (0,0,0) is in the center of the display window x, y, and z are measured in whatever units you choose; the display is automatically scaled appropriately. (You could, for example, create a sphere with a radius of 1E-15 m to represent a nucleus, or a sphere with a radius of 1E6 m to represent a planet, though it wouldn't make sense to put both of these objects in the same display!) The Output windowThe output of any -print- statements you execute in your program goes to the Output window, which is a scrolling text window. You can use this window to print values of variables, print lists, print messages, etc. Place it where you can see messages in it. The Code windowIf you type or copy the following simple program into the code window in IDLE and run it (press F5, or use the Run menu), you will see a display like the one shown in the figure. from visual import * Viewing the sceneIn the display window, click and drag with the right mouse button (hold down the command key on a Macintosh). Drag left or right, and you rotate around the scene. To rotate around a horizontal axis, drag up or down. Click and drag up or down with the middle mouse button to move closer to the scene or farther away (on a 2-button mouse, hold down the left and right buttons; on a 1-button mouse, hold down the Option key). II. Visual EntitiesObjects, names, and attributesThe graphical objects you create, such as spheres, boxes, and curves, continue to exist for the duration of your program, and the Visual 3D graphics module will continue to display them, wherever they are. You must give each object a name (such as redbox or ball in the example above) if you wish to refer to it again later in your program. All objects have attributes: properties like ball.pos (the position of the sphere), ball.color, and radius or other size parameter. If you change an attribute of an object, such as its position or color, Visual will automatically display the object in its new location, or with its new color. You can set the values of attributes in the "constructor" (the code used to create the object), and you can also modify attributes later: ball.radius = 2.2 In addition to the built-in set of attributes, you may create new attributes. For example, you might create a sphere named moon; in addition to its radius and location, you might give it attributes such as mass (moon.mass) and momentum (moon.momentum). VectorsNot all objects in Visual are visible objects. For example, Visual allows you to create 3D vector quantities, and to perform vector operations on them. If you create a vector quantity called a, you may refer to its components as a.x, a.y, and a.z. To add two vectors, a and b, however, you do not need to add the components one by one; Visual will do the vector addition for you: a = vector(1,2,3) If you print c , you will find that it is a vector with components (5, 7, 9.). Scalar multiplicationd = 3*a # d is a vector with components (3, 6, 9) Vector magnitudes = mag(c) # s is a scalar Vector productsf = cross(a,b) # cross product The attributes of Visual objects can be vectors, such as velocity or momentum. III. Simple Python ProgrammingImporting the 3D Graphics Module (Visual)The first line of your program must be: from visual import * CommentsA comment in a Python program starts with "#" # this line is a comment VariablesVariables can be created anywhere in a Python program, simply by assigning a variable name to a value. The type of the variable is determined by the assignment statement. a = 3 # an integer Basic Visual objects such as sphere() and box() have a set of "attributes" such as color, and you can define additional attributes such as mass or velocity. Other objects, such as vector(), have built-in attributes but you cannot create additional attributes. Warning about divisionDivision of integers will not come out the way you may expect, since the result is rounded down to the nearest integer. Thus: a = 3/4 To avoid this, you can place a decimal point after every number, like this: b = 3./4. We recommend putting the following statement as the first line of your program, in which case 3/4 will be 0.75; there are two underscores before the word "future" and two after the word "future": from __future__ import division Exponentiationx**2 # Not x^2, which is a bit operation in Python Logical TestsIf, elif ("else if"), else: if a == b: # see table of logical expressions below Logical expressions
ListsA list is an ordered sequence of any kind of object. It is delimited by square brackets. moons = [Io, Europa, Ganymede, Callisto] The function "arange" (short for "arrayrange") creates an array of numbers: angles = arange (0., 2*pi, pi/100) LoopsThe simplest loop in Python is a "while" loop. The loop continues as long as the specified logical expression is true: while x < 23: To write an infinite loop, just use a logical expression that will always be true: while True: Infinite loops are ok, because you can always interrupt the program by choosing "Stop Program" from the Run menu in IDLE. It is also possible to loop over the members of a sequence: moons = [Io, Europa, Ganymede, Callisto] if a == b: continue # go back to the start of the loop Printing resultsTo print a number, a vector, a list, or anything else, use the "print" option: print Europa.momentum To print a text message, enclose it in quotes: print "We crashed on the Moon with speed", v, "m/s." Python also offers a formatted print capability. Here price will be printed with 3 digits before the decimal place and 2 digits after, and num will be printed as an integer: print "It's $%3.2f dollars for %d copies" % (price,num) More Information about PythonWe have summarized a small but important subset of the Python programming language. Extensive Python documentation is provided on the Help menu in IDLE, and there is additional information at the Python website, but much of this information assumes that you already have lots of programming experience in other languages. We recommend the following book to those who want to learn more about Python, and about programming in general: Python Programming: An Introduction to Computer Science by John M. Zelle (Franklin Beedle & Associates, 2003). |
||||||||||||||||||||||