Posted By

nefd on 11/04/09


Tagged

python line algorithm bresenham cartesian


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

nefd


Bresenham's line algorithm


 / Published in: Python
 

  1. def bresenham_line((x,y),(x2,y2)):
  2. """Brensenham line algorithm"""
  3. steep = 0
  4. coords = []
  5. dx = abs(x2 - x)
  6. if (x2 - x) > 0: sx = 1
  7. else: sx = -1
  8. dy = abs(y2 - y)
  9. if (y2 - y) > 0: sy = 1
  10. else: sy = -1
  11. if dy > dx:
  12. steep = 1
  13. x,y = y,x
  14. dx,dy = dy,dx
  15. sx,sy = sy,sx
  16. d = (2 * dy) - dx
  17. for i in range(0,dx):
  18. if steep: coords.append((y,x))
  19. else: coords.append((x,y))
  20. while d >= 0:
  21. y = y + sy
  22. d = d - (2 * dx)
  23. x = x + sx
  24. d = d + (2 * dy)
  25. coords.append((x2,y2))
  26. return coords

Report this snippet  

You need to login to post a comment.