简体   繁体   English

Matlab:我在3D绘图中有两个点,我想用一条线连接它们

[英]Matlab: I have two points in a 3D plot and i want to connect them with a line

I have a 3D plot and two points coordinates A(0,0,0) and B(13,-11,19). 我有一个3D图和两个点坐标A(0,0,0)和B(13,-11,19)。 I just want to plot a visible line connecting this two points ... I tried plot3(0,0,0, 13,-11,19) and other stuff but everything i tried failed miserably. 我只是想绘制一条连接这两点的可见线...我尝试了plot3(0,0,0,13,-11,19)和其他东西,但我试过的一切都失败了。

Here's how: 这是如何做:

% Your two points
P1 = [0,0,0];
P2 = [13,-11,19];

% Their vertial concatenation is what you want
pts = [P1; P2];

% Because that's what line() wants to see    
line(pts(:,1), pts(:,2), pts(:,3))

% Alternatively, you could use plot3:
plot3(pts(:,1), pts(:,2), pts(:,3))

Admittedly, this might seem a bit counter-intuitive at first, but in the long run it'll make sense. 不可否认,一开始这看起来有点反直觉,但从长远来看,这是有道理的。

If you read doc plot or doc line , you'll see that each expects sets of x , y and z data, respectively. 如果您阅读doc plotdoc line ,您会看到每个都分别需要xyz数据 That is, using 也就是说,使用

plot3(X,Y,Z)

with X , Y and Z some matrices, plot3 will draw a line from the first triplet ( X(1) Y(1) Z(1) ) to the second triplet ( X(2) Y(2) Z(2) ) and so on -- same for line . 使用XYZ一些矩阵, plot3将从第一个三元组X(1) Y(1) Z(1) )到第二个三元组( X(2) Y(2) Z(2) )绘制一条线等等 - line相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM