Download PHY307 Homework Assignment #3: Drawing Cubes and Racing Shapes and more Assignments Physics in PDF only on Docsity!
KEY to PHY307 HWK ASSIGNMENT #3, due Sept. 10, 2002:
- Use a for loop to draw 10 evenly spaced cubes, arranged along a line. Remember the attributes position, height, width, and length. Cubes are special cases of boxes where the height, width,
and length are all the same. (See the VPython documentation at www.vpython.org). You can
make the cubes overlapping or not; you can make them of the same size or not. Remember the incantation at the beginning of the program necessary to give your code the visual library. Submit the Python code and describe what you see.
Simple:
# This program draws 10 cubes, spaced evenly along a line.
# The cubes have edges of dimension 0.5 and are separated by distance 1.0.
from visual import *
# Step through 10 values of w, setting x position of cube to be w.
# Other properties, such as color, y, and z take default values.
for w in range(10):
box(height=0.5, width=0.5, length=0.5, x=w))
Shortest known solution:
from visual import *
for w in range(10): box(x=2*w)
Unfortunately, you can save space, but make your program harder to read, by putting a
single block statement onto the for line
More complicated:
To the box creation operation, one could add changes like:
height = w/10., width = w/10., length = w/10.
color = (w/10., 1.-w/10., 0)
to make, respectively, a range of sizes for the boxes or colors (the colors are in
(red,green,blue) triplets, with 1.0 being max and 0 being dark.)
See the Python examples at the course web page for a more baroque example.
- Race a yellow sphere, blue ring, and white cube. Remember the needed from/import statement at the beginning. When you create and move the objects (of types sphere, ring and box), remember that each has x, y, and z coordinates. When you create the objects, give them all the same x and y coordinates, but have them spaced by a distance of 2 in the z direction (write the creation/assignment lines separately for each object, you don’t want to use a for loop to start up the objects.) You probably want to use scene.autoscale=0 so that you can cleanly see the motion. Have the objects move in the x direction. You will want to control the animation rate using, say, rate(40). Give each object a different speed. Discuss how your programming experience went and comment on what you might like to do to improve the program (you don’t need to modify the program, just write how you would like it to look different.)
Problem 2, HWK 3, PHY307 Fall 2002
Race a yellow sphere, blue ring and white cube. Staggered by
a distance of 2 in the z direction, they breathtakingly race to the
right.
The 3 objects are intially created, then their positions are
changed by adding a small amount to each objects' x position
in a loop, whose rate is controlled by the rate() statement.
from visual import * racer1 = sphere(color=color.yellow, z=0) racer2 = ring(color=color.blue, z=2) racer3 = box(color=color.white,z=4)
for bonus kicks, draw a starting line
startline = cylinder(axis=(0,0,4),radius=0.1,color=color.red)
prevent autoscaling, so that motion can be seen more cleanly
scene.autoscale= while 1: rate(40) racer1.x = racer1.x + 0. racer2.x = racer2.x + 0. racer3.x = racer3.x + 0.