1 / 37

CSE 8A Lecture 12

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.

iolani
Download Presentation

CSE 8A Lecture 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. If-statements and pretty pictures

  3. Image courtesy of Aaron Gable, CS 5 Black

  4. Image courtesy of Aaron Gable, CS 5 Black

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

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

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

  8. Aside: Complex numbers

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

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

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

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

  13. 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?)

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

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

  16. 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)

  17. 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)

  18. 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)

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

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

  21. 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)

  22. 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)

  23. 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)

  24. 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)

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

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

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

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

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

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

  31. 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?

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

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

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

  35. Summary of Concepts • If-statements • Top-down design

  36. TODO • Reading for next class: 8.1-8.5 (Sounds) • Start PSA6 • VOTE!

More Related