在3D中的两点之间绘制线 [英] Plotting lines between two points in 3D

查看:113
本文介绍了在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)

然后我尝试使用scatterplot3dplot3d软件包进行3D绘图.在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)

这篇关于在3D中的两点之间绘制线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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