在3D中绘制两点之间的线

时间:2022-02-15 14:58:28

I am writing an regression algorithm which tries to "capture" points inside boxes. The algorithm tries to keep the boxes as small as possible, so usually the edges/corners of the boxes go through points, which determines the size of the box.

我正在编写一种回归算法,试图“捕获”框内的点。该算法试图使盒子保持尽可能小,因此通常盒子的边缘/角落通过点,这决定了盒子的大小。

Problem: I need graphical output of the boxes in R. In 2D it is easy to draw boxes with segments(), which draws a line between two points. So, with 4 segments I can draw a box:

问题:我需要R中框的图形输出。在2D中,可以很容易地绘制带有segment()的框,它在两点之间画一条线。所以,有4个段我可以画一个盒子:

plot(x,y,type="p")
segments(x1,y1,x2,y2)

I then tried both the scatterplot3d and plot3d package for 3D plotting. In 3D the segments() command is not working, as there is no additional z-component. I was surprised that apparently (to me) there is no adequate replacement in 3D for segments()

然后我尝试使用scatterplot3d和plot3d包进行3D绘图。在3D中,segments()命令不起作用,因为没有其他z组件。我很惊讶,显然(对我来说)3D中没有足够的替代品()

Is there an easy way to draw boxes / lines between two points when plotting in three dimensions ?

在三维绘图时,有一种简单的方法可以在两点之间绘制方框/线条吗?

1 个解决方案

#1


12  

The scatterplot3d function returns information that will allow you to project (x,y,z) points into the relevant plane, as follows:

scatterplot3d函数返回允许您将(x,y,z)点投影到相关平面的信息,如下所示:

library(scatterplot3d)
x <- c(1,4,3,6,2,5)
y <- c(2,2,4,3,5,9)
z <- c(1,3,5,9,2,2)
s <- scatterplot3d(x,y,z)

## now draw a line between points 2 and 3
p2 <- s$xyz.convert(x[2],y[2],z[2])
p3 <- s$xyz.convert(x[3],y[3],z[3])
segments(p2$x,p2$y,p3$x,p3$y,lwd=2,col=2)

The rgl package is another way to go, and perhaps even easier (note that segments3d takes points in pairs from a vector)

rgl包是另一种方法,也许更容易(请注意,segments3d从向量中成对获取点)

plot3d(x,y,z)
segments3d(x[2:3],y[2:3],z[2:3],col=2,lwd=2)

#1


12  

The scatterplot3d function returns information that will allow you to project (x,y,z) points into the relevant plane, as follows:

scatterplot3d函数返回允许您将(x,y,z)点投影到相关平面的信息,如下所示:

library(scatterplot3d)
x <- c(1,4,3,6,2,5)
y <- c(2,2,4,3,5,9)
z <- c(1,3,5,9,2,2)
s <- scatterplot3d(x,y,z)

## now draw a line between points 2 and 3
p2 <- s$xyz.convert(x[2],y[2],z[2])
p3 <- s$xyz.convert(x[3],y[3],z[3])
segments(p2$x,p2$y,p3$x,p3$y,lwd=2,col=2)

The rgl package is another way to go, and perhaps even easier (note that segments3d takes points in pairs from a vector)

rgl包是另一种方法,也许更容易(请注意,segments3d从向量中成对获取点)

plot3d(x,y,z)
segments3d(x[2:3],y[2:3],z[2:3],col=2,lwd=2)