Bresenham algorithm
if (deltaCol >deltaRow)
{
fraction = deltaRow *2-deltaCol;
while (nextCol != endCol)
{
if (fraction >=0)
{
nextRow =nextRow +stepRow;
fraction =fraction -deltaCol;
}
nextCol=nextCol+stepCol;
fraction=fraction +deltaRow;
pathRow[currentStep]=nextRow;
pathCol[currentStep]=nextCol;
currentStep++;
}
}
else
{
fraction =deltaCol *2-deltaRow;
while (nextRow !=endRow)
{
if (fraction >=0)
{
nextCol=nextCol+stepCol;
fraction=fraction -deltaRow;
}
nextRow =nextRow +stepRow;
fraction=fraction +deltaCol;
pathRow[currentStep]=nextRow;
pathCol[currentStep]=nextCol;
currentStep++;
}
}
}
The initial if conditional uses the values in deltaCol and deltaRow to determine which axis is the longest. The first block of code after the if statement will be executed if the column axis is the longest. The else part will be executed if the row axis is the longest. The algorithm will then traverse the longest axis, calculating each point of the line along the way. Figure 1 shows an example of the path the troll would follow using the Bresenham line-of-sight algorithm. In this case, the row axis is the longest, so the else part of the main if conditional would be executed.
Figure 1 . Bresenham tile-based chase

Figure 1 shows the troll's path, but of course this function doesn't actually draw the path. Instead of drawing the line points, this function stores each row and column coordinate in the pathRow and pathCol arrays. These stored values are then used by an outside function to guide the troll along a path that leads to the player.