Hi, I have an array that holds a grid using blueprints, so no 2d arrays. My grid currently is 24 by 24. I want to be able to get every grid cell within 2 selections but I have no idea how to do the math involved. So for example if I click grid cell 0 and 25 which are directly diagonal from each other, it would make a squares worth of selection, I’ll be able to know that 0, 1, 24, 25 are within the selection. Please help
Modulo is your friend
When you divide a cell index by the length of a row (24), you get an integral portion and a remainder. Integer division gives you the integral portion, Modulo gives you the remainder.
For example usually when you divide 27 by 24 you get 1,125. But with integral division you only get 1, and you can get the remainder with modulo operator (27 % 24 = 3).
This allows you to determine the row and column of a cell according to its index.
Row number : index / 24
(integer division)
Column number : index % 24
(modulo)
Now you can determine the row and column of any cell based on its index. The rest should be easy. Any cell (index) is selected if its row is between the two selected rows AND if its column is between the two selected columns.
Thank you! “figuring out the rest” Happened to be a lot harder than I thought lol Especially since I then had to flip my selection of cell if dragging from a lower cell to a higher cell. But it all works now, thank you again!
You can also check if 2 indexes are diagonal using :
(if a+ (n+1)(d//n-a//n) = d) or (if a+ (n-1)(d//n-a//n) = d) here n being 24, a and d being the 2 indexes.