Best way to draw a grid mesh like chess field

Hey there,

I am bit stuck about how I draw a simple grid like a chess field best way.
My approach so far is the following, but somehow the indices are wrongly connected.

int height = 500;
    int width = 500;
     int distance = 10;
    for (int y = 0; y < height; y = y + distance){
      for (int x = 0; x<width; x = x + distance){
        mesh.addVertex(ofPoint(x,y,0)); // make a new vertex
      }
    }
 

    int heightIndex = height / distance-1;
    int widthIndex = width / distance-1;

    for (int y = 0; y<heightIndex; y++){
      for (int x=0; x<widthIndex; x++){
        mesh.addIndex(x+y*widthIndex);      
        mesh.addIndex((x+1)+y*widthIndex);    
        mesh.addIndex(x+(y+1)*widthIndex);     
     
        mesh.addIndex((x+1)+y*widthIndex);     
        mesh.addIndex((x+1)+(y+1)*widthIndex);  
        mesh.addIndex(x+(y+1)*widthIndex);   
      }
    }

Is there a better way for that? any ideas what exactly is going wrong. I can not wrap my head around that.

solved i tlike this. only a small * was wrong :frowning:

int height = 500;
    int width = 500;
     int distance = 125;
    for (int y = 0; y < height; y = y + distance){
      for (int x = 0; x<width; x = x + distance){
          //mesh.addColor(ofFloatColor(ofRandomf(),ofRandomf(),ofRandomf()));
        mesh.addVertex(ofPoint(x,y,0)); // make a new vertex
      }
    }
    // now it's important to make sure that each vertex is correctly connected with the
    // other vertices around it. This is done using indices, which you can set up like so: 
    
    int heightIndex = height / distance-1;
    int widthIndex = width / distance-1;
    int widthIndexPlus = widthIndex+1;
    for (int y2 = 0; y2<heightIndex; y2++){
      for (int x2=0; x2<widthIndex; x2++){

       
        mesh.addIndex(x2+y2*widthIndexPlus);       // 0
        mesh.addIndex((x2+1)+y2*widthIndexPlus);     // 1
        mesh.addIndex(x2+(y2+1)*widthIndexPlus);     // 10
     
         
        mesh.addIndex((x2+1)+y2*widthIndexPlus);     // 1
        mesh.addIndex((x2+1)+(y2+1)*widthIndexPlus);   // 11
        mesh.addIndex(x2+(y2+1)*widthIndexPlus);     // 10

      }
    }