This project is read-only.

How to draw triangles properly

Jun 24, 2015 at 11:41 AM
Edited Jun 24, 2015 at 11:49 AM
Hey there,

could you show me how to draw the triangles without drawing double lines?
When I draw every single segment of the "mesh.Triangles()" there will be overlapping lines or not?

Thanks imn advance!
Jun 24, 2015 at 1:31 PM
Since every edge (except for the boundary) is shared by two triangles, lines will be drawn twice. But there's a simple way to prevent this:
foreach (var t in mesh.Triangles)
{
    // Check the 3 neighbor triangles / edges.
    for (int i = 0; i < 3; i++)
    {
        var n = t.GetNeighbor(i);

        // Consider each edge only once.
        if (n == null || t.ID < n.ID)
        {
            var v0 = t.GetVertex(i);
            var v1 = t.GetVertex((i + 1) % 3);

            DrawLine(v0, v1);
        }
    }
}
Or just do:
// Make sure vertices are numbered from 0 to n-1.
mesh.Renumber();

var vertices = mesh.Vertices.ToArray();

foreach (var e in mesh.Edges)
{
    var v0 = vertices[e.P0];
    var v1 = vertices[e.P1];

    DrawLine(v0, v1);
}
Marked as answer by GeoGecco on 6/24/2015 at 5:35 AM
Jun 24, 2015 at 1:39 PM
hey thanks for the quick answer.
The first solution doesn't work properly but the second does!
Jun 24, 2015 at 1:45 PM
Right, have a look at https://triangle.codeplex.com/wikipage?title=Topology ... the vertex indices should be
            var v0 = t.GetVertex((i + 1) % 3);
            var v1 = t.GetVertex((i + 2) % 3);
not tested, hope I got it right this time!
Jun 24, 2015 at 1:52 PM
yep now the other solution is working as well =)
thanks again!