Skip to content

Want to draw 3D cylinder objects

5 messages · Hans-Joachim Klemmt, jim holtman, Ben Bolker +1 more

#
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
#
Can you give a little more detail?

Do you want the nice 3d shading effect? Or is an ellipse, 2 lines and another ellipse good enough?  Does the viewing angle matter (flatness of ellipses).  Do the cylinders need to be placed at certain locations? Or are you trying to do a barplot with 3d cylinders instead of bars? (please, please, please say no to that last one)
#
Check out the 'rgl' package for creating 3D objects.

On Wed, Mar 26, 2008 at 3:02 PM, Hans-Joachim Klemmt
<h-j.klemmt at lrz.tu-muenchen.de> wrote:

  
    
#
Hans-Joachim Klemmt <h-j.klemmt <at> lrz.tu-muenchen.de> writes:
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
#
Here is one approach to drawing simple cylinders at specified locations, the height, width, and angle should all be numbers between 0 and 1 and you may want to rewrite the ms.cylinder function below so that the arguments are more meaningful relative to the input data that you want to use:
+ theta <- seq(2*pi, 0, length=200)
+ x1 <- cos(theta) * width
+ y1 <- sin(theta) * width * angle
+ 
+ x <- c(x1, x1[1:100], x1[100])
+ y <- c(y1 + height/2, y1[1:100]-height/2, y1[100]+height/2)
+ 
+ return(cbind(x,y))
+ }
Hope this helps,