前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >乌龟爬爬:TurtleGraphics in R

乌龟爬爬:TurtleGraphics in R

作者头像
Y大宽
发布2019-01-28 17:24:34
4530
发布2019-01-28 17:24:34
举报
文章被收录于专栏:Y大宽

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


安装并加载包

代码语言:javascript
复制
install.packages("TurtleGraphics")
library(TurtleGraphics)

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

代码语言:javascript
复制
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 打包上面的动作,直接出图

代码语言:javascript
复制
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:用循环让乌龟爬出圆形

代码语言:javascript
复制
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语句

代码语言:javascript
复制
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(函数)

代码语言:javascript
复制
turtle_init()
turtle_square <- function(r){
  for (i in 1:4) {
    turtle_forward(r)
    turtle_right(90)
  }
}
turtle_square(20)

image.png

代码语言:javascript
复制
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 随机线条

代码语言:javascript
复制
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 螺旋

代码语言:javascript
复制
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 其它

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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了,

代码语言:javascript
复制
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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.01.22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 现在用小乌龟爬出K字母-step by step
  • 2 打包上面的动作,直接出图
  • 3 for loop:用循环让乌龟爬出圆形
  • 4设置条件:if语句
  • 5 function(函数)
  • 6 随机线条
  • 7 螺旋
  • 还有更多,看原文去吧
  • 8 其它
    • ----------------------------
    • 快214了,
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档