[R] Want to draw 3D cylinder objects

Ben Bolker bolker at ufl.edu
Thu Mar 27 03:05:03 CET 2008


Hans-Joachim Klemmt <h-j.klemmt <at> lrz.tu-muenchen.de> writes:

> 
> Hello,
> 
> I want to draw 3D cylinder objects in R.
> 
> Given is the length and the diameter of the cylinder.
> 
> Has anybody an example?
> 
> Thank you very much!
> 
> Best regards
> 


  Here's a starting point, if you want to use the
rgl package:

cylinder3d <- function(len=1,rad=1,n=30,ctr=c(0,0,0),
         trans = par3d("userMatrix"),...) {
  if (missing(trans) && !rgl.cur()) trans <- diag(4)
  degvec <- seq(0,2*pi,length=n)
  circ <- cbind(rad*cos(degvec),rad*sin(degvec))
  x <- rad*cos(degvec)
  y <- rad*sin(degvec)
  p <- c(1,1,rep(2:n,each=4),1,1)
  z <- rep(c(-1,1,1,-1)*len/2,n)
  quads3d(x[p],y[p],z,...)
}

  it doesn't draw the ends of the cylinder.

  good luck,
    Ben Bolker



More information about the R-help mailing list