Math and Coding Scratch can draw graphs of linear equations. Given the coefficients of a linear equation, Scratch can calculate the points. If the pen is placed down, Scratch can draw lines connecting the points on the stage. Look at an equation: 3X + 4Y = 100. To plot this line, solve for Y: Y = –3/4X + 100/4 or Y = –.75X + 25 Scratch has a backdrop in the library that matches the coordinates and axes of the stage. Start a new Scratch project, and insert the xy-grid backdrop. Examine the backdrop. See how the X and Y axis are aligned on the stage. Notice the origin is in the center of the stage. Also insert the pencil sprite from the library, and delete the cat sprite. Save the project as MathCode05 in your working folder. Start with a value of –240 for X, which is the left edge of the grid. Add a repeat block with a value of 480. The stage is 480 pixels wide. Within the repeat block, change the X value by 1 and set Y to –.75 × X + 4. Add the pen down block before the repeat block, and run the code to draw the line. The result likely includes an additional line or two on the stage. In part, this is due to the fact that not all Y values are between 180 and –180. This can be corrected with an IF…THEN… ELSE decision to put down the pen only when the Y value is on the stage. Add code to draw the line 2X 3Y = 25. This forms a system of equations. Scratch supports graphically finding the solution for a system of equations. After both lines are drawn, use the mouse and Scratch’s coordinate display to locate the approximate point of intersection. Speculate why this is an approximate measure. As the sprite moves, the line is not drawn from the pencil lead. Speculate why this is the case. How can the realism of the program be improved? Linear Equations Goodheart-Willcox Publisher The XY-grid backdrop in the Scratch library can be used to show the coordinates of the stage. The line drawn by the code is shown in red. (X:0, Y:0) X Y (X:240, Y:0) –100 –200 100 200 100 –100 (X:0, Y:180) (X:–240, Y:0) (X:0, Y:–180) This code for drawing a line checks if the Y value is on the stage. when clicked clear pen up set x to 240 repeat 480 change x by 1 set y to / * * + 1 3 / 100 4 4 x position pen down pen up else if and 180 y position 180 y position then Copyright Goodheart-Willcox Co., Inc. 113 11 3
Previous Page Next Page