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 in an incremental fashion.

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. Algorithm example Allow users to interact by adding balls to screen Use different size balls, varying between small and moderate. Pseudocode example Add mouse press function with append to the array Use random for sizing between 10 – 50px More info…

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 algorithm 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 Individual Parts of the Program
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 Combine: 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. Note the pseudocode: 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 The Bouncing Ball Class
(in examples 10-2 & 10-3) Copy/Paste the class and examine line-by-line. Then type the main program. Reason…

14 First introduced on page 133
Another look at dist() First introduced on page 133

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

16 //on page 198, this function measures distance to see if intersecting.
//it will be incorporated into Ball class 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 boolean variable tests with 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"); }

17 Example 10-4 uses the same functionality and logic by placing the intersect function into the Ball class.

18 Do Example 10-4 by modifying 10-2
…This is what you’ll be changing. In the Ball class: Add color variable: color c= color(100,50); Add the highlight() function Inside of display(): - change to fill(c) - add c =color(100,50) to reset color after ball displays 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.

19 Timing The millis() returns the number of milliseconds since a sketch started. 1,000 ms = 1 second Example 10-5 Type example 10-5 from scratch. Or Look at the example I put on class website: called “Ex10-5 with Pictures” (We’ll cover this later in Chap. 15)

20 Example 10-6 with a Remix To save time, copy the Timer class from the textbook website. Discuss what’s happening with Timer class For main program, instead of changing backgrounds, display names of classmates. I will explain Download the text file from class website

21 Main program for example 10-6 remix

22 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("830class.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);

23 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. No need to do the above in class because you did it for homework. Let’s talk about it.

24 Pages 206 – 207: 1,000 balls See pages (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). In 10-8 (from textbook), the totalDrops variable allows one to drop at a time.

25 Fancier Raindrop Type example 10-9 and let’s discuss what it is doing.
Once the visually pleasing rain is designed, it can easily be incorporated into the drop class. Calculating size of circles 2 * 2 = 4 3 * 2 = 6 4 * 2 = 8 5 * 2 = 10 6 * 2 = 12 7 * 2 = 14


Download ppt "Chapter 10 Algorithms."

Similar presentations


Ads by Google