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

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

View Full Document Right Arrow Icon
6. More on Iteration (Cont’d) For-Loop Problems While-Loop Problems
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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.
Background image of page 4
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.
Background image of page 5

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

View Full DocumentRight Arrow Icon
Preliminary Notes Dot: radius r, center (x,y) y+r > 1 x+r > 1 x-r < 0 y-r < 0 “Edge Hits”
Background image of page 6
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 .
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
Refinement “Draw the black square” Draw a unit black square With lower left corner at (0,0) DrawRect(0,0,1,1,’k’)
Background image of page 9

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

View Full DocumentRight Arrow Icon
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’.
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 50

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 Right Arrow Icon
Ask a homework question - tutors are online