简体   繁体   中英

Algorithm for a top left/bottom right grid?

I have an square grid from a GridLayout(4,4,0,0) for a JPanel, just say 4x4 for a small example

*-*-*-*
*-*-*-*
*-*-*-*
*-*-*-*

Say I want to loop through just the top left corner. For a case,

*-*
*

And for another case, the bottom right corner.

  *
*-*

I was thinking of a dynamic loop? For larger size boards.

Case: 1 (Top Left Corner Starting from, say (0,0))

int csize = (number of cols)/2+1;
for (int row = 0; row < (number of cols)/2; row++){
    csize--;
    for (int col = 0; col < csize; col++){
                ...stuff.....
    }
}

Case: 2 (Bottom Right Corner, Starting from say, (4,4))

int csize = (number of cols)/2;
for (int row = (number of cols) -1; row > (number of cols)/2; row--){
    csize++;
    for (int col = (number of cols) -1; col > csize; col--){
                        ...stuff...
    }
}

I kind of just did a initial size of cols, and decrease the nested loop for the column inside of rows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM