Strict Standards: Declaration of Doku_Renderer_metadata::table_open() should be compatible with Doku_Renderer::table_open(\$maxcols = NULL, \$numrows = NULL, \$pos = NULL) in /home2/cp-wiki/htdocs/inc/parser/metadata.php on line 480

Strict Standards: Declaration of Doku_Renderer_metadata::table_close() should be compatible with Doku_Renderer::table_close(\$pos = NULL) in /home2/cp-wiki/htdocs/inc/parser/metadata.php on line 480

# CS 190C LAB 5: Friday, February 13, 2009

## Demo Problems

### Random Grid Visualization

```from visual import *
from random import randint

def visualize(grid):
global scene

#hide all the objects in the scene
for object in scene.objects:
object.visible = False

rows = len(grid)                        # get number of rows
cols = len(grid[0])                     # get number of cols

for row in range(rows):
for col in range(cols):

# Choose the color for the box.  Colors are specified in
# (r,g,b) notation.
if grid[row][col] == 1:
color = (1,0,0)             # make the box red
else:
color = (0,0,1)             # make the box blue

# draw a box with position (x,y,z), where
#    x=col (left (-1) to right (+1) displacement)
#    y=-row (up  (+1) to down (-1) displacement;
# and the given color.
box(pos=(col, -row, 0), color=color)

def random_grid(n):
grid = []                               # intialize grid
for row in range(n):
rowlist = []                        # make an empty row
for col in range(n):
num = randint(0,1)              # fill out the row with
rowlist.append(num)             # random numbers

grid.append(rowlist)                # add the row to the grid

return grid

if __name__ == "__main__":
scene = display(                        # 3D window parameters
title="Random grid visualization",  # Window title
autocenter=True                     # Automatically center object
)

grid_size = 10
total_red, total_blue = 0, 0
label = label(pos=(grid_size/2, 2), text="Ratio of red:blue = 0")
for i in range(grid_size):
grid = random_grid(grid_size)       # Make a random 20x20 grid
visualize(grid)                     # Visualize the grid

count_red = 0                       # Count the red (=1) squares
for row in grid:
count_red = count_red + sum(row)

count_blue = (grid_size**2) - count_red
# Compute the blue (=0) squares
total_red += count_red
total_blue += count_blue
label.text = "Ratio of red:blue = " + str(float(total_red)/total_blue)
label.visible = True

rate(.5)                            # Allow at most 1/2 loop iterations
#  per second
#  (one frame per 2 seconds)```

## In-Lab Problem

Download this file to use as a starting point: lab5.py. Your job is to take this Monte Carlo estimation of pi (which you did in problem set 2) and use VPython to animate how the points as you generate them. Here is an example of how it would look, although it's still-image, not animated:

The VPython primitives used are: curve for the circle arc, arrow for the axes, and sphere for the points.

One way to use curve to graph the quarter circle is to use parametric equations. As you can see from the link, to graph a circle, the formula for each x and y point would be (r*cos(t), r*sin(t)). Since we're only worried about the first quadrant of the circle, then t should vary between 0 and pi/2. So start by making an array of t values like so:

`t = arange(0, math.pi/2, 0.01)`

Now, when you specify the points to the curve function, you need to do some sort of manipulation on t (the formula above) to get what you want. You can use the cos function to take the cosine of the entire array (not math.cos) and simply multiply that by 200 to get an array of x values that contain what you want. VPython documentation is here.

cs190c/lab5_09.txt · Last modified: 2009/02/13 11:58 by tang