use distance instead of pre-computing diagonals