也許這樣的事情有幫助嗎? :
#basic plot
plot(NULL, ann = F, xlim = c(-10,20), ylim = c(-10,20))
abline(h = -10:20, col = grey(0.75), lty = 2)
abline(v = -10:20, col = grey(0.75), lty = 2)
#starting coordinates
A_coords = c(0,0)
B_coords = c(10,0)
text(A_coords[1], A_coords[2], "A", col = "red")
text(B_coords[1], B_coords[2], "B", col = "blue")
for(i in 1:15000)
{
Sys.sleep(1)
text(A_coords[1], A_coords[2], "A", col = "white")
text(B_coords[1], B_coords[2], "B", col = "white")
#used jonas's idea
A <- A_coords + unlist(sample(list(c(0, 1), c(1, 0), c(-1, 0), c(0, -1)), 1))
B <- B_coords + unlist(sample(list(c(0, 1), c(1, 0), c(-1, 0), c(0, -1)), 1))
lines(c(A_coords[1], A[1]), c(A_coords[2], A[2]), col = "red")
lines(c(B_coords[1], B[1]), c(B_coords[2], B[2]), col = "blue")
A_coords <- A
B_coords <- B
text(A_coords[1], A_coords[2], "A", col = "red")
text(B_coords[1], B_coords[2], "B", col = "blue")
if(all(abs(A_coords - B_coords) <= 1)) break
}
list(steps = i, A_coordinates = A_coords, B_coordinates = B_coords)
看起來像功課。你試過什麼了? –
@BenBolker是的,它是家庭作業,我們有一些R的手冊,但它只包含幾個繪製圖形的命令,我正在跳躍,有一些簡單的命令,可以讓我繪製距離爲1的小線條。 –
我刪除了Rcpp標籤,因爲與我可以說最好的沒有任何關係。 –