[R] rgl how to plot a cylinder like arrow3d?

René Mayer mayer at psychologie.tu-dresden.de
Tue Aug 9 11:22:44 CEST 2011


Dear List,
I'm trying to draw vector in XYZ with rgl under use of a cylinder3d.
Therefore I scale and rotate a basis-cylinder).
However, somehow the rotation is wrong as
verified by overplotting arrow3d().
Where is my mistake?

library(heplots)
library(rgl)

# ... 2 vectors
data=data.frame(row.names=c('X','Y','Z'), x1=c(2,1,5),y=c(4,3,2))

vector3D=function(start=c(0,0,0), end, mycol='green'){
# ... cylinder as basis-vector
c=cylinder3d(rbind(      c(start), # start
			 c(0,0,1)), # end
		         radius = 0.2,
                          e1=cbind(0, 0, 1),
                          e2=cbind(1, 0, 0),
		         sides=10
                    )

# ... rotate cylinder horizontally and scale it
len=sqrt(sum(abs(end)*abs(end)))
c=scale3d(c,0.1,0.1,len)
c=rotate3d(c,-(pi/2),0,1,0)

# ... rotation angles
theta = atan2(end[2],end[1]) # angle in yx-plane for Z-achses rotation
phi = atan2(end[3],sqrt(end[1]^2+end[1]^2)) # angle in zx for Y-achses  
rotation

# ... rotation
c=rotate3d(c,phi,0,1,0) # Y-achses rotation
c=rotate3d(c, -theta,0,0,1) # Z-achses rotation
shade3d(addNormals(c), col=mycol)
}

open3d()
vector3D(start=c(0,0,0),end=data[,1], 'red');  
vector3D(start=c(0,0,0),end=data[,2],'green')
arrow3d(c(0,0,0),data[,1],color='red');arrow3d(c(0,0,0),data[,2],  
color='green')
axes3d(c('x','y','z'));title3d('main','sub','X','Y','Z');box3d()

thanks,
René



More information about the R-help mailing list