# Triangle mesh filled contour in MATLAB

## weblog/

Here is matlab snippet that takes a 2D triangle mesh and a function and produces a pretty, filled contour plot in MATLAB. If your mesh vertices are stored in V, and your triangles in F, then use my previously posted code to get the boundary/outline edges of your mesh in O. Then you can create a contour plot of some function W defined over V using:
``````% resample W with a grid, THIS IS VERY SLOW if V is large
[Xr,Yr,Wr] = griddata(V(:,1),V(:,2),W,unique(V(:,1)),unique(V(:,2))');
% find all points inside the mesh, THIS IS VERY SLOW if V is large
IN = inpolygon(Xr,Yr,V(O,1),V(O,2));
% don't draw points outside the mesh
Wr(~IN) = NaN;
contourf(Xr,Yr,Wr)
``````
And for a mesh like this one: You get something like this: You can also approach a continuous contour with this:
``````contourf(Xr,Yr,Wr,200)
``````trisurf(F,V(:,1),V(:,2),W,'FaceColor','interp','EdgeColor','interp')
``````line(V([O(:);O(1)],1),V([O(:);O(1)],2),'Color','k','LineWidth',6)