但是,也挺好玩的。
越是簡單的東西,越容易被忽略
安裝并加載包
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)
複制
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICMyYTMvw1dvwlMvwlM3VWaWV2Zh1Wa-cmbw5SN3VTMqNzYrF2ZvwFOzAzM5QjMtUGall3LcVmdhNXLwRHdo9CXt92YucWbpRWdvx2Yx5yazF2Lc9CX6MHc0RHaiojIsJye.png)
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