function P=cantorr2(x1,x2,y1,y2,k)
%function P=cantorr2(x1,x2,y1,y2,k)
%to implement: P=cantorr2(0,1,0,1,10); plot(P(:,1),P(:,2))
%k is number of (remaining) levels
x=x1:(x2-x1)/3:x2; %chop into thirds
ym=(y1+y2)/2;
P=[x(2) ym; x(3) ym; NaN NaN]; %endpoints middle line
if k>1
    k=k-1;
    P1=cantorr2(x(1),x(2),y1,ym,k);
    P2=cantorr2(x(3),x(4),ym,y2,k);
    P=[P1;P;P2]; 
    %P is the middle line, along with the lines on the left and right
end

