# 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 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 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.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern