February 19, 2020

Scratch is funny

We can make movies with Scratch

Things move, appear or disappear

But is hard to handle biological data

On the other hand R works well with data

But it is hard to make movies and games in R

What do they have in common?

Turtle Graphics

Turtle Graphics in 1967

Created by Seymur Papert and Cynthia Solomon

MIT’s Artificial Intelligence Laboratory

… to teach children …

… how to think with computers

You can make your own turtle

You can have your “turtle” in Scratch

In this version of Scratch, the Turtle is called Pen

It is an extension that you can use

Instead of a Turtle you get a Cat

The cat can move

(same as before)

Move the Cat Forward or Backward
move (10) steps
Turn (Rotate) the Cat
turn cw (15) degrees
turn ccw (15) degrees
Set the Turtle’s Position and Direction
go to x:(0) y:(0)
point in direction (90 v)

The cat can draw

This is new

Pen commands
pen up
pen down
Display Options
set pen color to (0)
set pen size to (1)

Where is the Turtle?

Get the Turtle’s Current Position and Direction
(direction)
(x position) (y position)
Show or Hide the Turtle
hide
show

Exercise

Write a script that make the cat do the following:

  • put the cat at coordinates (0,150)
  • hide the cat
  • clear any existing drawing
  • put the pen down
  • point in direction 130 degrees
  • repeat 40 times
    • move 200 steps
    • turn right 130 degrees

Drawing Stars

Let’s draw a star using Turtle graphics in Scratch!

This part is on the Scratch webpage

Key parts of computational thinking

Decomposition
breaking down a complex problem or system into smaller parts
Pattern Recognition
looking for similarities among and within problems
Abstraction
focusing on the important parts only, ignoring irrelevant detail
Algorithms
developing a step-by-step solution to the problem

You can also have a turtle in R

You can install TurtleGraphics in R with this command

install.packages("TurtleGraphics")

Then you load into the session using

library(TurtleGraphics)

Now you start a new “terrarium” with

turtle_init()

Turtle lives in a terrarium

Set Up a new Terrarium

turtle_init(width = 100, height = 100, mode = "error")
width
numeric; plot width.
height
numeric; plot height.
mode
character string; one of “error”, “clip”, or “cycle”.
We will explore these options later

The turtle can move

Move the Turtle Forward or Backward
turtle_move(10) move (10) steps
Turn (Rotate) the Turtle
turtle_right(15) turn cw (15) degrees
turtle_left(15) turn ccw (15) degrees
Set the Turtle’s Position and Direction
turtle_goto(0,0) go to x:(0) y:(0)
turtle_setangle(90) point in direction (90 v)
turtle_setpos(0,0) Move without drawing

She can draw and hide

Pen commands
turtle_up() pen up
turtle_down() pen down
Display Options
turtle_col(0) set pen color to (0)
turtle_lwd(1) set pen size to (1)
turtle_lty(1) Change line type
Show or Hide the Turtle
turtle_hide() hide
turtle_show() show

Where is the Turtle?

Get the Turtle’s Current Position and Direction
turtle_getangle() (direction)
turtle_getpos() (x position) (y position)

“Turbo” Mode

Evaluate a Larger Portion of Turtle Drawing Code

  • turtle_do({code here})

Let’s redo the stars in R

By default the terrarium size is 100,100

turtle_init()

In R, the turtle is not at (0,0)

turtle_getangle()
angle 
    0 
turtle_getpos()
 x  y 
50 50 

So we have to adapt our code (how?)

Terrarium has borders

turtle_move(80)
Error in .turtle_draw_error(distance, curX, curY, curAng, curGp, curDraw, : The Turtle escaped from the terrarium. :-(

We can relax the border control

turtle_init(mode = "clip")

Exercise

Now you should be able to draw stars and moons in R

Please do it.

Write the code to draw a star in R

Homework 2

Peer review

In Science, validity of results depends on peer review

Your work is evaluated by someone like you

Do it with the work of your classmates

Answer in the Google Form

Be honest, and be a good team player