【问题标题】:R: Christmas TreeR:圣诞树
【发布时间】:2014-09-17 14:43:51
【问题描述】:

对于一个作业,我们需要在 R 中绘制一棵圣诞树。 我在互联网上搜索并找到了一些有用的建议,但最终我不知道如何继续,希望有人能帮助我。

这是我目前的代码。

#ctree: prints a Christmas tree on screen with size N
ctree <- function(N){
for (i in 1:N){
    width = sample("*",i,replace=T)
    cat(width,sep="-","\n")
    }   
cat(width[1],"\n")
}

这让我得到了树的中间和右侧(N=4),这很好,但还不够。

*-
*-*-
*-*-*-
*-*-*-*-
*

我计划颠倒我所拥有的(基本上是右对齐函数的乘积)来创建左侧,随后删除左侧最右侧的列并将其与树的右侧粘合在一起,创建一个圣诞树。

我真的希望有人可以帮助我实现这一目标!期待您的建议。

提前致谢。

【问题讨论】:

  • 你为什么要使用samplerep 似乎是一个更好的选择。而且,你快到了。在代码中切换 *_ 并调整 _ 的重复频率。
  • 您是否尝试过您提到的计划?如果是这样,是不是出了什么问题?我们不应该做你的功课,但我们可以帮助你解决遇到的错误。
  • 嘿,半棵树总比没有好! :-)
  • 真可惜,因为我记得去年圣诞节在 R-Bloggers 上的这棵圣诞树,我认为它非常好:r-bloggers.com/merry-christmas-2

标签: r tree


【解决方案1】:

对于任何感兴趣的人:这就是我最终在 R 中创建一棵圣诞树的工作。

#ctree: prints a Christmas tree on screen with amount of branch levels N
ctree <- function(N){
filler = "*"
blank = ""

for (i in 1:N){
    row = c(sample(blank,N-i,replace=T),sample(filler,i,replace=T),sample(blank,N-i,replace=T))
    cat(row,"\n")
    }   
cat(c(sample(blank,(N-1),replace=T),sample(filler,1,replace=T),sample(blank,(N-1),replace=T)),"\n")
} #ctree

这就是结果!我自己的快乐小(或大,随你的船漂浮)树。

【讨论】:

  • 您不应该使用sample 重复一个值 n 次,该函数应该用于从一组中随机抽样 n 次。请改用rep(value,ntimes)...
  • 是的,@digEmAll,Roland 之前已经向我指出了这一点,但我只是在制作此功能后才看到该评论。我完全同意,rep 更好,更合适。 (:
【解决方案2】:

此代码来自其他人。我希望我能相信他们,但我失去了来源。它产生的树很漂亮,也许您可​​以根据自己的目的对其进行修改。

part <- list(x0=0,y0=0,x1=0,y1=1,
             branch1=NULL,branch2=NULL,extend=NULL,
             lwd=1,depth=0,col='springgreen')

par(mfrow=c(1,1),mar=c(5, 4, 4, 2) + 0.1)
segplot <- function(tree) {
  if (is.null(tree)) return()
  segments(tree$x0,tree$y0,tree$x1,tree$y1,
           col=tree$col,
           lwd=tree$lwd)
  segplot(tree$branch1)
  segplot(tree$branch2)
  segplot(tree$extend)
}
#segplot(part)

grow <- function(tree) {
  if (is.null(tree) ) return(NULL)

  tree$lwd=tree$lwd*1.2
  if (tree$lwd>2.5) tree$col <- 'brown'
  if (is.null(tree$extend)) {
    tree$extend <- list(
      x0=tree$x1,
      y0=tree$y1,
      x1=rnorm(1,1,.03)*(2*tree$x1-tree$x0),
      y1=(rnorm(1,.98,.02)+.02*(tree$x1==tree$x0))*(2*tree$y1-tree$y0),
      branch1=NULL,
      branch2=NULL,
      extend=NULL,
      lwd=1,
      depth=tree$depth,
      col=tree$col
    )
    length=sqrt((tree$x1-tree$x0)^2 + (tree$y1-tree$y0)^2)
    angle <- asin((tree$x1-tree$x0)/length)
    branch <- list(
      x0=(tree$x1+tree$x0)/2,
      y0=(tree$y1+tree$y0)/2,
      branch1=NULL,
      branch2=NULL,
      extend=NULL,
      lwd=1,
      depth=tree$depth,
      col=tree$col
    )
    shift <- rnorm(2,.5,.1)
    branch$x0 <- shift[1]*tree$x1+(1-shift[1])*tree$x0
    branch$y0 <- shift[1]*tree$y1+(1-shift[1])*tree$y0
    length=length*rnorm(1,.5,.05)
    co <- runif(1,.35,.45)
    branch$x1 <- branch$x0+sin(angle+co)*length

    branch$y1 <- branch$y0+cos(angle+co)*length
    tree$branch1 <- branch
    branch$x0 <- shift[2]*tree$x1+(1-shift[2])*tree$x0
    branch$y0 <- shift[2]*tree$y1+(1-shift[2])*tree$y0
    co <- runif(1,.35,.45)
    branch$x1 <- branch$x0+sin(angle-co)*length
    branch$y1 <- branch$y0+cos(angle-co)*length
    tree$branch2 <- branch    
  } else {
    tree$branch1 <- grow(tree$branch1)
    tree$branch2 <- grow(tree$branch2)
    tree$extend <- grow(tree$extend)
  }
  tree$depth <- tree$depth+1
  if (tree$depth>2)  tree$col <- 'green'
  if (tree$depth>4)  tree$col <- 'darkgreen'
  if (tree$depth>6)  tree$col <- 'brown'

  tree
}
tree <- part
for (i in 1:9) tree <- grow(tree) 
par(mar=c(0,0,0,0))
plot(x=c(-3,3),y=c(0,9),type='n',axes=FALSE,xlab='',ylab='')
segplot(tree)

【讨论】:

  • 我想我曾经在 R-bloggers 上看到过这个。我很惊讶
  • 哇,这棵树真漂亮。感谢分享。 (:
【解决方案3】:

这是一个更简洁的版本:

ctree <- function(N=10){
  for (i in 1:N) cat(rep("",N-i+1),rep("*",i),"\n")
  cat(rep("",N),"*\n")
}

ctree()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 2014-01-13
    • 2021-12-22
    • 1970-01-01
    • 2015-08-23
    相关资源
    最近更新 更多