Mapping elements in 2D upper triangle and lower triangle to linear structure

To go the opposite direction which is what I needed:

void printxy(int index)  
{  
    int y = (int)((-1+sqrt(8*index+1))/2);  
    int x = index - y*(y+1)/2;  
}

Leave a Comment