Page 136 - Learn To Program With Scratch
P. 136

Drawing an n-Leaved Rose

         N-LeavedRose .sb2  In this example, we’ll create an application that draws a rose with multiple
                         leaves on the Stage. The rose-drawing process can be broken down into the
                         following steps:
                         1.  Start at the origin of the Stage.
                         2.  Point the sprite in some direction. By convention, the Greek letter θ
                             (pronounced theta) represents an angle, so we’ll name the variable
                             for the sprite’s direction theta.
                         3.  Move the sprite r steps and draw a single point on the Stage. After that,
                             lift the pen up and return to the origin.
                         4.  Change the angle theta by some amount (we’ll use 1°) and repeat
                             steps 2–4.

                             The relation between the distance r and the angle theta is given by
                                                   r = a × cos(n × θ)
                         where a is a real number and n is an integer. This equation produces a rose
                         whose size and number of leaves are controlled by a and n, respectively. This
                         equation also involves the cosine trigonometric function (cos), which you’ll
                         find as a reporter block in the Operators palette (check the sqrt block). Given
                         the values of a and n, all we have to do is choose different values for theta,
                         calculate the corresponding values of r, and mark the resulting points on
                         the Stage. The user interface for this example is shown in Figure 5-30.























                         Figure 5-30: User interface for the n-leaved rose application

                             The application contains two sprites: The first sprite has the Redraw but-
                         ton costume, and the second sprite (called Painter) is a hidden sprite that
                         draws the rose. The user controls the number of desired leaves by chang-
                         ing n with the slider control and then clicks the Redraw button to draw the





           114   Chapter 5
                                           www.it-ebooks.info
   131   132   133   134   135   136   137   138   139   140   141