在R中绘制多个不同尺寸和坐标的3D框 [英] Draw multiple 3D boxes of different dimensions and coordinates in R

查看:148
本文介绍了在R中绘制多个不同尺寸和坐标的3D框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我偶然发现了R中的rgl-Package,该软件包可用于创建交互式3d图。现在,我想在一个3d图中可视化一组盒子。盒子B的笛卡尔坐标B_coord = [x,y,z],对应于左下角,尺寸为B_dim = [x1,y1,z1]。

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)

使用此代码将框移动到x,y,z并缩放为x1,y1,z1。我的问题是如何用相同的输入编写类似的函数,该函数通过将框按其左下角的左下角定位,并绘制尺寸为x1,y1,z1的框。我并不依赖于rgl软件包和R,但我喜欢其交互式3d视图。

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.

谢谢您的想法!

推荐答案

我认为您的代码已经做到了。为了更清楚地说明这些 rgl 函数的功能,我展开了您的函数并对其进行了注释,并将其放在一个更具说明性的示例中。

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

这篇关于在R中绘制多个不同尺寸和坐标的3D框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆