# Lec6 - 6. More on Iteration (Cont'd) For-Loop Problems...

This preview shows pages 1–11. Sign up to view the full content.

6. More on Iteration (Cont’d) For-Loop Problems While-Loop Problems

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
DrawRect(-1,-2,6,3,’y’) DrawRect Input 3 (-1,-2) 6 A yellow 6x3 rectangle at (-1,-2) -1 -2 6 3 ‘y’ Output
DrawDisk(-1,-2,6,’m’) DrawDisk Input A magenta disk with radius 6 & center at (-1,-2) -1 -2 6 ‘m’ Output (-1,-2) 6

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Problem 3: Paintball Hits = 50 Draw a black unit square with lower left corner at (0,0). Draw a radius .03 disk with center randomly located in square.
Problem 3: Paintball Hits = 50 If the disk is entirely in square, randomly color it ‘c’, ‘y’, or ‘m’ with equal probability. Otherwise, color it White. Repeat this process until 50 white disks drawn.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Preliminary Notes Dot: radius r, center (x,y) y+r > 1 x+r > 1 x-r < 0 y-r < 0 “Edge Hits”
Preliminary Notes How we simulate a 3-way random event? If ink = rand , then 1/3 the time we have: 0 < ink < 1/3 1/3 the time we have: 1/3 <= ink < 2/3 1/3 the time we have: 2/3 <= ink < 1 Check the inequalities and do the right thing .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Pseudocode Draw black square. Repeat until 50 white disks: Locate a random disk. If the disk is in the square then randomly color it’c’, ‘y’, or ‘m’. Otherwise, color it ‘w’ end
Refinement “Draw the black square” Draw a unit black square With lower left corner at (0,0) DrawRect(0,0,1,1,’k’)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Pseudocode DrawRect(0,0,1,1,’k’) EdgeHits = 0; while EdgeHits < 50 Locate a random disk. If the disk is in the square then randomly color it’c’, ‘y’, or ‘m’.
This is the end of the preview. Sign up to access the rest of the document.

## Lec6 - 6. More on Iteration (Cont'd) For-Loop Problems...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online