Virtual 1D index for 2D coordinates
Understanding Virtual 1D index → 2D coordinates:
How does this formula work?
row = index / n
col = index % n
Mapping 1D to 2D
Imagine you have a bookshelf with 4 shelves (rows), and each shelf holds 3 books (columns).
Shelf 0: [Book0] [Book1] [Book2]
Shelf 1: [Book3] [Book4] [Book5]
Shelf 2: [Book6] [Book7] [Book8]
Shelf 3: [Book9] [Book10] [Book11]
If someone says "Give me book number 7", how do you find it?
Why Division (/
) for Row?
Division tells you "How many COMPLETE rows can I fill?"
Book index = 7
Books per row = 3
How many complete rows before book 7?
7 / 3 = 2 (integer division)
This means: book 7 is in row 2 (the 3rd row)
Why Modulo (%
) for Column?
Modulo tells you "What's the POSITION within the current row?"
Book index = 7
Books per row = 3
Position within the row?
7 % 3 = 1
This means: book 7 is at column 1 (the 2nd position in its row)