颜林林的个人网站

怀旧Logo小海龟

2009-07-20 22:18

今天无意中看到一个叫做 PHPTurtle 的东西,突然怀念起当年在学习机上玩 Logo 语言的情形。那时候对键盘还极为生疏,为了照着说明书敲出各种命令,控制屏幕上的小海龟画出各种图形,只能用两根指头在键盘上点点戳戳。

由于最近正迷恋于 R 的缘故,加之它也正擅长图形,于是简单写了小段代码,模仿 Logo 语言的绘图:

turtle_x <- 0
turtle_y <- 0
turtle_direction <- 0
turtle_color <- "white"
turtle_drawing <- TRUE

turtle_init <- function(width = 100, height = 100, mar = rep(0, 4), bg = "black", ...) {
  par(mar = mar)
  par(bg = bg)
  plot(c(-width, width), c(-height, height), type = "n", xlab = "", ylab = "", axes = FALSE, ...)
  turtle_x <<- 0
  turtle_y <<- 0
  turtle_direction <<- 0
}

turtle_goto <- function(x, y) {
  if (turtle_drawing) {
    segments(turtle_x, turtle_y, x, y, col = turtle_color)
  }
  turtle_x <<- x
  turtle_y <<- y
}

turtle_pen_up <- function() {
  turtle_drawing <<- FALSE
}

turtle_pen_down <- function() {
  turtle_drawing <<- TRUE
}

turtle_forward <- function(distance) {
  x <- turtle_x + distance * cos(turtle_direction * pi / 180)
  y <- turtle_y + distance * sin(turtle_direction * pi / 180)
  turtle_goto(x, y)
}

turtle_turn_left <- function(degree) {
  turtle_direction <<- (turtle_direction + degree) %% 360
}

turtle_turn_right <- function(degree) {
  turtle_direction <<- (turtle_direction - degree) %% 360
}

turtle_set_color <- function(col) {
  turtle_color <<- col
}

turtle_demo <- function() {
  turtle_init()
  for (i in 1:4) {
    turtle_forward(50)
    turtle_turn_right(90)
  }
  cat("Press to continue...");
  readLines(n = 1)

  turtle_init()
  turtle_set_color("blue")
  for (i in 1:16) {
    for (j in 1:4) {
      turtle_forward(10)
      turtle_turn_right(90)
    }
    turtle_pen_up()
    turtle_forward(15)
    turtle_pen_down()
    if (i %% 4 == 0) {
      turtle_turn_left(90)
    }
  }
  cat("Press to continue...");
  readLines(n = 1)

  turtle_init()
  turtle_set_color("gold")
  for (i in 1:5) {
    turtle_forward(100)
    turtle_turn_right(144)
  }
  cat("Press to continue...");
  readLines(n = 1)

  turtle_init()
  turtle_set_color("green")
  size <- .01
  for (i in 1:720) {
    turtle_forward(size)
    turtle_turn_right(3)
    size <- size * 1.01
  }
  cat("Press to continue...");
  readLines(n = 1)

  turtle_init()
  turtle_set_color("red")
  for (i in 1:20) {
    turtle_forward(50)
    turtle_turn_right(100)
  }
  cat("Press to continue...");
  readLines(n = 1)

  turtle_init()
  turtle_set_color("yellow")
  for (i in 1:36) {
    for (j in 1:4) {
      turtle_forward(5)
      turtle_turn_right(90)
    }
    turtle_turn_right(10)
    turtle_forward(15)
    for (j in 1:4) {
      turtle_forward(10)
      turtle_turn_right(90)
    }
    turtle_turn_right(10)
  }
  cat("Press to continue...");
  readLines(n = 1)

  turtle_init()
  for (i in 1:25) {
    turtle_set_color( rgb(10 * i, 5 * i, 255 - (10 * i), maxColorValue = 255) )
    for (j in 1:9) {
      turtle_forward(100)
      turtle_turn_right(160)
    }
    turtle_turn_right(24)
  }
}

turtle_demo()

贴出其中一幅:

RTurtle

参考:

  1. http://www.bajobongo.net/zerro/piotr/programy/phpturtle/
  2. http://en.wikipedia.org/wiki/Logo_(programming_language)

相关文章