Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array As Grid, How to Move Diagonally?

Tags:

java

arrays

math

I have an array of size n^2 representing a square grid of size nxn.

I want to get from any square (0 to (n^2)-1) to the top-right most square you can get to moving diagonally in a straight line.

I already figured out that to get to the top-left most square is to take the current square x and the top-left most square is x%(n+1).

Does anyone know a similar method for top-right most square? Thanks!

like image 272
Kyle V. Avatar asked Nov 14 '22 16:11

Kyle V.


1 Answers

If I understood at all what you're trying to achieve, all you need to do is connect the two points with a straight line and using the line formulas calculate the coordinates of the cells that it goes through. You may use for this the Bresenham's line drawing algorithm.

like image 112
Alexey Frunze Avatar answered Dec 21 '22 06:12

Alexey Frunze