More About Recursion - 2 Java. Looking at more recursion in Java A simple math example Fractals.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Recursion rA recursive function must have at least two parts l A part that solves a simple case of the.
Advertisements

1 Line replacement approach Koch Star (Snowflake) Area replacement approach Sierpenski Triangle Iterated Function System approach Heighway Dragon CSE
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Computer Science 111 Fundamentals of Programming I
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
40S Applied Math Mr. Knight – Killarney School Slide 1 Unit: Sequences Lesson: SEQ-L3 Drawing Fractal Patterns Drawing Fractal Patterns Learning Outcome.
CompSci Today’s topics Java Recursion in Graphics Writing a Class Simple Animation Upcoming Simulation Reading Great Ideas, Chapters 5.
On Target Brought to you by: You need pencil calculator a pencil, calculator & scratch paper scratch paper for today.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)
Homework discussion Read pages 388 – 391 Page 400: 49 – 52, 72.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L10 (Chapter 19) Recursion.
CS Data Structures Appendix 1 How to transfer a simple loop- expression to a recursive function (factorial calculation)
Linear Fractal Mountains 2 step recursive process: –Subdivide chain by creating edge midpoints –Randomly perturb midpoint positions Gen 0: Gen 1: Gen 2:
The Wonderful World of Fractals
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Recursion.
1 Excursions in Modern Mathematics Sixth Edition Peter Tannenbaum.
Introduction Introduction: Mandelbrot Set. Fractal Geometry ~*Beautiful Mathematics*~ FRACTAL GEOMETRY Ms. Luxton.
Recursive Methods Noter ch.2. QUIZ What is the result of the method call foo(4) ? 1.Prints 4 2.Prints 1 3.Prints Prints Compiler error:
Triangle Town Hi, I’m Elke. Pleased to be your guide through Triangle Town. Don’t ever worry about getting lost. I’ll stay with you the whole time.
Chapter 14: Recursion J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
L-1 Lecture 13: Functions and Design © 2000 UW CSE University of Washington Computer Programming I.
Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
A Trek through Recursion. Help Commander Data (our main method) solve his mystery? Suggestion: Draw a diagram to help yourself Data starts by asking Counselor.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
Fractals. Most people don’t think of mathematics as beautiful but when you show them pictures of fractals…
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Java Programming: From Problem Analysis to Program Design, 4e Chapter 13 Recursion.
Some Fractals and Fractal Dimensions. The Cantor set: we take a line segment, and remove the middle third. For each remaining piece, we again remove the.
Computer Graphics I, Fall 2010 Programming with OpenGL Part 3: Three Dimensions.
Recursion H-Tree and Sierpinski Triangle. order 1order 2order 3.
Data Structures Using Java1 Chapter 5 Recursion. Data Structures Using Java2 Chapter Objectives Learn about recursive definitions Explore the base case.
Fractals! Bullock Math Academy March 22, 2014 Brian Shelburne
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 3: Three Dimensions Ed Angel Professor of Computer Science,
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 6: Three Dimensions Ed Angel Professor.
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Agenda For Feb PowerPoint Presentation on Java Methods. 3. Finish Happy Face Assignment (Due by the.
Lec 15 Writing an Applet Class. Agenda Writing an Applet class Java Graphics class.
L-1 Lecture 12: Functions and Design © 2000 UW CSE University of Washington Computer Programming I.
Development of a Fractal Dimension Calculator Kelly Ran.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Notes Over 10.1 Finding the Distance Between Two Points Find the distance between the two points.
COMP 51 Week Fourteen Recursion.
Data Structures.
Lecture 24: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
CSE / ENGR 142 Programming I
Solving Problems with Circles and Triangles
Introduction to Computer Science - Alice
Java Programming: Program Design Including Data Structures
Data Structures Using Java
Programming with OpenGL Part 3: Three Dimensions
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
Coordinate Proofs Lesson 6-2.
More About Recursion Java.
Programming with OpenGL Part 3: Three Dimensions
Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Programming with OpenGL Part 6: Three Dimensions
Programming with OpenGL Part 3: Three Dimensions
An exercise on recursion
CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Programming with OpenGL Part 3: Three Dimensions
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation
Presentation transcript:

More About Recursion - 2 Java

Looking at more recursion in Java A simple math example Fractals

A simple math example Calculating n! factorial (n) = n * factorial (n-1) if n>1 factorial (1) = 1 How do we code this in Java?

Fractals Sierpinski’s Gasket Starting with an equilateral triangle, recursively drawing smaller equilateral triangles inside, by building triangles of the midpoints

SG(0)

SG(1)

SG(2)

SG(3)

SG(4)

SG(5)

// created Sierpinski's gasket fractal public void sierpinski(int level) { // precondition: there is a blank picture at least // 291 x 281 int p1X = 10; int p1Y = 280; int p2X = 290; int p2Y = 280; int p3X = 150; int p3Y = 110; Graphics g = this.getGraphics(); g.setColor(Color.RED); drawGasket(g,level,p1X,p1Y,p2X,p2Y,p3X,p3Y); }

How do we solve the problem? Well, draw the line segments for the triangle Then, if the level is at least 1, calculate the midpoints, and make the 3 recursive calls on the sub-triangles

private void drawGasket(Graphics g, int level, int x1, int y1, int x2, int y2, int x3, int y3) { g.drawLine ( x1, y1, x2, y2); g.drawLine ( x2, y2, x3, y3); g.drawLine ( x3, y3, x1, y1); if ( level > 0 ) { int midx1x2 = (x1 + x2) / 2; int midy1y2 = (y1 + y2) / 2; int midx2x3 = (x2 + x3) / 2; int midy2y3 = (y2 + y3) / 2; int midx3x1 = (x3 + x1) / 2; int midy3y1 = (y3 + y1) / 2; drawGasket ( g, level - 1, x1, y1, midx1x2, midy1y2, midx3x1, midy3y1); drawGasket ( g, level - 1, x2, y2, midx2x3, midy2y3, midx1x2, midy1y2); drawGasket ( g, level - 1, x3, y3, midx3x1, midy3y1, midx2x3, midy2y3); } } Note how we decrease the level on the recursive calls Note2: We could also use Points instead of x’s and y’s

Assignment No reading assignment from Media Computation