我在3D矩阵上有3个点,我想连接它们。到目前为止,我可以创建一条从1点到另一点的线,但我想基本上创建一个连接3个点的三角形。
这是我到目前为止所拥有的
a = [1 1 2];
b = [4 2 9];
c = [5 3 6];
ab = b - a;
bc = c - b;
ca = a - c;
n = max(abs(ab)) + 1;
n1 = max(abs(bc)) + 1;
n2 = max(abs(ca)) + 1;
s = repmat(linspace(0, 1, n)', 1, 3);
for d = 1:3
s(:, d) = s(:, d) * ab(d) + a(d);
end
s1 = repmat(linspace(0, 1, n1)', 1, 3);
for d1 = 1:3
s1(:, d1) = s1(:, d1) * bc(d1) + b(d1);
end
s2 = repmat(linspace(0, 1, n2)', 1, 3);
for d2 = 1:3
s2(:, d2) = s2(:, d2) * ca(d2) + c(d2);
end
s = round(s);
s1 = round(s1);
s2 = round(s2);
Z = 593;
N = 512;
X = zeros(N, N, Z);
X1 = zeros(N, N, Z);
X2 = zeros(N, N, Z);
X(sub2ind(size(X), s(:, 1), s(:, 2), s(:, 3))) = 1;
X1(sub2ind(size(X1), s1(:, 1), s1(:, 2), s1(:, 3))) = 1;
X2(sub2ind(size(X2), s2(:, 1), s2(:, 2), s2(:, 3))) = 1;
clf
plot3(s(:, 1), s(:, 2), s(:, 3),1 ,s1(:, 1), s1(:, 2), s2(:, 3),2,s2(:, 1), s2(:, 2), s2(:, 3),3, 'r.-')
%plot3(s1(:, 1), s1(:, 2), s1(:, 3), 'r.-')
axis(N * [0 1 0 1 0 1])
grid on
答案 0 :(得分:2)
要简单地加入3个点,plot3
函数是一个不错的选择,但它不会在数据集的第一个和最后一个点之间画一条线,所以要强制最后一行,你可以简单复制集合末尾第一个点的坐标,这样就可以关闭图形。
请注意,我重新组织了坐标数据(从每个点的变量到每个坐标轴的变量)
a = [1 1 2]; %// I assumed these are [x y z] coordinate for point "a"
b = [4 2 9];
c = [5 3 6];
C = [a ; b ; c ] ; %// place all coordinates in a single matrix
C(end+1,:) = C(1,:) ; %// replicate the first line in last position to close the triangle
x = C(:,1) ; %// place all "X" coordinate in a vector
y = C(:,2) ; %// then Y
z = C(:,3) ; %// then Z
hp = plot3(x,y,z) ;
或者,如果你想要一个彩色三角形,你也可以使用patch
(这个函数会自动关闭图形,不需要添加你复制的点,但如果你需要添加复制点,你做)。
hpt = patch(x,y,z,'r') ;