# Diagonals

Home * Chess * Diagonals

Diagonals go from south-west to north-east on a chess board, the main diagonal is a1/h8. There are 15 diagonals, with line-length from 1 to 8. A Diagonal is monochrome, all their squares are either white or black.

# Main Diagonal

 abcdefgh 87654321                                                                                                       •      •      •      •      •      •      •      • 87654321 abcdefgh

# Square Mapping Notes

A 90 degree rotation of the Chessboard, as well as flipping vertically (reversed ranks) or (exclusive) mirroring horizontally (reversed files), change the roles of diagonals and anti-diagonals. However, we define the main diagonal on the chess board from a1/h8 and the main anti-diagonal from h1\a8. Whether the square difference of neighbored squares on a diagonal or anti-diagonal is either 7 or 9, depends on the square mapping. We further rely on little-endian rank-file mapping.

# Square Difference

Within a 0..63 square index range and the mentioned square mapping (a1 = 0), the difference of two neighbored squares (if any) on a diagonal is nine.

# Enumeration

If we follow a diagonal from south-west (a1) to north-east (h8) step by step, we increment the rank, and increment the file, which yields in same difference. Thus, subtracting rank from file indices (or vice versa) enumerates all Diagonals, unfortunately with negative numbers involved, which can easily adjusted by adding seven.

• The main diagonal a1/h8 with index 0 and length 8.
• The diagonals a8/ and h1/ with length 1, are index 7 and -7 respectively.
• All even indices are the diagonals with dark squares

rank - file

r/f 0 1 2 3 4 5 6 7
7 7 6 5 4 3 2 1 0
6 6 5 4 3 2 1 0 -1
5 5 4 3 2 1 0 -1 -2
4 4 3 2 1 0 -1 -2 -3
3 3 2 1 0 -1 -2 -3 -4
2 2 1 0 -1 -2 -3 -4 -5
1 1 0 -1 -2 -3 -4 -5 -6
0 0 -1 -2 -3 -4 -5 -6 -7

7 + rank - file

r/f 0 1 2 3 4 5 6 7
7 14 13 12 11 10 9 8 7
6 13 12 11 10 9 8 7 6
5 12 11 10 9 8 7 6 5
4 11 10 9 8 7 6 5 4
3 10 9 8 7 6 5 4 3
2 9 8 7 6 5 4 3 2
1 8 7 6 5 4 3 2 1
0 7 6 5 4 3 2 1 0

# Alternative Enumeration

Some alternative enumeration of diagonals to keep the main-diagonal index 0, by anding the difference with 15. This yields in a 0..15 range with 8 as gap or Nexus in the center of the range:

(rank - file) & 15

r/f 0 1 2 3 4 5 6 7
7 7 6 5 4 3 2 1 0
6 6 5 4 3 2 1 0 15
5 5 4 3 2 1 0 15 14
4 4 3 2 1 0 15 14 13
3 3 2 1 0 15 14 13 12
2 2 1 0 15 14 13 12 11
1 1 0 15 14 13 12 11 10
0 0 15 14 13 12 11 10 9

# Two Squares on a Diagonal

Two Squares are on the same Diagonal, if their file distance equals the rank distance

```bool squaresOnSameDiagonal(int sq1, int sq2) {
return ((sq2 - sq1) & 7) == ((sq2>>3) - (sq1>>3));
}
```

Alternatively, one may test whether the square difference is divisible by 9 [4]

```bool squaresOnSameDiagonal(int sq1, int sq2) {
return ((sq2 - sq1) % 9) == 0;
}
```