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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"><HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
<META NAME="GENERATOR" CONTENT="Adobe FrameMaker 5.5/HTML Export Filter">
<LINK REL="STYLESHEET" HREF="VisualRef.css" CHARSET="ISO-8859-1" TYPE="text/css">
<TITLE> Simple 3D Programming Using VPython </TITLE>
</HEAD>
<BODY BGCOLOR="#ffffff">
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="../index.html"><img src="icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A HREF="../index.html"><img src="icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A HREF="../index.html"><img src="icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Visual Reference</td>
<td><A HREF="../index.html"><img src="icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><img src="icons/blank.gif" border="0" height="32"
alt="" width="32"></td>
<td><img src="icons/blank.gif" border="0" height="32"
alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="../index.html">VPython Documentation</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="../index.html">VPython Documentation</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="../index.html">VPython Documentation</A>
<br><hr>
</DIV>
<DIV>
<H1 CLASS="Title" align="center"> <font color="#FF0000">Simple 3D Programming
Using VPython</font> </H1>
<DIV>
<H3 CLASS="Heading-1"> <font color="#0000A0">I. VPython: the Python/ Visual /
IDLE environment</font></H3>
<P CLASS="Normal">
The interactive development environment you will use is called "IDLE."</P>
</DIV>
<DIV>
<H3 CLASS="Heading2"> <font color="#0000A0">The Display window</font></H3>
<P CLASS="Normal">
When using VPython the display window shows objects in 3D.</P>
<P CLASS="Normal">
(0,0,0) is in the center of the display window<IMG SRC="axes.gif" ALIGN="RIGHT">
. The +x axis runs to the right, the +y axis runs up, and the +z axis points out of the screen, toward you.</P>
<P CLASS="Normal"> 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!)</P>
</DIV>
<DIV>
<H3 CLASS="Heading2"> <font color="#0000A0">The Output window</font></H3>
<P CLASS="Normal">
The 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.</P>
</DIV>
<DIV>
<H3 CLASS="Heading2"> <font color="#0000A0">The Code window</font></H3>
<P CLASS="Normal"> If you type 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 below. </P>
<P CLASS="program"> from visual import *</P>
<P CLASS="program"> redbox=box(pos=vector(4,2,3), size=(8.,4.,6.),color=color.red)</P>
<P CLASS="program">
greenball=sphere(pos=vector(4,7,3), radius=2, color=color.green)</P>
<P CLASS="Normal"> Visual is the name of the 3D graphics module used with
the Python programming language. VPython is the name of the combination
of the Python programming language, the Visual module, and the development
environment IDLE.</P>
<h3 class="Heading2"><font color="#0000A0">Viewing the scene</font></h3>
<p class="Normal"> In the display window, click and drag with the right mouse
button (hold down the shift key on a Macintosh). Drag left or right, and
you rotate around the scene. To rotate around a horizontal axis, drag up
or down with the right mouse button. 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 CTRL key).</p>
<div></div>
<div></div>
</DIV>
<DIV>
<H3 CLASS="Heading-1"> <font color="#0000A0">II. Visual Entities</font><IMG SRC="spherebox.gif" ALIGN="RIGHT">
</H3>
</DIV>
<DIV>
<H3 CLASS="Heading2"> <font color="#0000A0">Objects, names, and attributes</font></H3>
<P CLASS="Normal"> The 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 <span class="attribute">redbox</span>
or <span class="attribute">greenball</span> in the example above) if you
wish to refer to it again later in your program. All objects have attributes:
properties like <span class="attribute">greenball.pos</span> (the position
of the sphere), <span class="attribute">greenball.color</span>, and <span class="attribute">radius</span>
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.</P>
<P CLASS="Normal">
You can set the values of attributes in the "constructor" (the code used to create the object), and you can also modify attributes later:</P>
<P CLASS="program"> greenball.radius = 2.2</P>
<P CLASS="Normal"> In addition to the built-in set of attributes, you may
create new attributes. For example, you might create a sphere named <span class="attribute">moon</span>;
in addition to its radius and location, you might give it attributes such
as mass (<span class="attribute">moon.mass</span>) and momentum (<span class="attribute">moon.momentum</span>).</P>
<h3 class="Heading2"><font color="#0000A0">Vectors</font></h3>
<p class="Normal"> </p>
<P CLASS="heading-4">Not 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 <EM CLASS="code">a</EM>,
you may refer to its components as <EM CLASS="code">a.x</EM>, <EM CLASS="code">
a.y</EM>, and <EM CLASS="code"> a.z</EM>. To add two vectors, <EM CLASS="code">
a</EM> and <EM CLASS="code"> b</EM>, however, you do not need to add the
components one by one; Visual will do the vector addition for you:</P>
</DIV>
<DIV><P CLASS="program"> a = vector(1.,2.,3.)</P>
<P CLASS="program">
b = vector(4.,5.,6.)</P>
<P CLASS="program">
c=a+b</P>
<P CLASS="Normal"> If you print <EM CLASS="code"> c</EM> , you will find that
it is a vector with components (5.,7.,9.).</P>
<h3 class="Heading2"><font color="#0000A0">Scalar multiplication</font></h3>
<p class="Normal"> </p>
<p class="program"></p>
<P CLASS="program">d = 3.*a # d is a vector with components (3., 6., 9.)</P>
<DIV></DIV>
<DIV>
<h3 class="Heading2"><font color="#0000A0">Vector magnitude</font></h3>
<p class="Normal"> </p>
<P CLASS="program">d = mag(c) # d is a scalar</P>
<P CLASS="program"> z = mag(c)**2 # you can't square a vector; just its magnitude</P>
</DIV>
<DIV>
<h3 class="Heading2"><font color="#0000A0">Vector products</font></h3>
<p class="Normal"> </p>
<P CLASS="program">f = cross(a,b) # cross product</P>
<P CLASS="program">
g = dot(a,b) # dot product</P>
<P CLASS="program"> h = norm(a) # normalized (unit) vector</P>
<P CLASS="Normal"> The attributes of Visual objects can be vectors, such
as velocity or momentum.</P>
</DIV>
</DIV>
<DIV>
<H3 CLASS="Heading-1"> <font color="#0000A0">III. Simple Python Programming</font></H3>
</DIV>
<DIV>
<H3 CLASS="Heading2"><font color="#0000A0">Importing the 3D Graphics Module
(Visual)</font></H3>
<div>
<p class="Normal"> The first line of your program must be:</p>
<p class="program"> from visual import *</p>
</div>
<div>
<h3 class="Heading2"><font color="#0000A0"></font></h3>
</div>
<H3 CLASS="Heading2"><font color="#0000A0">Comments</font></H3>
<P CLASS="Normal">
A comment in a Python program starts with "#"</P>
<P CLASS="program"> # this line is a comment</P>
</DIV>
<DIV>
<H3 CLASS="Heading2"><font color="#0000A0">Variables</font></H3>
</DIV>
<DIV>
<P CLASS="Normal">
Variables 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.</P>
<P CLASS="program"> a = 3 #<span class="program"> an integer</span></P>
<P CLASS="program"><span class="program"> b = -2. # a floating-point number</span></P>
<P CLASS="program"><span class="program"> c = vector(0.4, 3e3, -1e1) # a vector
</span></P>
<P CLASS="program"><span class="program"> Earth = sphere(pos=(0,0,0), radius=6.4e6)
# a Visual object</span></P>
<P CLASS="program"><span class="program"> bodies = [ship, Ea</span>rth, Moon]
# a list of objects</P>
<P CLASS="Normal"> 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 momentum. Other objects, such as vector(), have built-in attributes
but you cannot create additional attributes. Numbers, lists, and other built-in
objects do not have attributes at all.</P>
</DIV>
<DIV>
<H3 CLASS="Heading2"><font color="#0000A0">Warning about division</font></H3>
<P CLASS="Normal">
Division of integers will not come out the way you may expect, since the result is rounded down to the nearest integer. Thus:</P>
<P CLASS="program"> a = 2/3</P>
<P CLASS="program">
print a # a is 0</P>
<P CLASS="Normal"> To avoid this, place a decimal point after every number, like
this:</P>
<P CLASS="program"> b = 2./3.</P>
<P CLASS="program"> print b # b is 0.6666667, as expected</P>
</DIV>
<DIV>
<div>
<p class="Normal">We recommend putting the following statement at the start
of your program, in which case 2/3 will be 0.6666667; there are two underscores
before and after the word "future":</p>
<p class="program"> from __future__ import division</p>
<h3 class="Heading2"><font color="#0000A0">Exponentiation</font></h3>
<p> </p>
</div>
<P CLASS="program"> x**2 # Not x^2</P>
<P CLASS="program">10**-2 gives an error; use 10.**-2</P>
</DIV>
<DIV>
<H3 CLASS="Heading2"> <font color="#0000A0">Logical Tests</font></H3>
<P> If, elif ("else if"), else:</P>
<P CLASS="program"> if a == b: # see table of logical expressions below</P>
<P CLASS="program"> c = 3.5 # indented code executed
if test is true</P>
<P CLASS="program">
elif a < b:</P>
<P CLASS="program"> c = 0. # c will be set to zero
only if a < b</P>
<P CLASS="program">
else:</P>
<P CLASS="program"> c = -23.2</P>
<P CLASS="program"> </P>
</DIV>
<DIV>
<H3 CLASS="Heading2"> <font color="#0000A0">Logical expressions</font></H3>
<TABLE BORDER="1">
<TR>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
==</P>
</TD>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
equal</P>
</TD>
</TR>
<TR>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
!=</P>
</TD>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
not equal (also <>)</P>
</TD>
</TR>
<TR>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
<</P>
</TD>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
less than</P>
</TD>
</TR>
<TR>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
> </P>
</TD>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
greater than</P>
</TD>
</TR>
<TR>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
<=</P>
</TD>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
less than or equal </P>
</TD>
</TR>
<TR>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
>=</P>
</TD>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
greater or equal</P>
</TD>
</TR>
<TR>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
or</P>
</TD>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
logical or</P>
</TD>
</TR>
<TR>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
and</P>
</TD>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
logical and</P>
</TD>
</TR>
<TR>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
in</P>
</TD>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
member of a sequence</P>
</TD>
</TR>
<TR>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
not in</P>
</TD>
<TD ROWSPAN="1" COLSPAN="1">
<P CLASS="program">
not sequence member</P>
</TD>
</TR>
</TABLE>
</DIV>
<DIV>
<H3 CLASS="Heading2"> </H3>
<H3 CLASS="Heading2"><font color="#0000A0">Lists</font></H3>
<P CLASS="Normal">
A list is an ordered sequence of any kind of object. It is delimited by square brackets.</P>
<P CLASS="program"> moons = [Io, Europa, Ganymede, Callisto]</P>
<P CLASS="Normal"> The function "arange" (short for "arrayrange")
creates a sequence of numbers:</P>
<P CLASS="program"> angles = arange (0., 2.*pi, pi/100.) </P>
<P CLASS="program">
# numbers from 0. to 2.*pi-(pi/100.) in steps of (pi/100.)</P>
<P CLASS="program">
</P>
<P CLASS="program"> numbers = arange(10) # integer argument -> integers</P>
<P CLASS="program"> print numbers # [0,1,2,3,4,5,6,7,8,9] </P>
<P CLASS="program"> </P>
</DIV>
<DIV>
<H3 CLASS="Heading2"> <font color="#0000A0">Loops</font></H3>
<P CLASS="Normal">
The simplest loop in Python is a "while" loop. The loop continues as long as the specified logical expression is true:</P>
<P CLASS="program"> while x < 23:</P>
<P CLASS="program"> x = x + vx*dt</P>
<P CLASS="Normal">
To write an infinite loop, just use a logical expression that will always be true:</P>
<P CLASS="program"> while 1==1:</P>
<P CLASS="program"> ball.pos = ball.pos + (ball.momentum/ball.mass)*dt</P>
<P CLASS="Normal">
Since the value assigned to a true logical expression is 1, the following also produces an infinite loop:</P>
<P CLASS="program"> while 1:</P>
<P CLASS="program"> a = b+c</P>
<P CLASS="Normal">
Infinite loops are ok, because you can always interrupt the program by choosing "Stop Program" from the Run menu in IDLE.</P>
<P CLASS="Normal">
It is also possible to loop over the members of a sequence:</P>
<P CLASS="program"> moons = [Io, Europa, Ganymede, Callisto]</P>
<P CLASS="program"> for a in moons:</P>
<P CLASS="program"> r = a.pos - Jupiter.pos</P>
<P CLASS="program"> </P>
<P CLASS="program">for x in arange(10):</P>
<P CLASS="program">
# see "lists" above</P>
<P CLASS="program">
...</P>
<P CLASS="program"> </P>
<P CLASS="program">for theta in arange(0., 2.*pi, pi/100.): </P>
<P CLASS="program">
# see "lists" above</P>
<P CLASS="Normal">
You can restart a loop, or terminate the loop prematurely:</P>
<P CLASS="program"> if a == b: continue # go back to the start of the loop</P>
<P CLASS="program"> if a > b: break # exit the loop</P>
<P CLASS="program"> </P>
</DIV>
<DIV>
<H3 CLASS="Heading2"> <font color="#0000A0">Printing results</font></H3>
<P CLASS="Normal">
To print a number, a vector, a list, or anything else, use the "print" command:</P>
<P CLASS="program"> print Europa.momentum</P>
<P CLASS="Normal">
To print a text message, enclose it in quotes:</P>
<P CLASS="program"> print "We have just crashed on the Moon with speed",
v, "m/s."</P>
<h3 class="Heading2"><font color="#0000A0">More Information about Python</font></h3>
<p>See <a href="index.html">The Visual Module of VPython</a> for detailed
descriptions of all aspects of Visual.</p>
<p>We have summarized a very small subset of the Python programming language.
Extensive <a href="../index.html">Python Documentation</a> is provided
here, and there is additional information at the <a href="http://www.python.org/">Python
website</a>, 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: <i>Learning Python</i>,
by Mark Lutz & David Ascher, 1999, O'Reilly.</p>
</DIV>
<DIV> </DIV>
</DIV>
<hr>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="../index.html"><img src="icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A HREF="../index.html"><img src="icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A HREF="../index.html"><img src="icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Visual Reference</td>
<td><A HREF="../index.html"><img src="icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><img src="icons/blank.gif" border="0" height="32"
alt="" width="32"></td>
<td><img src="icons/blank.gif" border="0" height="32"
alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="../index.html">VPython Documentation</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="../index.html">VPython Documentation</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="../index.html">VPython Documentation</A>
<br><hr>
</DIV>
</BODY>
</HTML>
|