Skip to content

Instantly share code, notes, and snippets.

@amccaugh
Created September 5, 2016 03:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amccaugh/f459e45650915351bb65070141a28e3f to your computer and use it in GitHub Desktop.
Save amccaugh/f459e45650915351bb65070141a28e3f to your computer and use it in GitHub Desktop.
def line_supercover(Py_ssize_t y0, Py_ssize_t x0, Py_ssize_t y1, Py_ssize_t x1):
cdef int dx = abs(x1-x0)
cdef int dy = abs(y1-y0)
cdef int x = x0
cdef int y = y0
cdef int ii = 0
cdef int n = dx + dy
cdef int err = dx - dy
cdef int x_inc = 1
cdef int y_inc = 1
cdef int max_length = (max(dx,dy)+1)*3
cdef Py_ssize_t[::1] rr = np.zeros(max_length, dtype=np.intp)
cdef Py_ssize_t[::1] cc = np.zeros(max_length, dtype=np.intp)
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:
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
rr[ii] = y
cc[ii] = x
return np.asarray(rr[0:ii+1]), np.asarray(cc[0:ii+1])
@paulbrodersen
Copy link

Thanks. Works beautifully.

@ogencoglu
Copy link

What exactly are Py_ssize_t here? Do we import something from Cython?

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