MATLAB Answers

0

how to draw multiple patches with different number of vertices?

Asked by Itzik Ben Shabat on 8 Feb 2015
Latest activity Commented on by Ted Shultz on 2 Nov 2018
Hi, I am trying to draw many patches which have a different number of vertices. I have the vertices stored in the matrix V with size(MX3), m is the number of points. and I have the faces vertex indexes in a cell array of size (NX1), N is the number of faces. in each cell of C there is a different number of indexes for a given face (3 vertices indexes for a triangle, 4 for a quad etc). my question is: is there a better way to draw all of these patches without using a for loop? i tried using patch(FV) but t doesnt work for cell arrays. so for example:
V=[0 0;0 1;1 0;1 1;2 0];
C={[1 2 3];[3 2 4 5]};
FV.vertices=V;
FV.faces=C{1};
patch(FV);
FV.faces=C{2};
patch(FV,'facecolor','blue');
this will draw the first polygon black and the second blue but the question is how can i draw both of them at once? (in the real case i have a much larger number of polygons); thanks

  0 Comments

Sign in to comment.

2 Answers

Answer by Stephen Cobeldick on 8 Feb 2015
Edited by Stephen Cobeldick on 8 Feb 2015
 Accepted Answer

Yes, you can plot them all at once by padding your matrix with NaN's.
The patch documentation states MATLAB does not require each face to have the same number of vertices. In cases where they do not, pad the end of the Faces matrix with NaNs. To define a patch with faces that do not close, add one or more NaNs to the row in the Vertices matrix that defines the vertex you do not want connected.
If you have just a few vectors, then you can create this matrix by hand.
If you need to combine many such vectors together, padding them with NaN's, you should be able to adapt this code to concatenate unequal-length vectors together into one matrix, padding with NaN's:
A = {[0,1,2],[3],[4,5,6],[7],[8,9]};
B = cellfun('length',A);
C = cellfun(@(v,n)[v,nan(1,max(B)-n)],A,num2cell(B),'UniformOutput',false);
C = vertcat(C{:});

  4 Comments

Show 1 older comment
I got past this part, and was able to pad out my matrix, but now the first set of polygons won't display any color.
I tried making my polygons with multiple separate calls to 'patch', but the axes' color limit scale would not readjust with each one (i.e., even if polygon 2 was higher value than polygon 1, both would be displayed as the darkest shade on the scale)
@Dave: Interesting problem. I think you should submit this as a new question, and please give the exact values that you are using to create the patches with.

Sign in to comment.


Answer by Ted Shultz on 1 Nov 2018

Sometimes when I run into this class of problem, I find that the polyshape tool is the answer. For example, if I want to plot two triangles and a square, all at once (and the same color/properties) I can use:
% shapes are in columns
x = [
2 5 3
2 5 4
8 8 4
nan nan 3];
y = [
4 0 1
8 2 1
4 0 2
nan nan 2];
figure
ps=polyshape(x(:),y(:));
h=plot(ps);

  2 Comments

Thanks for the answer. This question was actually posted before ' polyshape was introduced ( R2017b). What are its advantages over the good old patch ?
I’m certainly no expert, and patch certainly is the right tool to use most times. Polyshape is a powerful patch like object that I’m sure must have some overhead associated with it. In some situations, I’ve found that using polyshape can speed up my code (or at least speed up writing my code…). For example when I am using a “for” loop, I can generate all my Polyshape objects before displaying them (there is a significant slowdown when plotting many objects on a map). Polyhshape also has some nice methods built in that are able to replace my own much slower code (like the ability to union/subtract/xor/add/intersect polyshapes together, rotation, etc).
The code @Stephen Cobeldick included is pretty slick, and actually did answer your question, I only added my comment because I figured someone who found this old question may also want to be aware of polyshape.

Sign in to comment.