不知道有没有人玩过控制小海龟画图的 Logo 语言(http://en.wikipedia.org/wiki/Logo_%28programming_language%29)?今天无意间看到有人用 PHP 来实现了一个 PHPTurtle(http://freshmeat.net/projects/phpturtle),于是我就照猫画虎,写一个 RTurtle 来玩玩:
<br />
turtle_x <- 0<br />
turtle_y <- 0<br />
turtle_direction <- 0<br />
turtle_color <- "white"<br />
turtle_drawing <- TRUE<br />
<br />
turtle_init <- function(width = 100, height = 100, mar = rep(0, 4), bg = "black", ...) {<br />
par(mar = mar)<br />
par(bg = bg)<br />
plot(c(-width, width), c(-height, height), type = "n", xlab = "", ylab = "", axes = FALSE, ...)<br />
turtle_x <<- 0<br />
turtle_y <<- 0<br />
turtle_direction <<- 0<br />
}<br />
<br />
turtle_goto <- function(x, y) {<br />
if (turtle_drawing) {<br />
segments(turtle_x, turtle_y, x, y, col = turtle_color)<br />
}<br />
turtle_x <<- x<br />
turtle_y <<- y<br />
}<br />
<br />
turtle_pen_up <- function() {<br />
turtle_drawing <<- FALSE<br />
}<br />
<br />
turtle_pen_down <- function() {<br />
turtle_drawing <<- TRUE<br />
}<br />
<br />
turtle_forward <- function(distance) {<br />
x <- turtle_x + distance * cos(turtle_direction * pi / 180)<br />
y <- turtle_y + distance * sin(turtle_direction * pi / 180)<br />
turtle_goto(x, y)<br />
}<br />
<br />
turtle_turn_left <- function(degree) {<br />
turtle_direction <<- (turtle_direction + degree) %% 360<br />
}<br />
<br />
turtle_turn_right <- function(degree) {<br />
turtle_direction <<- (turtle_direction - degree) %% 360<br />
}<br />
<br />
turtle_set_color <- function(col) {<br />
turtle_color <<- col<br />
}<br />
<br />
turtle_demo <- function() {<br />
turtle_init()<br />
for (i in 1:4) {<br />
turtle_forward(50)<br />
turtle_turn_right(90)<br />
}<br />
cat("Press <Enter> to continue...");<br />
readLines(n = 1)<br />
<br />
turtle_init()<br />
turtle_set_color("blue")<br />
for (i in 1:16) {<br />
for (j in 1:4) {<br />
turtle_forward(10)<br />
turtle_turn_right(90)<br />
}<br />
turtle_pen_up()<br />
turtle_forward(15)<br />
turtle_pen_down()<br />
if (i %% 4 == 0) {<br />
turtle_turn_left(90)<br />
}<br />
}<br />
cat("Press <Enter> to continue...");<br />
readLines(n = 1)<br />
<br />
turtle_init()<br />
turtle_set_color("gold")<br />
for (i in 1:5) {<br />
turtle_forward(100)<br />
turtle_turn_right(144)<br />
}<br />
cat("Press <Enter> to continue...");<br />
readLines(n = 1)<br />
<br />
turtle_init()<br />
turtle_set_color("green")<br />
size <- .01<br />
for (i in 1:720) {<br />
turtle_forward(size)<br />
turtle_turn_right(3)<br />
size <- size * 1.01<br />
}<br />
cat("Press <Enter> to continue...");<br />
readLines(n = 1)<br />
<br />
turtle_init()<br />
turtle_set_color("red")<br />
for (i in 1:20) {<br />
turtle_forward(50)<br />
turtle_turn_right(100)<br />
}<br />
cat("Press <Enter> to continue...");<br />
readLines(n = 1)<br />
<br />
turtle_init()<br />
turtle_set_color("yellow")<br />
for (i in 1:36) {<br />
for (j in 1:4) {<br />
turtle_forward(5)<br />
turtle_turn_right(90)<br />
}<br />
turtle_turn_right(10)<br />
turtle_forward(15)<br />
for (j in 1:4) {<br />
turtle_forward(10)<br />
turtle_turn_right(90)<br />
}<br />
turtle_turn_right(10)<br />
}<br />
cat("Press <Enter> to continue...");<br />
readLines(n = 1)<br />
<br />
turtle_init()<br />
for (i in 1:25) {<br />
turtle_set_color( rgb(10 * i, 5 * i, 255 - (10 * i), maxColorValue = 255) )<br />
for (j in 1:9) {<br />
turtle_forward(100)<br />
turtle_turn_right(160)<br />
}<br />
turtle_turn_right(24)<br />
}<br />
}<br />
<br />
turtle_demo()<br />