# how can i draw a specific space-filling curve mathematically? almost achieve, need a bug fixing.

here is a figure (fig_1) illustrating a space-filling spiral curve constitutes a triangle. i am trying to draw this mathematically.

with @David K's help, we got this figure (fig_2) by using this formula

\begin{align} &(-r, r) \\ &(r,-r) \\ &(-r,-r) \\ &(-2r,2r) \\ &(2r,-2r) \\ &(-2r,-2r) \\ &(-3r,3r) \\ &(3r,-3r) \\ &(-3r,-3r) \\ &(-4r,4r) \\ &(4r,-4r) \\ &(-4r,-4r) \\ &\ldots \end{align}

here is a piece of python code to implement the formula

import numpy as np
x = np.array([])
y = np.array([])
for r in range(1,9):
x = np.append(x, np.array([-r,r,-r]))
y = np.append(y, np.array([r,-r,-r]))
plt.plot(x,y)


fig_2 is very close to fig_1 though, there seems to be a bug. how to improve this formula?

thanks to @David K

doubling all the positive coordinates might work better

import numpy as np
x = np.array([])
y = np.array([])
for r in range(9):
x = np.append(x, np.array([-r,2*r,-r]))
y = np.append(y, np.array([2*r,-r,-r]))
plt.plot(x,y)


outputs

enter image description here