function dragon(level, P1, P2, P3)
global phi

if (level==1)
  %draw line P1->P2
  line([P1(1) P2(1)],[P1(2) P2(2)]);
  %draw line P2->P3
  line([P2(1) P3(1)],[P2(2) P3(2)]);
  hold on
else
    A = [cos(phi) +sin(phi); -sin(phi) cos(phi)];
    
    P12 = P1 + A*(P2-P1)/sqrt(2); %generate point at right anglein between P1 and P2
    dragoncurve(level-1, P1, P12, P2);

    P23 = P3 - A*(P3-P2)/sqrt(2); %generate point at right anglein between P2 and P3
    dragoncurve(level-1, P2, P23, P3);
end