- Home /

# Triangulation Problems

I have a collection of points. The vertex order looks like this:

```
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
```

The points are lay out like a plane, and i want the final result to look like a plane. I tried using the Triangulation script on the Unify Community Wiki, but it did not work, due to having points "inside" the outer points.

Currently, my code for triangulation is this:

```
Vector3[] vertices = new Vector3[xMax * yMax];
int[] triangles = new int[vertices.Length * 3];
for (int x = 0; x < xMax; x++) {
for (int y = 0; y < yMax; y++) {
int v = x + (y * xMax);
// Assign the vertex position (this isnt the problem)
vertices[v] = vertexPos;
// Assign triangles (the thing that doesn't work)
// The if then statements are to make sure the indicies dont go out of bounds
if (t < triangles.Length - 5) {
if (v < vertices.Length - xMax) {
triangles[t++] = v;
triangles[t++] = v + 1;
triangles[t++] = v + xMax;
triangles[t++] = v + 1;
triangles[t++] = v + xMax;
triangles[t++] = v + xMax + 1;
}
}
}
}
```

All the things I've tried have not resulted in a nice smooth flat plane like i would like it to be.

**Answer** by Bunny83
·
Oct 18, 2015 at 10:26 PM

You have to multiply y by xMax, not yMax

```
int v = x + (y * xMax);
```

Further more when creating the triangle indices you should reduce the limits of your for loops by 1

```
for (int x = 0; x < xMax-1; x++) {
for (int y = 0; y < yMax-1; y++) {
```

I just checked your 2 triangles and your second triangle is facing the wrong way. It need to be clockwise. The first one is clockwise, the second one is counter clockwise. So the second one is facing backwards.

The size of the triangles array is also too small. There are two rows with each 4 quads:

```
0 1 2 3 4
Q1 Q2 Q3 Q4
5 6 7 8 9
Q5 Q6 Q7 Q8
10 11 12 13 14
```

That means since each quad requires two triangles and each triangle 3 indices you need

```
int[] triangles = new int[ (xMax-1) * (yMax-1) * 2 * 3 ];
```

So finally it should look something like:

```
Vector3[] vertices = new Vector3[xMax * yMax];
int[] triangles = new int[ (xMax-1) * (yMax-1) * 6 ];
// vertices
for (int y = 0; y < yMax; y++)
{
for (int x = 0; x < xMax; x++)
{
int v = x + (y * xMax);
vertices[v] = new Vector3(x,y,0);
}
}
// triangles
int t = 0;
for (int y = 0; y < yMax-1; y++)
{
for (int x = 0; x < xMax-1; x++)
{
int v = x + (y * xMax);
triangles[t++] = v;
triangles[t++] = v + 1;
triangles[t++] = v + xMax;
triangles[t++] = v + 1;
triangles[t++] = v + xMax + 1;
triangles[t++] = v + xMax;
}
}
```

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Problems with UV on created mesh 1 Answer

Switching meshes 1 Answer

Mesh information explained. 1 Answer

DrawMesh slowing FPS 0 Answers

3D Meshes: how to minimize error during reduction of order? 0 Answers