Reputation: 47
Recently I stumbled over the rgl-Package in R, which can be used to create interactive 3d plots. Now I want to visualize a set of boxes in one 3d plot. A Box B has cartesian coordinates B_coord=[x,y,z], which correspond to the lower left back corner and dimensions B_dim=[x1,y1,z1].
Apparently it is easy to draw, scale and position some cubes with the following exemplary code:
open3d()
printBox <- function(x,y,z,x1,y1,z1) {
mycube <- scale3d(cube3d(),x1,y1,z1)
wire3d(translate3d(mycube,x,y,z))
}
printBox(0,0,0,1,1,1)
With this code the boxes are moved to x,y,z and scaled to x1,y1,z1. My question is how to write a similar function with the same input which positions the boxes by the coordinates of their lower left back corner and draws a box with the dimensions x1, y1, z1. I am not tied to the rgl package and R, but I like its interactive 3d view.
Thank you for your ideas!
Upvotes: 3
Views: 1116
Reputation: 22827
I think your code already does that. So as to make it more clear, and explain what those rgl
functions do, I unrolled your function and commented it and put it in a more illustrative example.
library(rgl)
open3d()
# create and plot a box at (x,y,z) of size (x1,y1,z1)
printBox <- function(x, y, z, x1, y1, z1) {
mycube <- cube3d() # create a cube as mesh object
mycube <- scale3d(mycube, x1, y1, z1) # now scale that object by x1,y1,z1
mycube <- translate3d(mycube, x, y, z) # now move it to x,y,z
wire3d(mycube) # now plot it to rgl as a wireframe
}
# Display 5 boxes along a diagonal line
n <- 5
for (i in 1:n) {
x <- i/n
y <- i/n
z <- i/n
sz <- 1/(2*n)
printBox(x, y, z, sz,sz,sz )
}
axes3d() # add some axes
Upvotes: 3