Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 10 Algorithms.

Similar presentations


Presentation on theme: "Chapter 10 Algorithms."— Presentation transcript:

1 Chapter 10 Algorithms

2 In this Chapter… Learn how to construct a program that does more than one thing. Employ cumulative learning of drawing shapes, interactivity, variables, loops, conditions, functions, objects, arrays, and more. Think of what YOU want to do with your projects and beyond… The primary strategy is to break down into smaller programs; then integrate together in the end.

3 What to Expect The chapter uses a generic example that serves as a good guide for creating larger programs. It is a game; albeit simple. It involves: Interactivity Multiple objects (using arrays) A goal We will go through chapter bit by bit.

4 A couple of definitions
algorithm – A logical sequence of steps required to perform a task. pseudocode – An outline of a program, written in a form that can easily be converted into real programming statements.

5 The algorithm process Idea – Start with an idea
Parts – break into smaller parts Write the pseudocode for each part Implement the algorithm with code Take the and functionality of each algorithm and build a class Integration – Integrate all classes into a larger program/algorithm.

6 An Example - Sum a list of numbers:
The algorithm might be: Create variable to hold Sum Create Counter and set it at 0 Create a loop that cycles through the array. Each time: Calculate the Sum of the first item Increase the counter by 1 Solution saved in Sum Display solution

7 Then translate algorithm to code by using while() or for()
int [] nums = {3, 10, 1 }; int sum = 0; int i = 0; while(i < nums.length) { sum = sum + nums[i]; i++ ; } println(sum); text(sum, 10, 20);

8 This chapter breaks down the solution even more
Developing an idea Breaking down idea Working out algorithm for each part Writing code for each part Creating algorithm for putting parts together Integration of parts. You should use this strategy in all of your projects…

9 Description of Rain Game
The object of the game is to catch raindrops before they hit the ground. New drops will drop from the top of the screen from time to time. They will be located randomly across the screen. The player must catch the raindrop with the mouse before they hit the bottom of the screen.

10 The Smaller Parts *Think about elements (nouns usually)
*Think about behavior (verbs usually) Program a circle controlled by the mouse to catch rain A collision detection program. If the rain catcher catches a raindrop. A timing program that executes a function every N seconds A program with circles falling from top to bottom

11 Without a class would be
void setup() { size(400, 400); } void draw() { background(255); stroke(0); fill(170); ellipse(mouseX, mouseY, 64, 64); INSTEAD… lets use Example 10-1 to create a Catcher class.

12 If ball hits edge, reverse direction
Before doing the intersection test, let’s simply get the bouncing ball class. The algorithm for it is: x and y locations radius Speed for x and y Constructor: Set radius with argument Randomly pick location Randomly pick speed Increment x by speed in x direction Increment y by speed in y direction If ball hits edge, reverse direction Display by drawing circle at x and y location

13 Compare distance with sum of radii
If the distance between the center of circle1 and circle 2 is less than the sum of the radius of circle 1 and circle 2, then they intersect.

14 //on page 198, this function measures distance to see if intersecting.
boolean intersect(float x1, float y1, float x2, float y2, float r1, float r2) { float distance = dist(x1, y1, x2, y2); if (distance < r1+r2) { return true; }else { return false; } //on page 198, this function tests it with a specific objects ball 1 and ball 2 boolean intersecting = intersect(ball1.x, ball1.y, ball2.x, ball2.y, ball1.r, ball2.r); if(intersecting) { println("The circles are intersecting"); }

15 Take a look of my copy of adding intersect() & intersecting() to the main page…
Copy your example 10_2 folder and duplicate it. We will be modifying it. Open your example10_2 and add intersect and intersecting to the main page.

16 Do Example 10-4 by modifying 10-2
FYI, this is what you’ll be changing. In the Ball class: Add color variable: color c= color(100,50); Highlight() function Inside of void display() add fill(c) and c = color(100,50); Add the boolean intersect() function as shown on page 201 In the main page: Add an if statement for if intersect(); then highlight() For an extra challenge, have the word “POW” appear on the page when they intersect.

17 We will do example 10-5 instead.
Timer The millis() returns the number of milliseconds since a sketch started. * One millisecond is 1 one thousandth of a second. * So 1,000 ms = 1 second * 300 ms = about a 3rd of a second An example is to change the background every 2 seconds. void draw() { if (millis() > 2000 ) { background(0); } } We will do example 10-5 instead.

18 A 2nd example of importing text
//Another Example of text display String [] people; int x= 0; int y= 10; void setup() { size(200,300); people = loadStrings("830people.txt"); } void draw() { fill(#3355CC, 20); rect(0,0,width, height); for(int i = 0; i < people.length; i++) { fill(#654321); text(people[i], 10, y ); y += 15; noLoop(); //keeps the names from repeating println("There are "+ people.length ) ; printArray(people);

19 This is example 10-7, but it’s best as a class
float x, y; void setup() { size(400,400); background(100); x = width/2; y = 0; } void draw() { background(#ccff99); fill(#00ff55); noStroke(); ellipse(x, y, 16, 16); y++; }} Instead, type the class as shown on page 205. Then do exercise 10-3 in order to use the class.

20 Pages 206 – 207: 1,000 balls See pages 206-207.
(no need to type this time) Notice all balls drop at once although it seems like they don’t. This is due to varied speed at random(1, 5) and frameRate; In 10-8 (from textbook), the totalDrops variable allows one to drop at a time.

21 Fancier Raindrop Type example 10-9 and let’s discuss what it is doing.
Once the visually pleasing rain is designed, it can nicely be incorporated into the drop class.


Download ppt "Chapter 10 Algorithms."

Similar presentations


Ads by Google