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

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

r
  1turtle_x <- 0
  2turtle_y <- 0
  3turtle_direction <- 0
  4turtle_color <- "white"
  5turtle_drawing <- TRUE
  6
  7turtle_init <- function(width = 100, height = 100, mar = rep(0, 4), bg = "black", ...) {
  8  par(mar = mar)
  9  par(bg = bg)
 10  plot(c(-width, width), c(-height, height), type = "n", xlab = "", ylab = "", axes = FALSE, ...)
 11  turtle_x <<- 0
 12  turtle_y <<- 0
 13  turtle_direction <<- 0
 14}
 15
 16turtle_goto <- function(x, y) {
 17  if (turtle_drawing) {
 18    segments(turtle_x, turtle_y, x, y, col = turtle_color)
 19  }
 20  turtle_x <<- x
 21  turtle_y <<- y
 22}
 23
 24turtle_pen_up <- function() {
 25  turtle_drawing <<- FALSE
 26}
 27
 28turtle_pen_down <- function() {
 29  turtle_drawing <<- TRUE
 30}
 31
 32turtle_forward <- function(distance) {
 33  x <- turtle_x + distance * cos(turtle_direction * pi / 180)
 34  y <- turtle_y + distance * sin(turtle_direction * pi / 180)
 35  turtle_goto(x, y)
 36}
 37
 38turtle_turn_left <- function(degree) {
 39  turtle_direction <<- (turtle_direction + degree) %% 360
 40}
 41
 42turtle_turn_right <- function(degree) {
 43  turtle_direction <<- (turtle_direction - degree) %% 360
 44}
 45
 46turtle_set_color <- function(col) {
 47  turtle_color <<- col
 48}
 49
 50turtle_demo <- function() {
 51  turtle_init()
 52  for (i in 1:4) {
 53    turtle_forward(50)
 54    turtle_turn_right(90)
 55  }
 56  cat("Press to continue...");
 57  readLines(n = 1)
 58
 59  turtle_init()
 60  turtle_set_color("blue")
 61  for (i in 1:16) {
 62    for (j in 1:4) {
 63      turtle_forward(10)
 64      turtle_turn_right(90)
 65    }
 66    turtle_pen_up()
 67    turtle_forward(15)
 68    turtle_pen_down()
 69    if (i %% 4 == 0) {
 70      turtle_turn_left(90)
 71    }
 72  }
 73  cat("Press to continue...");
 74  readLines(n = 1)
 75
 76  turtle_init()
 77  turtle_set_color("gold")
 78  for (i in 1:5) {
 79    turtle_forward(100)
 80    turtle_turn_right(144)
 81  }
 82  cat("Press to continue...");
 83  readLines(n = 1)
 84
 85  turtle_init()
 86  turtle_set_color("green")
 87  size <- .01
 88  for (i in 1:720) {
 89    turtle_forward(size)
 90    turtle_turn_right(3)
 91    size <- size * 1.01
 92  }
 93  cat("Press to continue...");
 94  readLines(n = 1)
 95
 96  turtle_init()
 97  turtle_set_color("red")
 98  for (i in 1:20) {
 99    turtle_forward(50)
100    turtle_turn_right(100)
101  }
102  cat("Press to continue...");
103  readLines(n = 1)
104
105  turtle_init()
106  turtle_set_color("yellow")
107  for (i in 1:36) {
108    for (j in 1:4) {
109      turtle_forward(5)
110      turtle_turn_right(90)
111    }
112    turtle_turn_right(10)
113    turtle_forward(15)
114    for (j in 1:4) {
115      turtle_forward(10)
116      turtle_turn_right(90)
117    }
118    turtle_turn_right(10)
119  }
120  cat("Press to continue...");
121  readLines(n = 1)
122
123  turtle_init()
124  for (i in 1:25) {
125    turtle_set_color( rgb(10 * i, 5 * i, 255 - (10 * i), maxColorValue = 255) )
126    for (j in 1:9) {
127      turtle_forward(100)
128      turtle_turn_right(160)
129    }
130    turtle_turn_right(24)
131  }
132}
133
134turtle_demo()

贴出其中一幅:

RTurtle

参考:

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