0% found this document useful (0 votes)
135 views

Lab 1 Introduction To Vpython

This document provides instructions for an introductory lab assignment in computational physics using the VPython programming environment. Students are instructed to create a VPython program that generates 3D graphical objects like spheres and arrows. The program demonstrates how to set attributes of objects like position, color, and scale arrows. Students are asked to name objects, print out attributes, and comment their code. Screenshots and code are to be pasted into a Lab1 document for submission.

Uploaded by

api-590983365
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views

Lab 1 Introduction To Vpython

This document provides instructions for an introductory lab assignment in computational physics using the VPython programming environment. Students are instructed to create a VPython program that generates 3D graphical objects like spheres and arrows. The program demonstrates how to set attributes of objects like position, color, and scale arrows. Students are asked to name objects, print out attributes, and comment their code. Screenshots and code are to be pasted into a Lab1 document for submission.

Uploaded by

api-590983365
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Computational Physics

Lab 1: Introduction to VPython

Getting Started

Go to https://www.glowscript.org/ to get started. At the top right corner of the page, click sign in.
There, sign in with your school Google account as you would for any other school website. Once
you are signed in, it will prompt “You are signed in as your_name and your programs are here.”
on the home page. Click on the linked “here” to access your stored programs.

Our First Program

Create a new program and name it "Lab1". In your program editor, enter the following lines of
code:
from visual import *

Every VPython program begins with this line of code. This line tells the program to use the 3D
module called “visual” (which is what the “V” stands for in VPython!)

VPython has built-in commands to generate simple 3D objects. On the next line of your program,
type:
sphere()

Run your code. Your amazing output will open in a new browser tab or window and look very
much like this:
Error Messages

If your program contains errors, it won’t run and the Python interpreter will let you know.
Change the first line of your program to read:

from bisual import *

An obvious mistake. Run your program now and observe the output. No sphere is created and
the error is reported something like this:

Error: Line 2: Cannot import from bisual

You may not understand the error messages, but it is important that you know there is an error.
You can usually find the error and fix it pretty quickly with some practice. One hint that will make
your life easier: make only a few changes to your program at a time and run it to check for errors.
If there is an error, it will be easier to track down. Correct the error now and re-run your code.

Attributes

We can change the position, radius, or color of our sphere by giving it attributes. Change the last
line of your program to read:

sphere(pos=vector(-5,2,-3), radius=0.40, color=color.red)

Run the program. Our sphere has now been given new attributes. A sphere has attributes pos,
radius, and color. The pos attribute specifies the location of the center of the sphere in the familiar
three dimensional coordinate system x-y-z. The default radius is 1, but there are no units. Feel free
to imagine what “1” is measured in. The default color is white but the other colors are blue, green,
cyan, magenta, yellow, orange, or black. Experiment with these attributes.

Change the attributes of your sphere to:

sphere(pos=vector(2,4,0), radius=0.20, color=color.white)

Create a second sphere with position (-3,-1,0) and radius = 0.15. Make this sphere green. You
should now see two spheres when you run your program. In a bit, the white sphere will represent
a baseball and the green sphere will represent a tennis ball.
Arrows

Another object we can create in our 3D graphics window is an arrow. We will use arrows to depict
quantities like velocity and force in later programs. Add the following line to your program and
run it:

arrow(pos=vector(2,-3,0), axis=vector(3,4,0), color=color.cyan)

To illustrate the difference between the pos and axis attributes, add another arrow to your
program:

arrow(pos=vector(3,2,0), axis=vector(3,4,0), color=color.red)

Experiment with changing the pos and axis attributes of these arrows until you understand more
or less how they work.

Questions

It is time to pause for a moment and create the file that you will be turning in for a grade. Create a
new document and name it Lab1LastName. In the first lines of the file be sure to name the lab
and yourself appropriately. It should look like this:
In this file, you will record all of your written responses to questions, screenshots and samples of
code. You can easily spot assigned tasks because they appear just like this:

Question 1: What does the pos attribute of an arrow describe?

Question 2: What does the axis attribute of an arrow describe?

Question 3: What position would you give a sphere so that it


would appear at the tip of the red arrow? Check your answer
with your program!

You should answer these questions in your Lab1 document now. Copy the question into your
document and follow it with your answer.

Scaling

Change the last line of your program to the following:

arrow(pos=vector(3,2,0), axis=-0.5*vector(3,4,0), color=color.red)

Notice that the red arrow now points in the opposite direction because of the minus sign. It is also
half as long as the cyan arrow. Multiplying by a number changes the length of an arrow.

For the next section, you’ll only need one arrow. Instead of deleting one of the arrows from your
program, let’s “comment out” a line. Change the second to the last line of your program to the
following:

#arrow(pos=vector(2,-3,0), axis=vector(3,4,0), color=color.cyan)

The pound sign or hash (#) at the beginning of the line causes the program to ignore this line. Run
your program to verify that the cyan arrow is no longer there. From now on, you will be asked to
place comments into your code. For example, I can do this:

#arrow(pos=vector(2,-3,0), axis=vector(3,4,0), color=color.cyan)

# The line above used to create an arrow, but I have “commented it out”
# because I am following directions!

The interpreter will ignore all of this, but your teacher can grade it later.
Place a comment in your code that includes your name and the due date of Lab 1

Change the attributes of the red arrow so that its tail is at the position of the baseball (white
sphere) and tip is at the position of the tennis ball (green sphere). You will have to determine the
pos and axis attributes of the arrow. Remember, pos gives the position of the arrow tail, and axis
describes its length. HINT: Subtract the position of the white sphere from the position of the
green sphere and use that to set the arrow axis.

Gosh that arrow is thick. For a sleeker looking arrow, add

color=color.red, shaftwidth=0.1)

to the end of your arrow command. A little research will often reveal all of the fun little
improvements you can make.

Naming Objects and Using Object Attributes

We can give names to the objects in a program. This makes it easier to refer to them or their
attributes later on. Change the “sphere” lines of your program to the following:

baseball = sphere(pos=vector(2,4,0), radius=0.20, color=color.white)


tennisball = sphere(pos=vector(-3,-1,0), radius=0.15, color=color.green)

We can now refer to the attributes of each sphere by writing, for example, tennisball.pos to
refer to the tennis ball’s position. That is, tennisball.pos is a vector with value (-3,-1,0).
Similarly, tennisball.radius is a number with value 0.15. You can check this by printing
these attributes. Add the following line to your program:

print (tennisball.pos)

The output gives us all three coordinates of the tennis ball position. If we like, we can print out
only the y coordinate. Change the last line of your code to:

print (tennisball.pos.y)

Give your arrow a UNIQUE name also.


No, not “arrow”.
Challenge

Challenges are NOT required for the assignment, but they are a big part of the fun.

Since we can refer to the attributes of objects symbolically, we want to write symbolic expressions
for the axis and pos of the arrow. The expressions should use general attribute names in symbolic
form, like tennisball.pos or baseball.pos, not specific numerical values. This way, if the
position of the tennis ball or baseball is changed, the arrow will still point from the baseball to the
tennis ball. Think about how you calculated what the arrow axis values should be and write it into
the arrow axis.

Final Tasks

Paste a screenshot of your program output into your Lab1 document.

Paste your final program text (not a screenshot) into your Lab1 document.
Format your code so that it looks presentable.

Submit your Lab1 document by sharing with [email protected] - please


make sure to check the notify box so that I receive an email.

You might also like