R语言

乌龟爬爬:TurtleGraphics in R

2019-01-22  本文已影响41人  Y大宽

但是,也挺好玩的
越是简单的东西,越容易被忽略


安装并加载包

install.packages("TurtleGraphics")
library(TurtleGraphics)

1 现在用小乌龟爬出K字母-step by step

turtle_init(width = 100, height = 100)
turtle_lwd(lwd = 15)
turtle_col(col = "blue")
turtle_forward(distance = 40)
turtle_backward(distance = 20)
turtle_right(angle = 40)
turtle_lwd(lwd = 8)
turtle_forward(distance = 25)
turtle_right(angle = 180)
turtle_forward(distance = 25)
turtle_left(angle = 80)
turtle_forward(distance = 25)
K

turtle_init()
By default its size is 100 by 100 units. You can easily change it by passing appropriate valuesto thewidthandheightarguments (e.g.turtle_init(width=200, height=200)).To define what happens if the Turtle moves outside the plot region, you can set themodeoption. The default value,"clip", means that the Turtle can freely go outside the board (but itwill not be seen). The"error"option does not let the Turtle out of the Terrarium – if the Turtletries to escape, an error is thrown. The third value,"cycle", makes the Turtle come out on the other side of the board if it tries to cross the border.
简单来说:这个函数设置乌龟的活动范围。默认乌龟出现在区域中间,头朝北(上)
mode可以设置为clip,erro,cycle分别对应乌龟出界时候的反应,具体分别为
clip:可以出界,看不见它
erro:不允许乌龟出界,如果出去会有错误提示
cycle:如果出界就从另一面出现(穿越)

2 打包上面的动作,直接出图

turtle_init()
turtle_do({
  turtle_init(width = 100, height = 100)
  turtle_lwd(lwd = 15)
  turtle_col(col = "blue")
  turtle_forward(distance = 40)
  turtle_backward(distance = 20)
  turtle_right(angle = 40)
  turtle_lwd(lwd = 8)
  turtle_forward(distance = 25)
  turtle_right(angle = 180)
  turtle_forward(distance = 25)
  turtle_left(angle = 80)
  turtle_forward(distance = 25)
})

和1一样,只是,省却了中间步骤

3 for loop:用循环让乌龟爬出圆形

turtle_init()
turtle_setpos(x=30, y=50)
turtle_lwd(lwd = 15)
turtle_col(col = "red")
turtle_do({
 for (i in 1:180) {
  turtle_forward(dist = 1)
  turtle_right(angle = 2)
 } 
})
circle

4设置条件:if语句

turtle_init()
turtle_do({
  for (i in 1:5) {
    x <- runif(5)
    if (x>0.5){
    turtle_right(angle = 45)
    turtle_lwd(lwd = 2)
    turtle_col(col = "red")
  } else {
    turtle_left(angle = 45)
    turtle_lwd(lwd = 5)
    turtle_col(col = "blue")
  }
  turtle_forward(distance = 10)
  }
})
if

5 function(函数)

turtle_init()
turtle_square <- function(r){
  for (i in 1:4) {
    turtle_forward(r)
    turtle_right(90)
  }
}
turtle_square(20)
image.png
turtle_init()
turtle_square(10)
turtle_right(180)
turtle_forward(20)
turtle_left(90)
turtle_square(10)
turtle_left(270)
turtle_square(10)
turtle_left(180)
turtle_forward(20)
turtle_left(90)
turtle_square(10)
image.png

6 随机线条

set.seed(120)
n = 10
turtle_init(100, 100, mode = "cycle")
turtle_do({
  for (i in 1:n) {
    turtle_left(runif(1,0,360))
    turtle_forward(runif(1,0,1000))
  }
})
randome line

7 螺旋

drawSpiral <- function(lineLen){
  if (lineLen > 0){
    turtle_forward(lineLen)
    turtle_right(50)
    drawSpiral(lineLen-5)
}
    invisible(NULL)
}
turtle_init(500,500,mode = "clip")
turtle_do({
  turtle_setpos(0,0)
  turtle_col("blue")
  drawSpiral(500)
  turtle_setpos(250,0)
  turtle_left(45)
  turtle_col("red")
  drawSpiral(354)
  turtle_setangle(0)
})
spiral

还有更多,看原文去吧

tree sierpinski Triangle

8 其它

turtle_init()
turtle_col("blue")
n=150
for (i in 1:n) {
  turtle_forward(distance = 1+0.5*i)
  turtle_right(89.5)
}
turtle_hide()
image.png
turtle_init()
turtle_col("red")
turtle_right(angle=234)
for (i in 1:100) {
  turtle_forward(dist=0.9*i)
  turtle_right(angle=144.3)}
image.png
turtle_init()
turtle_col("hotpink")
turtle_setpos(50,35)
turtle_right(angle=60)
d=25
turtle_setpos(50-d/2,50-d/2*tan(pi/6))
for (i in 1:100) {
  turtle_forward(dist=d)
  d=d+0.5
  turtle_right(angle=120+1)}
image.png

----------------------------

快214了,

dat<- data.frame(t=seq(0, 2*pi, by=0.1) )
xhrt <- function(t) 16*sin(t)^3
yhrt <- function(t) 13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t)
dat$y=yhrt(dat$t)
dat$x=xhrt(dat$t)
with(dat, plot(x,y, type="l"))
with(dat, polygon(x,y, col="firebrick3"))  
points(c(10,-10, -15, 15), c(-10, -10, 10, 10), pch=169,
       font=5, col = "pink",cex = 8)
heart
上一篇下一篇

猜你喜欢

热点阅读