Compute Distance Between Two Cells in a Matrix

239 Views Asked by At

I am trying to compute the distance between two cells in a matrix. Here is a drawing of what I am working on:

enter image description here

Please note the numbers are only here as a visual help, they do not mean anything by themselves.

If we virtually rotate the matrix 45° to the right by the bottom (hence having 46 at the bottom and 5 on top), I'd need to compute the distance between the left side to the right side. E.g: I'd need to mathematically figure out the number of cells between 1 and 41, 10 and 50, 19 and 49, 2 and 32, 3 and 23, and so on. This is kind of easy for squared matrices, but here the matrix can be any dimension with a side larger than the other.

How can I mathematically retrieve the distance between one side to the other by just having the dimensions of the said matrix ?

Please note I am not a mathematics person at all, be easy on me :)

1

There are 1 best solutions below

1
On BEST ANSWER

I understood your problem as follows. We have a “matrix” constituted by rhombic cells such that there are $m$ cells along a vertical boundary side and $n$ cells along a horizontal boundary side. For instance, at the picture we have $m=5$ and $n=6$. Then the rotated “matrix” has $m+n-1$ cells on each vertical boundary side. By induction with respect to $|m-n|$ we can show that the consecutive $m+n-1$ distances between the left and right sides of the rotated matrix are

$1, 3, 5, \dots, 2\min\{m,n\}-1,\dots, 2\min\{m,n\}-1, 2\min\{m,n\}-3,\dots, 5,3,1.$

That is $i$-th distance equals

$$\begin{cases} 2i-1,\mbox{ if } 1\le i\le\min\{m,n\},\\ 2\min\{m,n\}-1,\mbox{ if } \min\{m,n\}\le i\le m+n-\min\{m,n\},\\ 2(m+n-i)-1, \mbox{ if } 1\le i\le m+n-1 \end{cases}$$

For instance, at the picture we have the following distances:

5–5  1 
4–14 3
3–23 5
2–32 7
1–41 9
10–50 9
19–49 7   
28–48 5
37–47 3
46-46 1