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

Info icon This 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
Image of page 1

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

View Full Document Right 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
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
Image of page 3

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

View Full Document Right Arrow Icon
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.
Image of page 4
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.
Image of page 5

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

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

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

View Full Document Right 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
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’)
Image of page 9

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern