How do I create a patch with multiple faces, each with a different number of vertices?

8 Ansichten (letzte 30 Tage)
How do I create a patch with multiple faces, each with a different number of vertices?
For example, if I want to draw a rectangular prism, with vertices:
xyz = [0 0 0;1 0 0;0 1 0;0 0 1;1 0 1;0 1 1];
triangular faces:
T=[1 2 3;4 5 6];
and rectangular faces:
R=[1 2 5 4;2 3 6 5;1 3 6 4];
Can I do this using one call to the PATCH function?

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 27 Jun. 2009
To draw a patch with multiple faces, not all of which have the same number of vertices, you will need to either use multiple calls to the PATCH function or pad short rows with repeated copies of their first elements. To use multiple calls to PATCH, use the following code:
% Generate data
xyz = [0 0 0;1 0 0;0 1 0;0 0 1;1 0 1;0 1 1];
T=[1 2 3;4 5 6];
R=[1 2 5 4;2 3 6 5;1 3 6 4];
% Draw triangular patches - facealpha simply makes the patches translucent
P1=patch('Vertices',xyz,'Faces',T,'facecolor',[1 0 0],'facealpha',0.5);
% Draw rectangular patches - facealpha simply makes the patches translucent
P2=patch('Vertices',xyz,'Faces',R,'facecolor',[1 0 0],'facealpha',0.5);
P=[P1;P2];
view(3)
Now you can manipulate the handles of both patches simply by manipulating P.
To draw the same object using padding, you can do this:
% Generate data
xyz = [0 0 0;1 0 0;0 1 0;0 0 1;1 0 1;0 1 1];
T=[1 2 3;4 5 6];
R=[1 2 5 4;2 3 6 5;1 3 6 4];
% Assume R has more columns than T
M=ceil(size(R,2)/size(T,2));
% Pad T
T2=repmat(T,1,M);
F=[T2(:,1:size(R,2));R];
% Draw the patch - facealpha simply makes the patches translucent
P=patch('Vertices',xyz,'Faces',F,'facecolor',[1 0 0],'facealpha',0.5);
view(3)
This generalizes to more than 2 partial face matrices; find the largest, replicate the smaller ones as many times as needed, and concatenate the correctly sized portions of the padded matrices together.
or:
% Generate data
xyz = [0 0 0;1 0 0;0 1 0;0 0 1;1 0 1;0 1 1];
T=[1 2 3;4 5 6];
R=[1 2 5 4;2 3 6 5;1 3 6 4];
% I know that T is 2 x 3 and R is 3 x 4, so I need to pad T with its first column
F=[T T(:,1);R];
% Draw the patch - facealpha simply makes the patches translucent
P=patch('Vertices',xyz,'Faces',F,'facecolor',[1 0 0],'facealpha',0.5);
view(3)
This requires a bit more manual manipulation, to correctly size the matrix.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by