import numpy as np
from matplotlib import pyplot as mpl

def HC(z1, z2, n, orientation):
    """ Return the nth Hilbert curves with endpoint z1 and z2 and oriented by orientation"""
    if n >0:
        lpoints=[z1] + [z1 + (i + orientation*1j)*(z2-z1)/2 for i in range(3)] + [z2]
        ori=[-1, +1, +1, -1]
        return np.concatenate([HC(lpoints[i], lpoints[i+1], n-1, ori[i]*orientation) for i in range(4)])
    else:
        return [z1 + (z2-z1)/2*(1 +orientation*1j)]

def draw_complex_list(l):
    """Draw a broken line in the complex plane through a list of complex numbers"""
    points = np.array([[z.real, z.imag] for z in l])
    mpl.plot( points[:,0], points[:,1], 'b')
    mpl.show()
    return(0)
        
def wrapper(n, z1=0, z2=1, orientation = +1):
    """call wrapper(n) with  0 <= n <= 8"""
    draw_complex_list(HC(z1,z2, n, orientation))
    return(0)

