8 More Practice with Iteration and Conditionals Through
- Slides: 63
8. More Practice with Iteration and Conditionals Through Graphics For-Loop Problems Introduce While-Loops Insight Through Computing
We will Draw Pictures Using Three User-Defined* Graphics Functions Draw. Rect Draw. Disk Draw. Star Rectangles Circles 5 -pointed Stars *As opposed to built-in functions like sqrt and rem. Insight Through Computing
Example Insight Through Computing
Example Insight Through Computing
Example Insight Through Computing
Example Insight Through Computing
Why? • Get more practice with loops and if. • Warm-up to using Matlab’s graphics functions which use arrays • Warm-up to writing your own userdefined functions Insight Through Computing
Question Time What is the last line of output? x = 1 y = x; while y==x && x<=4 && y<=4 x = 2*x end A. 1 Insight Through Computing B. 2 C. 4 D. 8
Draw. Rect(-1, -2, 6, 3, ’y’) Insight Through Computing
Draw. Disk(1, 3, 4, ’r’) Insight Through Computing
Draw. Star(1, 3, 4, ’g’) Insight Through Computing
A Simple 3 -line Script Draw a black square. Then a magenta disk. Then a yellow star. Insight Through Computing
Solution close all figure axis equal off hold on Draw. Rect(-1, 2, 2, 'k') Draw. Disk(0, 0, 1, 'm') Draw. Star(0, 0, 1, 'y') hold off Insight Through Computing
A General Framework close all figure axis equal off hold on Fragment involving Draw. Rect’s, Draw. Disk’s and/or Draw. Star’s hold off shg Insight Through Computing
Some Matlab Graphics Commands % Close all figure windows… close all % Open a new figure window figure % Set x and y scaling to be the % same and do not display axes axis equal off % “Add-in mode” is on… hold on IGNORE FOR NOW Insight Through Computing
Some Matlab Graphics Commands % Exit the add-in mode… hold off % Bring the figure window to the % front… shg IGNORE FOR NOW Insight Through Computing
Syntax Let’s look at the rules associated with using Draw. Rect, Draw. Disk, and Draw. Star. Insight Through Computing
Draw. Rect(-1, -2, 6, 3, ’y’) -1 -2 6 3 ‘y’ 6 3 Draw. Rect Input Insight Through Computing (-1, -2) Output A yellow 6 x 3 rectangle at (-1, -2)
Draw. Rect( Coordinates of lower left corner Insight Through Computing , , ) color length width
Color Options White Black Red Blue Green Yellow Magenta Cyan Insight Through Computing ‘w’ ‘k’ ‘r’ ‘b’ ‘g’ ‘y’ ‘m’ ‘c’
Question Time What is the area of the red region? for k=1: 3 if rem(k, 2)==1 Draw. Rect(0, 0, k, k, ’r’) else Draw. Rect(0, 0, k, k, ’w’) end A. 1 B. 3 Insight Through Computing C. 6 D. 9 % red % white
Draw. Disk(-1, -2, 6, ’m’) -1 -2 6 6 Draw. Disk ‘m’ Input Insight Through Computing Output (-1, -2) A magenta disk with radius 6 & center at (-1, -2)
Draw. Disk( Coordinates of center Insight Through Computing , , radius , ) color
Draw. Star(-1, -2, 6, ’g’) -1 -2 6 Draw. Star ‘g’ Input Insight Through Computing Output 6 (-1, -2) A green star with radius 6 and center at (-1, -2)
Draw. Star Draw. Disk( Coordinates of center Insight Through Computing , , radius , ) color
Now Let’s Solve 3 Problems Star Array Insight Through Computing Nested Stars Paint. Ball
The Framework (Again) close all figure axis equal off hold on Fragment involving Draw. Rect’s, Draw. Disk’s and/or Draw. Star’s hold off shg Insight Through Computing We Focus On this part
Problem 1: Star Array Blue 12 -by-6 rectangle with lower left corner at (0, 0). White radius-1 stars with centers at (2, 4), (4, 4), (6, 4), (8, 4) (10, 4) (2, 2), (4, 2), (6, 2), (8, 2) (10, 2) Insight Through Computing
Preliminary Notes Top Row: y = 4 Bot Row: y = 2 1 2 3 4 5 2 4 6 8 10 column x-value The x-value in the k-th column is 2 k Insight Through Computing
Pseudocode Draw the blue rectangle for k = 1: 5 Draw the kth star in the top row end for k = 1: 5 Draw the kth star in the bottom row end Insight Through Computing
Pseudocode Draw the blue rectangle for k = 1: 5 Draw the kth star in the top row Draw the kth star in the bottom row end Insight Through Computing
Refinement Draw the blue rectangle Draw the blue 12 -by-6 rectangle with lower left corner at (0, 0). Draw. Rect(0, 0, 12, 6, ’b’) Insight Through Computing
Refinement Draw the k-th star in the top row Draw a white star with radius 1 and center (2 k, 4) Draw. Star(2*k, 4, 1, ’w’) Insight Through Computing
Refinement Draw the k-th star in the bottom row Draw a white star with radius 1 and center (2 k, 2) Draw. Star(2*k, 2, 1, ’w’) Insight Through Computing
Solution Draw. Rect(0, 0, 12, 6, ’b’) for k = 1: 5 Draw. Star(2*k, 4, 1, ’w’) Draw. Star(2*k, 2, 1, ’w’) end Insight Through Computing
Problem 2: Nested Stars Draw black square with center (0, 0) & side 2. 1 Draw radius 1 magenta star with center (0, 0) Draw nested sequence of yellow and magenta stars, each with center (0, 0) and radius reduced by a factor of 1. 2. Stop when radius <=. 1 Insight Through Computing
Preliminary Notes Star #1 : Draw. Star(0, 0, 1, ’m’) Star #2 : Draw. Star(0, 0, 1/1. 2, ’y’) Star #3 : Draw. Star(0, 0, 1/(1. 2)^2, ’m’) Star #4 : Draw. Star(0, 0, 1/(1. 2)^3, ’y’) Insight Through Computing
Preliminary Notes R=1 Star #1 : Draw. Star(0, 0, R, ’m’) R = R/1. 2 Star #2 : Draw. Star(0, 0, R, ’y’) R = R/1. 2 Star #3 : Draw. Star(0, 0, R, ’m’) R = R/1. 2 Star #4 : Draw. Star(0, 0, R, ’y’) Insight Through Computing
Pseudocode Draw the Black Square R = 1; k = 1; Repeat while R > 0. 1 Draw the k-th star Update R and k Insight Through Computing
Refinement Draw the black square Draw a black square with side 2. 1 And center (0, 0) s = 2. 1; Draw. Rect(-s/2, s, s, ’k’) Insight Through Computing
Refinement R = 1; k = 1; Repeat while R >. 1 Draw the k-th star Update R and k R = 1; k = 1; while R >. 1 Draw the k-th star R = R/1. 2; k= k+1; end Insight Through Computing
Refinement Draw the kth star if k is odd Magenta , radius R. center (0, 0) otherwise Yellow, radius R, center (0, 0) Insight Through Computing
Refinement if rem(k, 2)==1 Draw. Star(0, 0, R, ’m’) else Draw. Star(0, 0, R, ’y’) end Insight Through Computing
Solution R = 1; k = 1; while R >. 1 if rem(k, 2)==1 Draw. Star(0, 0, R, ’m’) else Draw. Star(0, 0, R, ’y’) end R = R/1. 2; k= k+1; end Insight Through Computing
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. Insight Through Computing
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. Insight Through Computing
Draw. Disk(-1, -2, 6, ’m’) -1 -2 6 6 Draw. Disk ‘m’ Input Insight Through Computing Output (-1, -2) A magenta disk with radius 6 & center at (-1, -2)
Preliminary Notes y+r > 1 x-r < 0 y-r < 0 Dot: radius r, center (x, y) Insight Through Computing “Edge Hits”
Preliminary Notes How we simulate a 3 -way random event? If ink = rand(1), then 1/3 the time we have: 0 < ink < 1/3 <= ink < 2/3 <= ink < 1 Check the inequalities and do the right thing. Insight Through Computing
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 Insight Through Computing
Refinement “Draw the black square” Draw a unit black square With lower left corner at (0, 0) Draw. Rect(0, 0, 1, 1, ’k’) Insight Through Computing
Pseudocode Draw. Rect(0, 0, 1, 1, ’k’) Edge. Hits = 0; while Edge. Hits < 50 Locate a random disk. If the disk is in the square then randomly color it’c’, ‘y’, or ‘m’. Otherwise, color it ‘w’ Edge. Hits = Edge. Hits + 1; end Insight Through Computing
Variable Definition We use a variable Edge. Hits to keep track of the number of disks that intersect the square’s boundary. Insight Through Computing
Refinement “Locate a random disk” The center (x, y) satisfies 0<x<1 and 0<y<1. x = rand; y = rand; Insight Through Computing
Refinement If the disk is in the square then randomly color it’c’, ‘y’, or ‘m’. Otherwise, color it ‘w’ Edge. Hits = Edge. Hits + 1; end How do we check that? Insight Through Computing
None of these conditions hold. y+r > 1 x-r < 0 y-r < 0 Insight Through Computing Dot: radius r, center (x, y)
All of these conditions hold. y+r <= 1 x-r >= 0 y-r >= 0 Insight Through Computing Dot: radius r, center (x, y)
All of these conditions hold. y+r <= 1 x-r >= 0 y+r<=1 && x-r>=0 && y-r>=0 Insight Through Computing
Question Time Want to count upper right corner hits. Which of these boolean conditions guarantees that (1, 1) is covered? (i) (ii) x + r >= 1 && y + r >= 1 x + y >= 2 - 2*r A. Neither B. (i) only Insight Through Computing C. Both D. (ii) only
Answer. Time (i) x + r >= 1 && y + r >= 1 (ii) x + y >= 2 – 2*r A. Neither B. (i) only C. Both D. (ii) only The case x = 1, y = 1 – r -. 000001, fools Condition (ii). Insight Through Computing
Refinement If the disk is in the square then randomly color it ’c’, ‘y’, or ‘m’. Otherwise, color it ‘w’ Edge. Hits = Edge. Hits + 1; end How do we do that? Insight Through Computing
Refinement randomly color it ‘c’, ‘y’, or ‘m’ 1/3 of the time the disk should be ‘y’ 1/3 of the time the disk should be ‘c’ Insight Through Computing
Refinement ink = rand(1); if ink < 1/3; Draw. Disk(x, y, r, ’m’) elseif 1/3 <= ink && ink < 2/3 Draw. Disk(x, y, r, ’y’) else Draw. Disk(x, y, r, ’c’) end Insight Through Computing
- More more more i want more more more more we praise you
- More more more i want more more more more we praise you
- What is the conversion of timber
- Human history becomes more and more a race
- Sin entered through one man
- Furcation involvement classification
- Who wrote night of the scorpion
- Selection sequence iteration
- Both conditional statements and iterative statement
- Repetition in pseudocode
- Similarities between iteration and recursion
- The more you take the more you leave behind
- The more you study the more you learn
- Aspire not to
- Law of inertia example
- Knowing more remembering more
- The more i give to thee the more i have
- More choices more chances
- The two following
- Conditionals exercises 0 1 2 3
- What is present real
- Conditional and time clauses
- Future time clauses present perfect
- Conditionals and biconditionals
- Present unreal conditional sentences
- Conditional clause formula
- Write each sentence as a conditional
- Future conditional sentences
- Conditionals and biconditionals
- Conditional sentence type 0 1 2 3
- Practice assessor and practice supervisor
- Lesson 7-3 multiplication properties of exponents answers
- 7-3 multiplication properties of exponents
- 7-3 practice more multiplication properties of exponents
- S'more standard to slope-intercept practice answers
- Let us practice more
- More multiplication properties of exponents quiz part 1
- Use the properties of exponents to rewrite each expression
- Product of powers
- Transforming teacher education through clinical practice
- Newton raphson method
- 4115
- Recurrence relation solver
- Iteration vs recursion
- Value iteration
- Markov decision process
- Orthogonal iteration
- Alan fern
- Tai sing lee
- What is the purpose of an iteration recap
- Iteration in mathematics
- What is iteration in computer science
- Recursion
- Iteration definition computer science
- Which statement is true about iteration goals?
- Birge vieta method examples
- Iteration variable
- Final iteration definition
- Value iteration
- Value iteration
- Lanczos iteration
- Embarrassingly parallel
- Iteration abstraction
- Jurassic park iterations