繁体   English   中英

如何在R(rgl.spheres)中绘制一个完美的圆形球体

[英]How to plot a perfectly round sphere in R (rgl.spheres)

R具有很好的绘制球体的功能(见下文)。 然而,球体不是圆形的,但在放大时看起来很锋利。我如何绘制完美的圆形球体? 谢谢! 干杯,克里斯

library(rgl)

rgl.spheres(1,1,1,radius=1,color="blue")

下面的函数是模仿rgl.spheres ng是球体上的网格数。 如果你增加ng ,你有更多圆球。

rgl.sphere <- function (x, y=NULL, z=NULL, ng=50, radius = 1, color="white", add=F, ...) {
  lat <- matrix(seq(90, -90, len = ng)*pi/180, ng, ng, byrow = TRUE)
  long <- matrix(seq(-180, 180, len = ng)*pi/180, ng, ng)

  vertex  <- rgl:::rgl.vertex(x, y, z)
  nvertex <- rgl:::rgl.nvertex(vertex)
  radius  <- rbind(vertex, rgl:::rgl.attr(radius, nvertex))[4,]
  color  <- rbind(vertex, rgl:::rgl.attr(color, nvertex))[4,]

  for(i in 1:nvertex) {
    add2 <- if(!add) i>1 else T
    x <- vertex[1,i] + radius[i]*cos(lat)*cos(long)
    y <- vertex[2,i] + radius[i]*cos(lat)*sin(long)
    z <- vertex[3,i] + radius[i]*sin(lat)
    persp3d(x, y, z, specular="white", add=add2, color=color[i], ...)
  }
}

测试功能:

rgl.sphere(rnorm(10), rnorm(10), rnorm(10), radius = runif(10), color = rainbow(10))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM