(Also available in WeScheme)

Students use function composition and the distance formula to detect when characters in their games collision.

 Lesson Goals Students will be able to: Explain how the distance formula is related to the Pythagorean theorem. Write a function for the distance formula. Student-Facing Lesson Goals I can explain how the distance formula is connected to the Pythagorean theorem. I can write a function that takes in 2 points and returns the distance between them. Materials

## 🔗Problem Decomposition Returns! 20 minutes

### Overview

Students revisit problem decomposition - the idea of breaking down a complex problem into simpler pieces, solving those pieces separately, and then composig the solutions to solve the original..

### Launch

"Problem Decomposition" is a powerful tool, which lets us break apart complex problems into simpler ones that we can solve, test, and then glue together into a complex solution.

Students may remember that there are two strategies for doing this:

1. Top-Down: Describe the problem at a high level, then fill in the details later

2. Bottom-Up: Focus on the smaller parts that you’re sure of, then build them together to get the big picture

### Investigate

For the following complex word problem, have students first decide which strategy they want to use, and then apply the Design Recipe to build the functions they need.

• A retractable flag pole starts out 24 inches tall, and can grow at a rate of 0.6in/sec. An elastic is tied to the top of the pole and anchored 200 inches from the base, forming a right triangle. Write a function that computes the area of the triangle as a function of time.

• This is easier to think about as two functions:

• one that computes the height of the pole, based on the seconds

• another that computes the area of the triangle, based on the height

• Does one function depend on (or "sit on top of") the other? If so, which one?

• Yes - `area` depends on `height`.

• Which strategy will YOU use: bottom-up (independent first) or top-down (dependent first)?

• Students answers will vary! They can define either function first.

• Complete Top Down / Bottom Up, using your chosen strategy for Problem Decomposition!

### Synthesize

Note: Defining the `height` first is bottom-up, and solving `area` first is top-down.

• Which strategy did students use?

• Did they try starting with one function, and then switch to another?

• Invite students to share. Oftentimes, responses are not only intriguing but can highlight the value of each approach. Explicitly point out that the `area` function _uses `height`, allowing us to break a big problem down into two smaller ones._

## 🔗Collision Detection 20 minutes

### Overview

Students once again see function composition at work, as they compose a simple inequality with the `distance` function they’ve created.

### Launch

Knowing how far apart our characters are is the first step. We still need the computer to be asking: "True or False: is there a collision?"

### Investigate

• This is easier to think about as two functions:

• one that computes the distance between a Player and a Character, based on their coordinates

• another that checks if those same coordinates are less than 50 pixels apart, based on the distance

• Does one function rely on the other? If so, which one?

• Complete Word Problem: is-collision.

• When you’ve finished, open your saved game file and fix the @code function in your game file, and click "Run"!

### Synthesize

• In our flag-pole exercise, we had to write two functions and could start with one or the other. In our game, however, we began already having written the `distance` function! Is this Top-Down or Bottom-Up decomposition?

• Explicitly point out that the `is-collide` function uses `height`, allowing us to break a big problem down into two smaller ones.

• Connect this back to `profit` (from Problem Decomposition), which relied on `revenue` and `cost`. Which function(s) would a top-down strategy address first?

• Connect this back to `is-onscreen` (from Sam the Butterfly), which relied on `is-safe-left` and `is-safe-right`. Which function(s) would a bottom-up strategy address first?

## 🔗Additional Exercises:

For teachers who’ve already introduced your class to flags, Flag of Trinidad and Tobago Starter File makes use of Pythagorean Theorem and could make for an interesting connection to this lesson.

These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, and 1738598). Bootstrap by the Bootstrap Community is licensed under a Creative Commons 4.0 Unported License. This license does not grant permission to run training or professional development. Offering training or professional development with materials substantially derived from Bootstrap must be approved in writing by a Bootstrap Director. Permissions beyond the scope of this license, such as to run training, may be available by contacting contact@BootstrapWorld.org.