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

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

问题描述

最近我偶然发现了 R 中的 rgl-Package,它可用于创建交互式 3d 绘图.现在我想在一个 3d 图中可视化一组框.Box 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天全站免登陆