370 likes | 530 Views
CSE 8A Lecture 12. Reading for next class: 8.1-8.5: Sounds! Today’s topics: Building more complex programs (LIVE CODING!) Start PSA 6: Chromakey!. A vote for CSE 8A Yen Chuan (Allen) Liu and Pin (Arthur) Chu. If-statements and pretty pictures. Image courtesy of Aaron Gable, CS 5 Black.
E N D
CSE 8A Lecture 12 • Reading for next class: 8.1-8.5: Sounds! • Today’s topics: • Building more complex programs (LIVE CODING!) • Start PSA 6: Chromakey! A vote for CSE 8A Yen Chuan (Allen) Liu and Pin (Arthur) Chu
The Mandelbrot Set Consider the following update rule for all complex numbers c: z0 = 0 zn+1 = zn2 + c If z does not diverge, c is in the M. Set. Benoit M. Imaginary axis z3 c z2 z1 z4 Real axis z0
The Mandelbrot Set Consider the following update rule for all complex numbers c: z0 = 0 zn+1 = zn2 + c If z does not diverge, c is in the M. Set. Benoit M. Imaginary axis z3 c z2 z1 z4 z0 Real axis example of a non-diverging cycle
The Mandelbrot Set Consider the following update rule for all complex numbers c: z0 = 0 zn+1 = zn2 + c The shaded area are points that do not diverge.
Challenge: Write the code to draw the Mandelbrot set (Today and Tuesday) Objective: Write a method that creates a new Picture object and plots the Mandelbrot set in the range (-2.0, -1.0) to (1.0, 1.0) Allow the user to specify the size of the Picture
CS Concept: Top-Down Design Objective: Write a method that creates a new Picture object and plots the Mandelbrot set in the range (-2.0, -1.0) to (1.0, 1.0) Allow the user to specify the size of the Picture Where to begin? • Determine the input and output • Develop an algorithm • Write the code
Challenge: Write the code to draw the Mandelbrot set Input and output Input: Output: User-specified width Point (0, 0) in Pixel space Point (-2.0, 1.0)in the complex plane
Challenge: Write the code to draw the Mandelbrot set Input and output Input: Width of picture (why not height too?) Output: A Picture object with the Mset drawn User-specified width Point (0, 0) in Pixel space Point (-2.0, 1.0)in the complex plane
CS Concept: Algorithm development Algorithm What sub-tasks can you think of that need to be solved? (Hint: think about what information you have or can get easily, and what information you need. E.g., it’s easyto loop through Pixels in the Pixel space, but how can yourelate this to a point in coordinate space?)
CS Concept: Algorithm Design/Pseudocode What is wrong with the following algorithm? • Calculate the height of the picture, call this height • Create a new Picture with the given width and height • For x from 0 to width: • For y from 0 to height: • Get the Pixel, p, at position (x, y) • If the point (x, y) is in the Mset, color p black • Return the newly created Picture • It needs an else condition to specify what happens if (x, y) is not in the set • x and y specify locations in the imagenot coordinates in the complex plane • The loop will not cover the whole picture • Nothing, it will plot the Mandelbrot set just fine.
Challenge: Write the code to draw the Mandelbrot set Algorithm for plotMset(int width): • Calculate the height of the picture, call this height • Create a new Picture with the given width and height • For x from 0 to width: • For y from 0 to height: • Get the Pixel, p, at (x, y) • Calculate (real, imag), the point in the complex plane corresponding to (x, y) • If (real, imag) is in Mset Color Pixel p black • Return the newly created Picture
Challenge: Write the code to draw the Mandelbrot set Algorithm for plotMset(int width): • Calculate the height of the picture, call this height Say width is 120, What should the height be? 120 • 60 • 80 • 90 • 120 • None of these (-2.0, 1.0) (1.0, -1.0)
Challenge: Write the code to draw the Mandelbrot set Algorithm for plotMset(int width): • Calculate the height of the picture, call this height What is the general formula for calculatingthe height from the width? width (-2.0, 1.0) (1.0, -1.0)
Challenge: Write the code to draw the Mandelbrot set Algorithm for plotMset(int width): • Calculate the height of the picture, call this height What is the general formula for calculatingthe height from the width? width (-2.0, 1.0) so (1.0, -1.0)
Challenge: Write the code to draw the Mandelbrot set Algorithm for plotMset(int width): • Calculate the height of the picture, call this height • Create a new Picture with the given width and height • For x from 0 to width: • For y from 0 to height: • Get the Pixel, p, at (x, y) • Calculate (real, imag), the point in the complex plane corresponding to (x, y) • If (real, imag) is in Mset Color Pixel p black • Return the newly created Picture
Challenge: Write the code to draw the Mandelbrot set Algorithm for plotMset(int width): • Calculate the height of the picture, call this height • Create a new Picture with the given width and height • For x from 0 to width: • For y from 0 to height: • Get the Pixel, p, at (x, y) • Calculate (real, imag), the point in the complex plane corresponding to (x, y) • If (real, imag) is in Mset Color Pixel p black • Return the newly created Picture
Challenge: Write the code to draw the Mandelbrot set Algorithm for plotMset(int width): Calculate (real, imag), the point in the complex plane corresponding to (x, y) (0, 0) (-2.0, 1.0) (x, y) (1.0, -1.0) Calculate (real, imag) (height, width)
Challenge: Write the code to draw the Mandelbrot set Algorithm for plotMset(int width): Calculate (real, imag), the point in the complex plane corresponding to (x, y) • First, a question: • Which of the following best describes • how you feel about algebra? • I love it! • Eh, take it or leave it, no big deal • It’s OK, but it makes me a little uncomfortable • I hate it! I find it really confusing! • Other (0, 0) (-2.0, 1.0) (x, y) (1.0, -1.0) Calculate (real, imag) (height, width)
Challenge: Write the code to draw the Mandelbrot set Algorithm for plotMset(int width): Calculate (real, imag), the point in the complex plane corresponding to (x, y) (0, 0) (-2.0, 1.0) (x, y) (1.0, -1.0) Calculate (real, imag) (height, width)
Challenge: Write the code to draw the Mandelbrot set Algorithm for plotMset(int width): Calculate (real, imag), the point in the complex plane corresponding to (x, y) (0, 0) (-2.0, 1.0) (x, y) (1.0, -1.0) Calculate (real, imag) (height, width)
Challenge: Write the code to draw the Mandelbrot set Algorithm for plotMset(int width): • Calculate the height of the picture, call this height • Create a new Picture with the given width and height • For x from 0 to width: • For y from 0 to height: • Get the Pixel, p, at (x, y) • Calculate (real, imag), the point in the complex plane corresponding to (x, y) • If (real, imag) is in Mset Color Pixel p black • Return the newly created Picture
Remember the update rule… Consider the following update rule for all complex numbers c: z0 = 0 zn+1 = zn2 + c If z does not diverge (i.e., absolute value stays under 2.0) after 25 iterations,it is in the Mandelbrot set. The shaded area are points that do not diverge.
public static boolean inMset( double real, double imag ) { double zReal = 0; double zImag = 0; double realUpdate; int numIter = 0; double absValZ = Math.sqrt(zReal*zReal + zImag*zImag); while (___________(1)__________________) { realUpdate = zReal*zReal-zImag*zImag + real; zImag = zReal*zImag*2+ imag; zReal = realUpdate; absValZ = Math.sqrt(zReal*zReal + zImag*zImag); numIter++; } if (______(2)____________) { return true; } else { return false; } } • What line of code should go in blank (1) • (more than one answer will work, but one is “more efficient”) • absValZ < 2.0 • numIter < 25 • absValZ < 2.0 || numIter < 25 • numIter < 25 && absValZ < 2.0 • absValZ > 2.0 || numIter > 25
public static boolean inMset( double real, double imag ) { double zReal = 0; double zImag = 0; double realUpdate; int numIter = 0; double absValZ = Math.sqrt(zReal*zReal + zImag*zImag); while ( absValZ < 2.0 && numIter < 25 ) { realUpdate = zReal*zReal-zImag*zImag + real; zImag = zReal*zImag*2+ imag; zReal = realUpdate; absValZ = Math.sqrt(zReal*zReal + zImag*zImag); numIter++; } if (______(2)____________) { return true; } else { return false; } } • What line of code should go in blank (2) • absValZ < 2.0 • absValZ >= 2.0 • numIter >= 25 • numIter >= 25 && absValZ >= 2.0 • numIter == 25 && absValZ < 2.0
CS Concept: Booleans are values if ( absValZ < 2.0 ) { return true; } else { return false; } • Which of the following is equivalent to the above code? • return absValZ; • return absValZ < 2.0; • return absValZ >= 2.0; • None of these
Challenge: Write the code to draw the Mandelbrot set Algorithm for plotMset(int width): • Calculate the height of the picture, call this height • Create a new Picture with the given width and height • For x from 0 to width: • For y from 0 to height: • Get the Pixel, p, at (x, y) • Calculate (real, imag), the point in the complex plane corresponding to (x, y) • If (real, imag) is in Mset Color Pixel p black • Return the newly created Picture
public static Picture plotMSetAns( int width ) { int height = (int)(2.0/3.0 * width); Picture newPic = new Picture(width, height); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Pixel p = newPic.getPixel(x, y); double real = ((3.0*x) / width) - 2.0; double imag = 2.0*(height - ((double)y))/height - 1.0; if (inMset( real, imag )) { p.setColor( new Color( 0, 255, 0 ) ); } } } return newPic; } What inefficiencies can you find in this code? How could you make it more general? How could you put it on a t-shirt?
Solo: (30 sec) • Discuss: (2 min) • Group: (20 sec) Chromakey:Visit strange new places 1 2 person (before) person (after) background In order to create modify the person picture so that it looks like the after picture, from which pictures to you take the pixels in the boxes? • Box 1 (yellow) from background, box 2 (green) from person • Box 1 from background, box 2 from background • Box 1 from person, box 2 from person • Box 1 from person, box 2 from background • None of the above
Solo: (30 sec) • Discuss: (30 sec) • Group: (20 sec) How will you know which part (yellow box or green) you are in? • Condition on the Pixel coordinates(not the color) • Condition on the Pixel colors in background • Depends on the Pixel colors in person • Depends on the Pixel colors in background, compared to those at the same coordinates in person person (before) background 1 2
Your chromakey • Call two chromakey methods: • One to replace background • One to replace tShirt • If there 2 or more people in the picture, you can call more methods – one to replace each tShirt with a different background if you like public void chromaKeyBlue(Picture newPicToReplaceBlue, int blueThreshold) for (int x = 0; x < this.getWidth(); x++) for (int y = 0; y < this.getHeight(); y++) { //Do some stuff } OK to edit pictures before the code runs
Summary of Concepts • If-statements • Top-down design
TODO • Reading for next class: 8.1-8.5 (Sounds) • Start PSA6 • VOTE!