Skip to content

Instantly share code, notes, and snippets.

@OfficalOffical
Forked from amccaugh/line_supercover
Last active February 12, 2021 07:14
Show Gist options
  • Save OfficalOffical/39a4a3d9222fc2555dedc84159971da2 to your computer and use it in GitHub Desktop.
Save OfficalOffical/39a4a3d9222fc2555dedc84159971da2 to your computer and use it in GitHub Desktop.
import numpy as np
def bresenham(x0,y0,x1,y1):
c = []
dx = abs(x1-x0)
dy = abs(y1-y0)
x = x0
y = y0
ii = 0
n = dx + dy
err = dx - dy
x_inc = 1
y_inc = 1
max_length = (max(dx,dy)+1)*3
rr = np.zeros( max_length, dtype = np.intp )
cc = np.zeros( max_length, dtype = np.intp )
print()
if x1 > x0: x_inc = 1
else: x_inc = -1
if y1 > y0: y_inc = 1
else: y_inc = -1
dx = 2 * dx
dy = 2 * dy
while n > 0:
c.append([x,y])
rr[ii] = y
cc[ii] = x
ii = ii + 1
if (err > 0):
x += x_inc
err -= dy
elif (err < 0):
y += y_inc
err += dx
else: # If err == 0 the algorithm is on a corner
rr[ii] = y + y_inc
cc[ii] = x
rr[ii + 1] = y
cc[ii + 1] = x + x_inc
ii = ii + 2
x += x_inc
y += y_inc
err = err + dx - dy
n = n - 1
n = n - 1
c.append( [x, y] )
rr[ii] = y
cc[ii] = x
return c
@OfficalOffical
Copy link
Author

adjusted to python 3.9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment