% Generates Figure 10.2 (interactive mode)

a = sqrt(3);
b = 1;

while 1
figure(1)
hold off
h=1e-2;
plot([-a a],[b,b],'k-','LineWidth',2);
hold on;
plot([-a a],[-b,-b],'k-','LineWidth',2);
plot([a,a],[-b b],'k-','LineWidth',2);
plot([-a,-a],[-b b],'k-','LineWidth',2);
axis equal;
axis tight;
axis([-a-h,a+h,-b-h,b+h]);
axis off;
plot(0,0,'k.','MarkerSize',16);

theta = linspace(0,2*pi,100);
plot(b*cos(theta),b*sin(theta),'k:','LineWidth',1);
S = rand('state'); % save goodseed S;
cl = 'b-g-c-';
cd = 'b.g.c.';
cdd = 'b:g:c:';
for k=1:3
    x=0; y=0; flag = 0;
    while abs(x)<a-h & abs(y)<b-h
        r = min(min(a-x,a+x),min(b-y,b+y));
        if flag
            plot(x+r*cos(theta),y+r*sin(theta),cl(2*k-1:2*k),'LineWidth',2);
        end
        flag = 1;
        [xi,yi]=ginput(1);
        phi = atan2(yi-y,xi-x);     % interactive mode
 %      phi = 2*pi*rand(1);         % random mode
        xn = x + r*cos(phi);
        yn = y + r*sin(phi);
        plot([x xn],[y yn],cdd(2*k-1:2*k),'LineWidth',2);
        x = xn;
        y = yn;
        plot(x,y,cd(2*k-1:2*k),'MarkerSize',16);
    end
    plot(x,y,'r.','MarkerSize',16);
end
pause;
end