Time for action - draw a graph

We want to graph each day and newAmount pair in our calculation. To help accomplish this task, we'll add a broadcast block to the end of the forever if block.

  1. With the double sprite selected, add a broadcast block as the last block in the forever if block. Add a second broadcast block before the forever if block starts so that we make sure we graph day 1.
  2. Create a new broadcast message named graph, and select it in both broadcast blocks.
  3. For every broadcast we send, we need to program something to receive it. From the Control palette, add a when I receive block to the double sprite. Make sure graph is set in the drop-down menu.
  4. In our graph, days will be represented by an x value and the newAmount will be represented by a y value. From the Motion palette, add a change y by block and a change x by block.
  5. We'll let each day represent ten pixels on our Scratch stage, so enter the number 10 as the value of the change x by block.
  6. For the y value, we need a way to limit the height of our graph, so we're going to conduct a calculation on the newAmount value before we graph it. From the Operators palette, add the mathematical functions block. This block displays the sqrt function by default, as seen in the following screenshot:
    Time for action - draw a graph
  7. From the drop-down list in the mathematical functions block, choose the natural logarithm notation, ln.
  8. Add the newAmount variable to the input of the mathematical functions block.
  9. Add the stamp block to the change x by block. The following screen shot shows what our scripts look like for the double sprite.
    Time for action - draw a graph
  10. Press the Space bar to draw a graph.
Time for action - draw a graph

Experiment with setting different values for startAmount and days and observe the graph. Depending on the values you use, your graph may look a little different than my screenshot.

What just happened?

You did look at the values reported in the newAmount monitor block, didn't you? If I gave you three pesos and doubled it every day for the next 15 days, you'd end up with 49,152 pesos. Wow.

We used a few tricks to graph each days and newAmount pair. For each day in our calculation, we moved to the right by ten pixels. This number works because we will never calculate more than 30 days, which means, we need 300 pixels on the X axis to draw our biggest graph.

The Y axis presented a problem because of the larger numbers assigned to the newAmount variable. That's why we calculated our y value by taking the natural log of newAmount.

If you calculated a value using 30 days and a startAmount of 5, then you realized there wasn't enough room on the stage for the last two days on our graph. For this game, we were willing to live with that.

Finally, we stamped each point on the graph so that when the double sprite moved to the next coordinate, we plotted a visible curve.

Mathematical functions

Scratch provides several built-in trigonometry functions to help us perform mathematical calculations:

  • sqrt (square root)
  • abs (absolute value)
  • sin (sine)
  • cos (cosine)
  • tan (tangent)
  • asin (arcsine)
  • acos (arccosine)
  • ln (natural logarithm)
  • log (log base 10)
  • e^ (exponential)
  • 10^ (raise 10 to a power)

Create patterns with stamp

The stamp block in the Pen palette creates an impression of the sprite that remains even after the sprite changes. It can be useful to create a history of the sprite's appearance or location. Or, we can use the stamp block to create a pattern, like we did with our graph.

If you look at the help topic for the stamp block, you'll see a different example.

Create patterns with stamp

Pop quiz

  1. Which of the following mathematical functions does Scratch make available for use in your scripts:
    • Square root
    • Natural logarithm
    • Cosine
    • All of the above
  2. The stamp block does what?
    • Organizes your stamp collection
    • Chases another sprite around the stage
    • Creates a lasting impression of the sprite
    • Transforms the sprite based on a graphic effect

Connect the dots

We're going to take our graph to the next level and connect each point with a line. Let's get right to it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.19.220