在 3D 中绘制两点之间的线
我正在编写一个回归算法,试图捕获"框内的点.算法会尽量保持盒子的小,所以通常盒子的边/角会经过点,这决定了盒子的大小.
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.
问题:我需要 R 中框的图形输出.在 2D 中,使用 segments()
绘制框很容易,它在两点之间画一条线.所以,我可以用 4 段画一个框:
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:
plot(x,y,type="p")
segments(x1,y1,x2,y2)
然后我尝试了用于 3D 绘图的 scatterplot3d
和 plot3d
包.在 3D 中 segments()
命令不起作用,因为没有额外的 z 分量.我很惊讶显然(对我来说)在 3D 中没有足够的 segments()
替换
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()
在三维绘图时,是否有一种简单的方法可以在两点之间绘制框/线?
Is there an easy way to draw boxes / lines between two points when plotting in three dimensions ?
scatterplot3d
函数返回的信息将允许您将 (x,y,z) 点投影到相关平面,如下所示:
The scatterplot3d
function returns information that will allow you to project (x,y,z) points into the relevant plane, as follows:
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)
rgl
包是另一种方法,而且可能更简单(请注意,segments3d
从向量中成对获取点)
The rgl
package is another way to go, and perhaps even easier (note that segments3d
takes points in pairs from a vector)
plot3d(x,y,z)
segments3d(x[2:3],y[2:3],z[2:3],col=2,lwd=2)