> ## Content Index
> Fetch the complete content index at: https://ashwiniag.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Virtual 1D index for 2D coordinates
- URL: https://ashwiniag.com/virtual-1d-index-for-2d-coordinates/
- Published: 2025-09-29T10:05:00.000Z
- Updated: 2025-09-29T10:05:00.000Z
- Author: Ashwini Gaddagi

## 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)
```