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

你说的曾经没有我的故事 提交于 2019-12-01 02:19:49

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. 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!