【r<-fun|【r<-fun|? 】用字符画出Z的曲线

5555555555 2n 2n 2n 2n 2n 2n 2n 2n 2n 2n 0a0a0a0a0a0a0a0a0a0a ZDZDZDZDZDZDZDZDZDZD h uh uh uh uh uh uh uh uh uh u oooooooooo

【【r<-fun|【r<-fun|? 】用字符画出Z的曲线】实现的代码函数:
zcurve = function(string = NULL, n = NULL){ strs = stringr::str_split(string, pattern = "") nInterval = n - 2 nTotal = n + nIntervalres = list()j = 1 for(s in strs){ nc = (n-1) * (length(s) %/% nTotal) + (length(s) %% nTotal %/% n) + 1 df = matrix(datahttps://www.it610.com/article/= "", nrow = n, ncol = nc) for (i in 1:length(s)){ rx = ifelse((i %% nTotal <= n) & (i %% nTotal > 0), i %% nTotal, ifelse(i %% nTotal == 0, 2, n - (i %% nTotal) %% n )) ry = ifelse((i %% nTotal <= n) & (i %% nTotal > 0), (n-1) * (i %/% nTotal) + 1, ifelse((i %% nTotal <= n) & (i %% nTotal == 0), (n-1) * (i %/% nTotal), (n-1) * (i %/% nTotal) + i %% nTotal %% n + 1) )df[rx, ry] = s[i] } res[[j]] = df j = j + 1 }if(length(res) == 1){ prmatrix(res[[1]], rowlab=rep("",nrow(res[[1]])), collab=rep("",ncol(res[[1]])), quote = FALSE) }else{ for(i in 1:length(res)){ cat("#---- Z Curve #", i, "\n") cat("\n") prmatrix(res[[i]], rowlab=rep("",nrow(res[[i]])), collab=rep("",ncol(res[[i]])), quote = FALSE) cat("\n") } }}

支持多个字符串输入,通过设定输入的字符串和行数,函数就可以自动画出曲线了~
测试如下:
> zcurve(string = paste0(letters[1:20], collapse = ""), n = 5) aiq bh jp r cgkos d fl nt em > zcurve(string = c(paste0(letters[1:20], collapse = ""), +paste0(letters[20:1], collapse = "")), n = 5) #---- Z Curve # 1 aiq bh jp r cgkos d fl nt em#---- Z Curve # 2 tld sm ke c rnjfb q oi ga ph

一开始的文字嘛,需要下面一行代码实现。
zcurve(string = paste0(rep(c("5","2","0","Z","h","o","u","D","a","n"),10),collapse = ""), n = 6)

【r<-fun|【r<-fun|? 】用字符画出Z的曲线
文章图片

    推荐阅读