Step 01. The Basic Structure VPython Programs

The goal of using VPython on PHYS 151 and PHYS 241 is to visualize the physical interaction of physical systems.

 

Code of background colors:

yellow : use the VIDLE for VPython

green font on yellow background: write the exact content on VIDLE for VPython

 

When you start to write the code, please be 100% sure that you already have the correct representation of the problem:
the geometrical representation (be able to create all the objects involved in the problem)
the physical representation (use the proper equations to symbolically solve the problem)
– and to respect the correct structure of Python programs (see below).

 

You may write your code using any text editor (as Notepad from Windows) or you may use the VPython IDLE (Integrated DeveLopment Environment). Whatever method you will use, be sure that you will save your code as a file having the suffix .py (ex. MyFirstProgram.py).

 

Find the VIDLE for VPython icon on your computer’s desktop.

Start VIDLE (double click on it). Now you can start typing the lines for your program.

 

The structure of VPython program is:

  1. Special setup statements
  2. Definition of constants
  3. Creation of objects
  4. Calculation to predict motion or move objects

 

In the empty window, type the following statements:

from future import division

from visual import *

The first line instructs VPython to avoid the integer division problems. There are two underscores before “future” and two after.
The second line instructs VPython to use the Visual graphics mode.

 

In VPython you may use a wide range of built-in objects (http://vpython.org/contents/docs/primitives.html).

From the list of the objects, find the box and open it.

box

The syntax of defining an object is:

name = object (attributes)

For the name of the object, you may choose any name, excepting the Python reserved words.

The object  may be any object from the primitives (built-in objects) list.

The attributes are specific for each primitive and they can be declared in any random order. The attributes describe the object’s specific features, in a natural language representation.

mybox = box ( pos=(x0,y0,z0), length=L, height=H, width=W)

mybox – is the name we are using to refer to our object

box – defines the object (is the name of the primitive)

attributes:
pos = (x0, y0, z0) – the position of the center of the object is at (x0, y0, z0)
length = L – the length (dimension on Ox direction)
height = H – the height (dimension on Oy direction)
width = W – the width (dimension on Oz direction)
color = color.red – defines the red color for my object (other colors: black, blue, yellow, green, white, magenta, cyan)

Note: x0, y0, z0, L, H, W must to be numbers or constants previously declared.

 

In your VIDLE window, after the first two lines of statements, try to define:

a green cube of side 50 cm, with the center in the coordinate.

a blue box named “home” with the center at (x0=1m, y0=1m, z0= – 1m) and LxHxW = 1.5 m x 0.5 m x 0.5 m.

save (File -> Save à … (save it on the Desktop, and give it the name “FirstCode-Name.py”)

where Name is your last name.

Run your program by pressing F5 on the keyboard

 

Add this line in your program:

home.color = color.red

and run again the program (F5).

What do you noticed?

 

Homework:

Try to set a red sphere of radius = 1 m on the top, left side of a black box L x H x W = 5m x 0.2m x 1 m.

 

 
hw1
 

Once you are done (or if do you need help), send your code (the .py file, as attachment) by email to: Ldumitri@spelman.edu, with subject: HW1-Name, where Name is your last name.

 

 

350 Spelman Lane S.W. Atlanta, GA 30314-4399 • (404) 681-3643 | Copyright 2024 Spelman College