File Exchange

image thumbnail

MESH2D: Delaunay-based unstructured mesh-generation

version (386 KB) by Darren Engwirda
Generate unstructured meshes for general two-dimensional geometries.


Updated 27 Mar 2021

From GitHub

View Version History

View license on GitHub

MESH2D is a MATLAB-based Delaunay mesh-generator for two-dimensional geometries. It is designed to generate high-quality constrained Delaunay triangulations for general polygonal regions in the plane. MESH2D provides simple and yet effective implementations of "Delaunay-refinement" and "Frontal-Delaunay" triangulation techniques, in additional to "hill-climbing" type mesh-optimisation. Support for user-defined "mesh-spacing" functions and "multi-part" geometry definitions are provided, allowing varying levels of mesh-resolution to be specified within complex domains. Algorithms implemented in MESH2D are "provably-good" - ensuring convergence, geometrical and topological correctness, and providing guarantees on algorithm termination and worst-case element quality bounds. MESH2D typically generates very high-quality output, appropriate for a variety of finite-volume/element type applications.

See TRIDEMO to get started with a set of example problems:

tridemo(0); % a very simple example to get everything started.
tridemo(1); % investigate the impact of the "radius-edge" threshold.
tridemo(2); % Frontal-Delaunay vs. Delaunay-refinement algorithms.
tridemo(3); % explore impact of user-defined mesh-size constraints.
tridemo(4); % explore impact of "hill-climbing" mesh optimisations.
tridemo(5); % assemble triangulations for "multi-part" geometries.
tridemo(6); % assemble triangulations with "internal" constraints.
tridemo(7); % investigate the use of "quadtree"-type refinement.
tridemo(8); % explore use of custom, user-defined mesh-size functions.
tridemo(9); % larger-scale problem, mesh refinement + optimisation.
tridemo(10); % medium-scale problem, mesh refinement + optimisation.

Additional information and references are available via the GitHub repository:

MESH2D is a simplified version of my three-dimensional mesh-generation algorithm JIGSAW, providing an implementation of "provably-good" Delaunay-refinement and Frontal-Delaunay triangulation techniques. JIGSAW is available here:

MESH2D also makes use of my AABBTREE and FINDTRIA packages to compute efficient spatial queries and intersection tests. See and for details.

Cite As

D. Engwirda, Locally-optimal Delaunay-refinement and optimisation-based mesh generation, Ph.D. Thesis, School of Mathematics and Statistics, The University of Sydney,, 2014.

D. Engwirda, Unstructured mesh methods for the Navier-Stokes equations, Honours Thesis, School of Aerospace, Mechanical and Mechatronic Engineering, The University of Sydney, 2005.

Comments and Ratings (139)


Hello, I would like to ask you whether mesh size function in mesh2d is applicable to distmesh2d, which is an implicit definition of geometric subdivision method. In addition, after running your program, I don't quite understand the mesh adaptation. For the article "mesh size functions for implicit geometries and PDE based gradient" written by per Olof person, I would like to ask you whether the mesh size function in mesh2d is applicable to distmesh2d It is pointed out that curvature adaptive, dull students, has not been understood in your algorithm, if there are some simple examples to guide it. Thank you. Student's email Thank you very much.


家铭 崔

Thanks for this toolbox.
I have a question: Is there a function to export the mesh information (such as nodal number, nodal co-ordinate, element number)? I want to submit these message to FEM
solver. Thanks very much.

Darren Engwirda

@chunfeng: No, MESH2D doesn't really have direct support for moving mesh applications. JIGSAW ( offers somewhat finer control for remeshing operations, though moving mesh workflows are often highly specialised...


Hello, what I want to ask is, when the partition area changes, can mesh generation be locally meshed without all re meshing. For example, for a moving circle in a rectangle, when the circle moves, only the area around the circle needs to be locally meshed, and other parts only need to be changed appropriately. Thank you and look forward to your reply


Darren Engwirda

@happy aprillia: the list of examples in tridemo.m should get you off to a good start.

happy aprillia

thank you for providing the toolbox. how can we use the toolbox?

Darren Engwirda

@Oliver Lojek: No, I'm not sure what the issue may be at this stage --- there aren't any known limitations. The best thing to do is to open an "issue" via GitHub and attach the data / scripts you're using. If I can reproduce the error I'll be able to provide a little more feedback.

Oliver Lojek

Dear Darren, thank you for providing this very powerful toolbox to the public. Using the toolbox on polygons derived from shape files with geo-coordinates, works well for WGS84 reference system with Latitude & Longitude values, however if I project/change the reference system and use the same polygon, Mesh2D initially generates an identical grid but when I attempt to refine the initial grid by means such as laid out in the tridemo.m (cases 3 or 8) the toolbox gets caught in an endless iteration, whereas on Latitude & Longitude it runs 4-5 iterations depending on the refinement settings.

Assuming it has to do with the coordinate values, since they are the only thing that changes from Lat. Long. (degree) to Universal TransMercator (UTM, kilometers), I reduced the UTM input values by subtracting the mean to obtain small numbers like the Lat./Long. case but to no avail. Has anybody encountered a similar problem? I'm guessing it's me but I cannot fathom where I go wrong.

Help would be much appreciated,


Jonas Allgeier

Lucas Setterholm


Hao Qiu

very helpful!!!



hongah chae

Abhishek Chauhan

Darren Engwirda

@Elias Wirth: The best place to start are the set of examples in tridemo.m.

Elias Wirth

How do I set this up?

Babak Kiani

Mahmoud Sami

How can I make Delaunay in Mesh 3D?

Yu-Lin Tsai

Thanks to the author for this awesome MATLAB toolbox for generating unstructured grids!

Darren Engwirda

@Ben: The mesh-spacing information is actually just applied everywhere - on the boundaries and in the interior - so you don't need to do anything special to get this to work. Perhaps tridemo(8) and/or tridemo(3) may be helpful here.


Hi Darren, I understand you can impose a target mesh size along the outer perimeter of the domain. But is it also possible to impose a target mesh size within the interior of the domain? In other words: is there a way to locally refine in certain areas inside the domain, based on a file describing the target mesh size in 2D?

Michael Barrow

Do you have an LGPL licensed version of this code? I am using it for academic research and would like to supply this code along with my research work to others. But the GPL license would mean others would be forced to apply GPL to their entire project if they want to use any code with this integrated. Let me know.

Darren Engwirda

@Neal Morgan: I haven't been able to reproduce this issue - setting opts.disp = inf seems to suppress output for me. Would you email me an example script and a description of your MATLAB release etc.

Darren Engwirda

@heyworld: I'm not sure that I understand exactly what you're asking - it's possible to setup geometries that contain subdomains (see TRIDEMO(5) for example), and to define user-specified mesh-spacing functions (see TRIDEMO(8) for instance). I hope that some combination of these features can be used to satisfy the various far-field and interior constraints you describe.

Neal Morgan

I love your program and it has been very helpful for my research. I was wondering if there is a way to suppress the refine and smooth triangulation tabular output.
I have tried setting the options.DISP={inf}; as suggested in the comments of refine2 but it does prevent the command line output.


Dear Dr. Darren, i haver a prolem that is how to discrete two region with different resolution or size function. When we try to solve a PDE which has infinity boundary, one needs to truncate into a finite region. howerer, for approximating better with the infinity, we usually generate a larger domain whinch includes a subdomain in which we construct refined mesh. so how to do it? Please giver some suggestion or strategies. The same problem can be met in threee dimension Volume discrete. Many Thanks!

Sambit Mishra

Damn good this is !!!

Marcos Delgado

Dung Tran

Thomas Williams

Hi Darren,
I found this incredibly useful a coursework project involving implementing an as-rigid-as-possible shape deformation algorithm. I wanted to experiment with other 2D meshes, but could not find anything suitable on the web. I was able to use MESH2D to make my own from tracing images and using those mouse clicks as the border vertices. Thank you for the great work.

Darren Engwirda

@Jon Jon: I suggest you use the current version of the code - the new implementation is generally better / more reliable / faster, and is up-to-date with the current MATLAB toolchain.

Jon Jon

Hi,because of using history version mesh2Dv24,I've got the same problem as Marcus's(Spe 19 2013),When I use meshfaces to get 2D unstructured mesh generation for polygonal geometry I get the message: 'Warning: Duplicate data points have been detected. Some data points do not map to vertices in the triangulation. To avoid this behavior, call UNIQUE on the data points prior to calling DELAUNAY.'
In delaunayn at 58
as follow:
if dupesfound warning(message('MATLAB:delaunayn:DuplicateDataPoints'));
I want to know how to deal with this problem. I appreciate if any one could help me

Jon Jon

Darren Engwirda

@Jon Jon: TRIDEMO contains a list of example problems, and is probably the best place to start. REFINE2 and SMOOTH2 are the primary mesh refinement + smoothing algorithms.

Jon Jon

hi I'm a new learner from China,i have a question that which file(.m) can i begin to run the program? i want to try it.
thanks a lot!

Darren Engwirda

@lutzzz: Yes, you should be able to use TRICON2 to do this. In the list of edges returned, the boundary edges are those that have only 1 non-null triangle neighbour (i.e. edge(:,4) == 0). The list of boundary nodes are those referenced by these boundary edges.


Hi again,
the modelling tool I use requires the node numbers of the outer boundaries. At first I thought I could use the nodes from my input shape, but since I'm using refinement by mesh-size function, additional nodes along the boundaries are introduced with mesh generation. Is there a way to get these using the tool?

Darren Engwirda

@heyworld: It is possible to build edge-based information - the TRICON2 routine should be helpful here.


Dear Darren, how do i calculate the eage information like eage number? the program do not output that.

Charles Aboussafy

Darren Engwirda

@Olivier: I think this discussion is probably better suited to email. If you have a specific case you'd like me to look at, feel free to send the details.

Olivier Barbara

Thanks a lot for your immediate reply from my 17 Jan 2018 topic. The process you described is already exactly what I implemented and it works perfectly already all around the profile. The difficulty remains within the Wake area where the 2 boundary layers follow side by side a curve I impose from the profile trailing edge up to the end of the mesh area. The offset curves of the Wake one have 2 different point distributions (generated with mesh2D) that are not compatible to merge quads at the middle Wake curve.
The only solution I found for the moment is (1) to perform the mentioned process, (2) to locally modify the distribution points of the 2 offset Wake curves, (3) regenerate the triangular mesh with the Delaunay function. That' works, but I'm not confident of the triangle cell skewness generated close to the modify edges.
Your tridemo 5 example shows you have already succeeded to partially impose some edge constraints. Don't you think your algorithm could be adapted up to fully impose some edge point distributions?
Best regards

Darren Engwirda

@lutzzz: The code is a 'Constrained Delaunay Triangulation' (CDT) algorithm. The digits printed relate to the size of the tessellation, where |CDT1| is the number of edges (the 1-dimensional facets), and |CDT2| is the number of triangles (the 2-dimensional facets). Intersecting polygons definitely are a problem, as it is often unclear what is 'inside' or 'outside' the domain, and which constraints really need to be satisfied. The FIXGEO2 routine may be helpful in these cases.


Thanks a lot, the tool seems to do a good job. I have a question about the output: What are |CDT1(X)| and |CDT2(X)|? I have noticed that when |CDT2(X)| doesn't grow larger with each iteration the program doesn't seem to finish and eventually crashes because of too little memory. What could be causing this behaviour? Are there some tips as to what the polygons should look like for the algorithm to converge? Are intersecting polygons a problem?

Darren Engwirda

@Olivier Barbara: Not directly, no - you'd have to do some of the work and create the boundary-layer mesh yourself. If you were interested to do this, it may be possible to: (1) create an 'envelope' for the boundary-layer by 'offsetting' the required polygons, (2) mesh the remainder of the domain using MESH2D, and (3) create a compatible mesh in the boundary-layer by 'marching' elements back to the initial boundaries.

Olivier Barbara

Thanks for this mesh generator.It will help me at last to relaunch some topics I gave up 10 years ago.
I started on a simple mesh around a NACA profile where I would like to impose a thin boundary layer (quads) around.
I haven't find yet if it's possible to force as an input a distribution of points on the boundary of the unstructured mesh generated with this tool. If this option exists, it will help me to merge both structured boundary layer and unstructured external mesh together with coincident points at the frontier.
This question is close to one asked by II II the 31 oct 2016 (see below).
Many thanks in advance.
Olivier Barbara

Darren Engwirda

@Graham Griffiths: Typically you would start with a given geometry and call REFINE2 to build a conforming triangulation. If instead you're just looking to triangulate points, the standard DELAUNAY function might be what you're after...

Graham W Griffiths

Thanks for a great program!
Is it possible to start with just a list of the vertices, i.e. without specifying the triangulation.

Darren Engwirda

@Robin Maye: You can specify a target mesh size distribution by defining a "mesh-spacing" function H(x,y) - i.e. the HFUN argument in REFINE2. H(x,y) defines the target length of triangle edges. Type "HELP REFINE2" for details, and see, for instance TRIDEMO(8) for example code.

Robin Maye

Hello, how do I if I want to mesh with elements of thinner size only for elements around a precise coordinate on the y-axis ?

Darren Engwirda

@S W: I'm not sure about the multi-part issue -- feel free to email me that geometry and I'll look into it. No attempt is made to reorder nodes/elements for sparse bandwidth, fill-in, etc. Generally, I think it's best to leave these kind of details to the sparse-solver itself, as various strategies are available. MATLAB also provides the SYMAMD and SYMRCM routines, which can be used to compute various sparse matrix permutations.


Hi Darren, thanks for a great tool. For a multipart geometry, refine2 only seems to be meshing the first 3 parts. Am I going wrong somewhere? Also is the node numbering for multiparts optimised to make a diagonal centered stiffness matrix? Thank you!

Darren Engwirda

@sedat dogru: You may wish to start with the examples in TRIDEMO. In particular TRIDEMO(0) shows how to setup a simple geometry and call the triangulation routines.

sedat dogru

hello, I do not understand how to start to use it. Can you help me, please?


Thank you for quick response. I was just little confused because there is no mesh2D file. After few practices with REFINE2, I'll test TRIDEMO to understand detailed functionalities. Thank you again!

Darren Engwirda

@JUNHYEONG PARK: The main triangulation routine is REFINE2. This is probably where to start. I would also encourage you to have a look at the code in TRIDEMO though -- I've tried to structure these examples to introduce various functionality.


After I downloaded zip file and unzipped it, I couldn't find mesh2D file. Can anybody recommend how to get started easily not by tridemo.m?


Thanks for the very quick update! The extra unconnected vertices were making the FEA code unhappy.

Darren Engwirda

@Erik: To prevent issues with floating-point precision near the edges of the convex hull, the domain is inflated by adding four additional vertices as an external bounding box. These are the extra vertices you see in the output. These padding vertices are never indexed by the triangulation.

While this isn't a bug, I've updated the code so that the user doesn't see this anymore. The domain is still inflated internally, but any extra vertices are pruned off the data structures before they're returned. Your example should now return only the four vertices passed as input.


I have been using the old version for a while, this looks like a nice update. A quick question - for a very simple square geometry, I get more vertices than I expect, some of which are not in the mesh domain. Any hints as to what I am doing wrong?


geom = [-1 -1; 1 -1; 1 1; -1 1]
edge = [1 2;2 3;3 4;4 1]
[vert,etri,tria,tnum] = refine2(geom,edge)

vert =

-1 -1
1 -1
1 1
-1 1
-2 -2
2 -2
2 2
-2 2


Thank you for making this code available. It makes a show stopper analysis step for many users easy to use. Well done.


Can mesh2d control edge length? I used other programs before which specify the maximum triangle area (e.g. 100 m2), but they give real small elements (e.g. 10 m2). So I wonder if mesh2d can control the edge length instead of mesh area. Thank you.


Genaro Beas

Franck n'guyen

perfect as usual !

Darren Engwirda

@NC: It's possible to pass a user-defined mesh-spacing function to the REFINE2 routine. If you already have an expected water-depth function, you should be able to build a related spacing function. Have a look at the example code in TRIDEMO for more details.


Hi, I am using mesh2d to generate a computational grid to simulate water wave behaviuour in shallow water coastal environments. Does anyone have any tips on how to (automatically) implement a mesh refinement based on the water depth (finer mesh in shallower water)? Cheers


Hi, I am trying to create my own function to define the mesh size. I want to pass some other arguments to the function I have defined, hfun1. I have done it like this, following the manual provided in mesh2d.m:
hdata.args = {cp(1),cp(2)}; = @hfun1;
function h = hfun1(x,y,args{:})
h = 10-0.1*sqrt( (x-cp(1)).^2+(y-cp(2)).^2 );

When I do this I get an error "Unbalanced or unexpected parenthesis or bracket."
If I remove the brackets and say
function h = hfun1(x,y,args)
It says too many input arguments.
I have been trying to fix this, I am not able to understand where I am going wrong. Can somebody please help me?

Nasser Ramsis

im getting the following error:

Undefined function or variable 'tsearch'.

Error in meshfaces>boundarynodes (line 199)
i = tsearch(ph(:,1),ph(:,2),th,p(:,1),p(:,2));

Error in meshfaces (line 140)
pbnd = boundarynodes(qtree.p,qtree.t,qtree.h,node,edge,options.output);

Error in mesh2d (line 102)
[p,t,junk,stats] = meshfaces(node,edge,[],hdata,options);

Franck n'guyen

magnifique ! merci !


Hi, excellent work on these tools. I wonder if you could help me with something. I'm trying to alter your refine.m function to allow for another parameter to be added. I have boundary codes (0-2) at each of the original nodes, I need for those codes to remain at the same nodes and populate the new nodes made from your function, i.e. one of my boundaries has all code 1 because it is a land boundary so I need all the newly created nodes on that boundary to have a 1 value.
Thanks, and again, excellent function :)

Matthias Braendel

ll ll

Hi, I really appreciate for you work. However, i am wondering if this tool could generate mesh with the boundary node constant. That is the mesh generated with the input node (on the edge), the new node (on the edge) will not be added.

Alexander Naitzat

Hello. Excellent tool. Where can I find data set of interesting 2D meshes, or data set of interesting polygons from which I can generate meshes ?

Richard Crozier

there's a version of this with some bug fixes (e.g. missing tsearch) and more examples here:

Smitha Subeesh

Smitha Subeesh


This work is really awesome. Its very helpful for me. But I am facing problem with parabolic functions in distmesh2d.m
Please help me by giving an example of discretization of a domain which is an intersection of 2 parabolas in the 1st quadrant.


jun liu

Hi,I've got the same problem as Marcus's(Spe 19 2013),When I use meshfaces to get 2D unstructured mesh generation for polygonal geometry I get the message: 'Warning: Duplicate data points have been detected. Some data points do not map to vertices in the triangulation. To avoid this behavior, call UNIQUE on the data points prior to calling DELAUNAY.'
In delaunayn at 58
as follow:
if dupesfound warning(message('MATLAB:delaunayn:DuplicateDataPoints'));
I want to know how to deal with this problem. I appreciate if any one could help me.


Thank you for your work. I am trying to use it for meshing a star but I would like to get an uniform meshing;
Is there anyway to do that without using refine ?

Mario Koddenbrock


Hello, first thank you for this great tool!
My question is, there is a solution to the problem with the deleted function dsearch in tinterp?

Rui Liang

Just want to inform Jeremy and Frederic that, read Mike's fix carefully. Do not just change "tsearch" to "tsearchn" but also notice the changes in the brackets after "tsearchn". It works for me.


Hey, Got the same errors as Frederic, and tried to fix them, but it's still not working!

Sergei P.

Frédéric Moresmau

Hi, I get error in meshdemo
I fixed the tsearch with the tserachn how (Spe 23 2013) suggsted.
Now the next error is

Error using fixmesh
Too many input arguments.

Error in meshfaces (line 157)
[p,t,fnum,fnum] = fixmesh(p,t,[],fnum);

how to fix all this ?


zheng kang

Thanks Darren for sharing this. Helps a lot!


does anyone know why this hit me with this problem:
"Checking Geometry
Undefined function or variable 'wbar'.
Error in meshfaces (line 127)
Error in facedemo (line 39)
meshfaces(node,edge,face,hdata); "

Im trying to run this:

node = [0.0, 0.0; .4,0.0;0.2,0.5;0.4,0.5;0.2,1; 0.0,1; ...

edge = [1,2; 2,3; 3,4; 4,5;5,6;6,1;...

face{1} = [1,2,3,4,5,6];
face{2} = [7,8,9,4,3,2];
face{3} = [10,11,12,8];

hdata.hmax = 0.05;

which should be a sawtooth grating between air layers.


Hi, finally some clear meshing program also for beginners! Thanks! There is anyhow a problem with "DSEARCH" in mesh_collection(6). Is it anyhow possible to repair it?


We have been using this function since early 2010 and has proved very valuable for creating basic 2D meshes for finite element analysis.

The only issue is that with newer releases (since R2012?) the "tsearch" function is no longer supported by Matlab.

We have used the fix proposed by Mike (23 Sep 2013) and it has worked reasonably well for our problems and was fairly simple to fix in the source code.

Thanks again Darren & Mike for the quick fix.

This type of submission is one reason the File Exchange is simply awesome.

Tanvir Faisal

there are few functions seem to be missing such as tsearch, pointLocation. any idea??



unstructured mesh generator

Ademiloye Adesola

Ademiloye Adesola

Excellent work! I suggest Prof Darren should try to implement some of the bugs fixed in the comments below when preparing the newer version.


Excellent ...code..very useful for FEA..

Putra Arri Sandhi


you are a genius!!!! awesome addition... thanks very helpful....

Thomas Benson

This is a great submission! A minor change was required for my application. Reason: The triangulation tends to be slightly over-refined on complex boundaries. For some applications this is a bonus, but for hydrodymanic modelling it is not really necessary. I therefore replaced the line in 'quadtree.m' (about line 64) which reads:

L = 2.0*dist2poly(wm,edgexy,2.0*len);

with the following:


This helps prevent over refinement and a nice mesh. Excellent work Darren!



Great work thank you very much.

I've got a question I appreciate if any one could help, where exactly the changes can take place ? I know it would be most about the geometry but I wasn't able to play with it.


I'm doing something that must be pretty standard. I'm needing to mesh a 2d domain with an internal boundary. My approach has been to create 'holes' with zero area, which imposes duplicate nodes. Mesh2d seems able to remove these and gives e.g., the message 'WARNING: 2 duplicate node(s) removed'. However, with some of my examples I get the message: 'Warning: Duplicate data points have been detected. Some data points do not map to vertices in the triangulation. To avoid this behavior, call UNIQUE on the data points prior to calling DELAUNAY.' Now what I would like to know is whether this problem in the geometry is fixed by Mesh2d? I include a simple example of the sort of problem I'm interested in.
PS. Mesh2d has proved invaluable - Thank you!
p1 = [0 0; ...
3 0; ...
3 3; ...
0 3];
p2 = [0.5 1.5; ...
1.5 1.5; ...
1.5 2.5; ...
1.5 1.5; ...
0.5 1.5];
n1 = size(p1,1);
n2 = size(p2,1);
c1 = [(1:n1-1)', (2:n1)'; n1, 1];
c2 = [(1:n2-1)', (2:n2)'; n2, 1];
node = [p1; p2];
cnect = [c1;c2+n1];
hdata.hmax = 1;
[p,t] = mesh2d(node,cnect,hdata,[]);

Richard Clarke

Great routine, although I was having some issues removing redundant nodes until I changed the following in line 66 of changegeometry.m

del = nNode-i(end);


del = nNode-length(i);


This is a powerful and useful tool. It is well written and documented.

Since Mathworks abandoned tsearch, simply calling tsearchn instead works for me. Moving to tsearchn requires two changes that I am aware of:

mytesearch.m Line 68:
i(j)=tsearchn([x y],t,[xi(j) yi(j)]);

meshfaces.m Line 199:
i = tsearchn(ph,th,p);

I do not agree with the solution proposed by Chen and Barnhart. Delaunay triangulations are not unique. Their approach effectively generates a separate triangulation and searches over it. Mapping such results into the original Mesh2D triangulation is ill-advised.

Elias Pirayesh

There are some useful examples on meshing a 3D surface on:


How can I use it on 3D surface?
Is there some algorithm to create unstructured triangular meshes for 3D geometry?

William Barnhart

One more fix. In quadtree.m, near line 528 (right before %Undo rotation, p=rotate(p,-theta)), add:

% remove duplicate nodes
for i=1:length(goodid)

So that's three places to fix with the comments below:
meshfaces.m, line 203
mytsearch.m, line 69
quadtree.m, line 528 (or thereabouts)

William Barnhart


In my tsearch, replace:
i(j) = tsearch(x,y,t,xi(j),yi(j));

for k=1:size(t,1)

Then, no need to worry about delaunay triangulations

William Barnhart

Tsearch fix courtesy of Rowena Lohman.
tsearch.m needs to be replaced in two locations. First:

in meshfaces.m, line 203 replace:


for j=1:size(th,1)

In mytsearch.m line 69, replace:
i(j) = tsearch(x,y,t,xi(j),yi(j));


t = DelaunayTri([x y]);
i(j)= pointLocation(t,[xi(j) yi(j)]);

This uses DelaunayTri which is being fazed out, but delaunayTriangulation can be equally used

Thomas Benson

Hi Darren, thanks for this submission. Is there a version of Mesh2d that works with V2012b ? (TSEARCH.m no longer works and requires the use of DelaunayTri.m). Cheers.


I too need an update to replace tsearch function. The suggested replacement is not appropriate for me as performing 'DelaunayTri' within your routine can alter the triangulation depending on initial inputs i.e. I have specified internal boundaries, DelaunayTri does not respect these boundaries when implemented in the suggested way.

Is a modified/updated version in consideration?


Song Chen

Find the following line:

p = node;
e = edge;
i = tsearch(ph(:,1),ph(:,2),th,p(:,1),p(:,2));
Change it to

p = node;
e = edge;
dt = DelaunayTri(ph);
i = pointLocation(dt,p);

These two codes should be equivalent.

Jody Klymak

tsearch is gone as of 2012a. Any hope of a modification to use DelaunyTri?


Richard Crozier

I think Maxim is right:

>> A = triarea([0,0; 1,0; 0.5,1],[1,2,3])

A =


I think the area of that triangle should clearly be 0.5.


I think, there are a bug in the function "triarea".
I miss a factor 0.5 in row 14.



Thank you very much for this package, it is exactly what I'm looking for. I do have one question/comment. In Matlab 2011a, I get a warning that tsearch is going to be retired and to use delaunayTri/pointLocation isntead. I subbed in this set of commands (seemingly appropriately), and I cannot get the code to work correctly. Could you possibly advise on this? I'm going to use the code as is for the time being and try to implement and update. I'm not sure if you would be willing to use this update as a newer revision or are currently working on this problem.

Thank you,

Darren Engwirda

Hi Philippe, you may want to have a look at the utility "connectivity" routine. Amongst other things, it will identify the boundary nodes in a mesh.

Philippe N/A

Hi and ty for the great toolbox.

I'm using this to make a mesh that I want to use in another modeling tool. This other tool requires me to give nodes that are located on the edge of the mesh figure.

So, once the mesh is generated, is there any way to retrive nodes that have been generated on the contour of the figure?


Jose Luis Prego

Hi, thank you very much for your work. It looks impressive. However allow me to add a small suggestion. It would Very Nice if the Toolbox comes with a small info-note or Manual about the functions and how to start for a beginer. Appart from this you´ve made a Great Work!
Thank you again

Brian Faulkner

Extremely helpful, this is a great library. Well documented and simple to use. Bravo sir, bravo.


Thanks so much for your file upload. This was invaluable to me

Philippe N/A

Hi there.
First of all, thank you for that excellent toolbox.
I was wondering if it was possible to specify some point inside a geometric figure. For example, in the Lake Superior demo, I would like to insert a point in a specific location in the middle of the lake so the meshing is finer around that point. However, I can’t find how to do this. I bet I have to construct a new point (let’s say p8[x,y]) and connect it with existing points. But I get lost in that process.

Any help would be greatly appreciated.

With regards,


I love your code, but I've recently begun some efforts to see if I can't speed up the runtime a bit.

Is there a particular reason why "mydelaunayn.m" uses the "delaunayn" function instead of the "delaunay" function? As far as I can tell on my machine (with Matlab 7.10), "delaunay" is around four times faster than "delaunayn" and the code seems to function just fine when I make the replacement.

Marios Karaoulis

Why the following fails with meshfaces. I can run it with mesh2d

node = [0.0, 0.0; 1.0,0.0; 1.0,1.0; 0.0,1.0; 2.0,0.0; 2.0,1.0; 3.0,0.0; 3.0,1.0];
edge = [1,2; 2,3; 3,4; 4,1; 2,5; 5,6; 6,3; 5,7; 7,8; 8,6];
faces{1} = [1,2,3,4];
faces{2} = [5,6,7,2];
faces{3} = [8,9,10,6];
hdata.edgeh=[1,0.05 ;7,1];

running it with meshfaces

??? Index exceeds matrix dimensions.

Error in ==> meshpoly>getedges at 283
bnd = e(~idx,:);
% Boundary edges

Error in ==> meshpoly at 88
e = getedges(t,size(p,1));
% Unique edges

Error in ==> meshfaces at 147
[pnew,tnew] =

Johannes Korsawe

Very nice tool! Thank you a lot for sharing this.

Best regards, Johannes

Very minor irritation:
In line 127 of meshfaces.m, the variable wbar is addressed but never defined.

Canh Le

This is what I was looking for. Many thanks!!



Thank you for this great work. Really, really helpful!

I need to ask a question: When using meshfaces, is it possible to use a different 'hfun' for each face?


Robert Moucha

Many thanks,

Any plans to bypass MATLAB's tsearch?
In the 2010a release "Warning: TSEARCH will be removed in a future release. Use DelaunayTri/pointLocation instead."

johnus magnus

Just a great collection of functions, thankyou!



I would like to generate a mesh with certain nodes specified on the interior. When I try to do this I end up with areas of my domain that are not filled with elements. For example, I would like node[0.25 0.5] connected to node[0.75 0.5] and the mesh to fill the entire region from [0 0;1 0;1 1;0 1]. The actual meshes I would like to make have more complex node positions.

The following produces a hole in the mesh:

node=[0 0;1 0;1 1;0 1;0.25 0.5;0.75 0.5];

Marios Karaoulis

Marios Karaoulis

Do you have any thoughts going 3D?

Darren Engwirda

Author comment: v24 was posted to resolve a licence conflict. The Mathworks no longer allows code to be released under a GNU GPL, so this has been removed for v24.

There are some (minor) revisions to the code, based on user feedback. Specifically, v24 may be slightly slower than v23, but should generally produce higher quality meshes, especially for complex geometries.

Comments/feedback is always welcome -

Marios Karaoulis

Hi, thanks again for your software. What's new in this version ( i am using v23).

MATLAB Release Compatibility
Created with R2018b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!