Clicking Points to Capture Coordinates

On the previous pages, you drew a shape based on a list of points you were given. On this page, you will automate the process of getting coordinates by clicking on the desired points on the screen.
Jasmine and Morgan start the same conversation as in the main lab, but it goes in a slightly different direction:
Take turns speaking
Morgan: Typing all these coordinates is a pain. Why can't we just sketch the letters on the screen, and then click on the corners?
Jasmine: Umm, I think they won't like us drawing on the screen. But we could find a picture of what we want to trace and import it as a stage background. We'll have to write a block that keeps waiting for a mouse click, and then adds those mouse coordinates to a list. When all the points have been entered, the block should report the list of points.
set (E) to (list of points)
Morgan and Jasmine develop code to implement their idea.
Entering points for E
(At the end of the movie, you can see that they used a click outside the stage to indicate the end of the shape.)
  1. Click here to load a starter project. Then save it.
    Complete the unfinished block definition for list of points:
    list of points{script variables(point list); set point list to (); forever{wait for click; if(click on stage?){add() to (); go to point(); stamp; if(){}}else{report()}}}
    • Fill in the missing code needed there to implement the algorithm.
    • To collect data, you need to start with an empty list. You will add a point for each mouse click.
    • Also supply all missing input values (in go to point and draw from point to point).
    • The stamp block is built into Snap!. It puts a picture of the sprite's costume on the stage at the sprite's location.
  2. Talk with Your Partner Think and write: Read the code inside the list of points reporter, and try to understand its structure. For example:
    • Why is it a reporter? Since it draws the shape as you click on its corners, isn't the job done? Why should it report a value?
    • It uses a forever block. When, and how, does it report a value?
    • What is the if else block testing, and why?
    • obsolete:
    • Can you use a script variable for the list inside list of points, or does it have to be global?
  3. Test your block several times and debug any problems.
  4. Try saving the output of list of points in a global variable and using your draw points to recreate the shape you drew. Debug any problems.
  5. Abstraction: Notice how convenient it is to have point, y coordinate of, go to point, draw from point to point, etc., already built to use the point abstract data type. Once you've created a point from the mouse coordinates, you never have to think about coordinates again in this activity.