2012-11-19 212 views
15

我想用我從3個點,其中計算的矢量繪製一個平面:如何在Matlab中繪製3D平面?

pointA = [0,0,0]; 
pointB = [-10,-20,10]; 
pointC = [10,20,10]; 

plane1 = cross(pointA-pointB, pointA-pointC) 

如何在3D繪圖「基準面1」?

+0

我相信有SE網站matlab。 –

+0

nope,我的錯誤 - > http://area51.stackexchange.com/proposals/38040/matlab –

+0

你很可能需要生成一堆點在飛機上,然後繪製那些使用「衝浪」或一些類似的功能... – Isaac

回答

26

這裏的陰謀使用fill3飛機一個簡單的方法:

points=[pointA' pointB' pointC']; % using the data given in the question 
fill3(points(1,:),points(2,:),points(3,:),'r') 
grid on 
alpha(0.3) 

enter image description here

+0

'fill3'採取'X, Y,Z'作爲輸入而不是3分。看看你畫的飛機,它不會通過'(0,0,0)'。你畫了一個穿過'(0,-10,10)','(0,20,20)'和'(0,10,10)' –

+0

的平面根據Matlab文檔(fill3的第二行)「 fill3(X,Y,Z,C)填充三維多邊形,X,Y和Z三元組指定多邊形頂點「。儘管我輸入點數爲'fill3'(使用了錯誤的維數),但我做了一個錯誤,現在已經糾正了。感謝您的注意。我仍然認爲一個班輪比幾條線更好...... – bla

+0

沒關係,無論如何你都有我的upvote,我只是想讓你糾正這個錯誤:) –

13

您已經計算出的法向量。現在你應該決定你的飛機在xz的限制,並創建一個矩形的補丁。

解釋:每個平面可用其法向量(A,B,C)和另一個係數D來表徵。這架飛機的方程式是AX+BY+CZ+D=0。跨產品之間兩點之間的差異,cross(P3-P1,P2-P1)允許查找(A,B,C)。爲了找到D,簡單地把任何一點到上面提到的公式:

D = -Ax-By-Cz; 

一旦你有平面的方程,你可以坐4分擺在這個平面上,並得出他們之間的補丁。

enter image description here

normal = cross(pointA-pointB, pointA-pointC); %# Calculate plane normal 
%# Transform points to x,y,z 
x = [pointA(1) pointB(1) pointC(1)]; 
y = [pointA(2) pointB(2) pointC(2)]; 
z = [pointA(3) pointB(3) pointC(3)]; 

%Find all coefficients of plane equation  
A = normal(1); B = normal(2); C = normal(3); 
D = -dot(normal,pointA); 
%Decide on a suitable showing range 
xLim = [min(x) max(x)]; 
zLim = [min(z) max(z)]; 
[X,Z] = meshgrid(xLim,zLim); 
Y = (A * X + C * Z + D)/ (-B); 
reOrder = [1 2 4 3]; 
figure();patch(X(reOrder),Y(reOrder),Z(reOrder),'b'); 
grid on; 
alpha(0.3); 
+0

如果B是0,這不起作用 – Eric

+0

@Eric,true,它應該有零或接近零係數的其他情況。 –

+0

看到我的答案爲另一個解決方案 – Eric

1

這就是我想出了:

function [x, y, z] = plane_surf(normal, dist, size) 

normal = normal/norm(normal); 
center = normal * dist; 

tangents = null(normal') * size; 

res(1,1,:) = center + tangents * [-1;-1]; 
res(1,2,:) = center + tangents * [-1;1]; 
res(2,2,:) = center + tangents * [1;1]; 
res(2,1,:) = center + tangents * [1;-1]; 

x = squeeze(res(:,:,1)); 
y = squeeze(res(:,:,2)); 
z = squeeze(res(:,:,3)); 

end 

,你會爲使用:

normal = cross(pointA-pointB, pointA-pointC); 
dist = dot(normal, pointA) 

[x, y, z] = plane_surf(normal, dist, 30); 
surf(x, y, z); 

其中在飛機上繪製邊長爲60的正方形問題

0

我想補充一下Andrey Rubshtein給出的答案,他的代碼在B = 0時非常好用。這裏是他的代碼編輯後的版本

下面的代碼的工作原理當不爲0

normal = cross(pointA-pointB, pointA-pointC); 
x = [pointA(1) pointB(1) pointC(1)]; 
y = [pointA(2) pointB(2) pointC(2)]; 
z = [pointA(3) pointB(3) pointC(3)]; 
A = normal(1); B = normal(2); C = normal(3); 
D = -dot(normal,pointA); 
zLim = [min(z) max(z)]; 
yLim = [min(y) max(y)]; 
[Y,Z] = meshgrid(yLim,zLim); 
X = (C * Z + B * Y + D)/ (-A); 
reOrder = [1 2 4 3]; 
figure();patch(X(reOrder),Y(reOrder),Z(reOrder),'r'); 
grid on; 
alpha(0.3); 

下面的代碼工作時,C不是0

normal = cross(pointA-pointB, pointA-pointC); 
x = [pointA(1) pointB(1) pointC(1)]; 
y = [pointA(2) pointB(2) pointC(2)]; 
z = [pointA(3) pointB(3) pointC(3)]; 
A = normal(1); B = normal(2); C = normal(3); 
D = -dot(normal,pointA); 
xLim = [min(x) max(x)]; 
yLim = [min(y) max(y)]; 
[Y,X] = meshgrid(yLim,xLim); 
Z = (A * X + B * Y + D)/ (-C); 
reOrder = [1 2 4 3]; 
figure();patch(X(reOrder),Y(reOrder),Z(reOrder),'r'); 
grid on; 
alpha(0.3); 
+0

你爲什麼使用reOrder? @奎師那切塔尼亞 –