Home > front end >  Get Index by X and Y
Get Index by X and Y

Time:03-26

There's a way to get X and Y based on Index like

x = index % columns;
y = index / columns;

How can I reverse this and get Index based on X and Y?

CodePudding user response:

Assuming we're dealing with integers here, if you have the following code:

x = index % columns; // get the remainder of dividing index by columns 
y = index / columns; // get the result of dividing index by columnsn

Then the reverse would be:

index = y * columns   x;
  • Related