0
$\begingroup$

I am creating a chess game and programming the movement for the piece that moves diagonal, but I have a problem, I cannot figure out how to find how many squares the piece is moving through, relative to the two points eg: 6,1 and 3,4 goes through 4 tiles, and I need a formula that finds this distance with any diagonal line, so I can then use that number for how many times the pathfinding must loop, I have looked at other equations, bot nothing seems to work with my particualar problem.

  • 0
    See for example this question: http://math.stackexchange.com/questions/1121541/number-of-unit-squares-that-meet-a-given-diagonal-line-segment-in-more-than-one2017-02-06

1 Answers 1

0

6,1 and 3,4 goes through 4 tiles

That's because $\lvert 6-3\rvert = \lvert 1-4\rvert = 4-1$. In other words, the absolute difference in $x$ coordinate equals the absolute difference in $y$ coordinate as it is a diagonal movement, and both of these equal the number of squares minus one. The minus one is because you start counting at $1$ for distance $0$.