Advertisement
JohnnyTurbo

Flatten 2D Arrays

Sep 1st, 2020
1,604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.73 KB | None | 0 0
  1. private int CoordinatesToFlatIndex(Vector2Int cellIndex, int height)
  2.         {
  3.             return height * cellIndex.x + cellIndex.y;
  4.         }
  5.  
  6.         // If using width...
  7.         // return width * cellIndex.y + cellIndex.x;
  8.        
  9.         private Vector2Int FlatIndexToCoordinates(int flatIndex, int height)
  10.         {
  11.             Vector2Int coordinates = new Vector2Int
  12.             {
  13.                 x = flatIndex / height,
  14.                 y = flatIndex % height
  15.             };
  16.             return coordinates;
  17.         }
  18.        
  19.         // If using width...
  20.         /*
  21.         Vector2Int coordinates = new Vector2Int
  22.         {
  23.             x = flatIndex % width,
  24.             y = flatIndex / width
  25.         };
  26.         */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement