Skip to main content
1 of 3

How different can the number of pixels in a straight line be to its real length?

I can program, but have only recently started to learn graphics and the related math, so sorry for any ignorance here.

Imagine a top-down 2D game with a programmable robot that drives around and uses a laser range finder to get the distance to the nearest wall.

I'm using DDA to draw a line from the robot (in the direction it's facing), then checking each point on the line till it hits something (or runs out of range).

Whenever the line hits something, the function just returns the number of iterations it took to get to that point (the number of pixels that were checked) as the distance. This avoids calculating the length of the line properly, but how sketchy is this approach?

If the line is horizontal or vertical, then there's no discrepancy, but how greatly does the number of pixels in a line differ from its real length in the worst case?

Notes:

  • The length of the line is never just a few pixels, say always at least 50px.
  • Longer lines obviously have larger discrepancies, but the question is really about relative differences.