devel/Natalia/Rbloggers.md

The famous Turtle Graphics is now available on R !

plot of chunk unnamed-chunk-1

Do you find this plot fancy ? If yes you can find the code at the end of this article BUT if you spend a little time to read it thoroughly, you can learn how to create better ones.

We would like to encourage you and your children (yes, your children) to use our new R package - TurtleGraphics !

TurtleGraphics package offers to R-users functionality of the "turtle graphics" from Logo educational programming language. The main idea standing behind it is to inspire the children to learn programming and show that working with computer can be entertaining and creative.

It is very elementary, clear and requires basic algorithm thinking skills, that even children are able to form them. You can learn it in just five short steps.

library(TurtleGraphics)
turtle_init()

plot of chunk unnamed-chunk-2 * turtle_forward() and turtle_backward() - In its argument you have to give the distance you desire the Turtle to move. For example, to move the Turtle forward for a distance of 10 units use the turtle_forward() function. To move the Turtle backwards you can use the turtle_backward() function.

turtle_forward(dist=15)

plot of chunk unnamed-chunk-3

turtle_right(angle=30)

plot of chunk unnamed-chunk-4

turtle_up()
turtle_forward(dist=10)
turtle_down()
turtle_forward(dist=10)

plot of chunk unnamed-chunk-5

turtle_hide()

plot of chunk unnamed-chunk-6

These were just the basics of the package. Below we show you the true potential of it:

  turtle_star <- function(intensity=1){
  y <- sample(1:657, 360*intensity, replace=TRUE)
  for (i in 1:(360*intensity)){
  turtle_right(90)
  turtle_col(colors()[y[i]])
  x <- sample(1:100,1)
  turtle_forward(x)
  turtle_up()
  turtle_backward(x)
  turtle_down()
  turtle_left(90)
  turtle_forward(1/intensity)
  turtle_left(1/intensity)
  }}
  turtle_init(500,500)
  turtle_left(90)
  turtle_do({
  turtle_star(7)
  })

plot of chunk unnamed-chunk-7

One may wonder what turtle_do() function is doing here. It is an avdanced way to use the package. The turtle\_do() function is designed to call more complicated plot expressions, because it automatically hides the Turtle before starting the operations that occurs in a faster proceed of plotting.

  drawTriangle<- function(points){
  turtle_setpos(points[1,1],points[1,2])
  turtle_goto(points[2,1],points[2,2])
  turtle_goto(points[3,1],points[3,2])
  turtle_goto(points[1,1],points[1,2])
  }
  getMid<- function(p1,p2) c((p1[1]+p2[1])/2, c(p1[2]+p2[2])/2)
  sierpinski <- function(points, degree){
  drawTriangle(points)
  if (degree  > 0){
  p1 <- matrix(c(points[1,], getMid(points[1,], points[2,]),
  getMid(points[1,], points[3,])), nrow=3, byrow=TRUE)

  sierpinski(p1, degree-1)
  p2 <- matrix(c(points[2,], getMid(points[1,], points[2,]),
  getMid(points[2,], points[3,])), nrow=3, byrow=TRUE)

  sierpinski(p2, degree-1)
  p3 <- matrix(c(points[3,], getMid(points[3,], points[2,]),
  getMid(points[1,], points[3,])), nrow=3, byrow=TRUE)
  sierpinski(p3, degree-1)
  }
  invisible(NULL)
  }
  turtle_init(520, 500, "clip")
  p <- matrix(c(10, 10, 510, 10, 250, 448), nrow=3, byrow=TRUE)
  turtle_col("red")
  turtle_do(sierpinski(p, 6))
  turtle_setpos(250, 448)

plot of chunk unnamed-chunk-8

We kindly invite you to use TurtleGraphics ! Enjoy ! A full tutorial of this package is available here .

Marcin Kosinski, Natalia Potocka



Rexamine/TurtleGraphics documentation built on Aug. 9, 2022, 9:25 a.m.