% BRACH.M
%
% Plots of the brachistochrones 
%
% Coded by Yonggon Lee      
% December, 7, 2001


n = 50;
button = 3;         % default button - Initialize Plot

while(button ~= 2 & button ~= 32)      % continue if not 'MIDDLE CLICK' or 'SPACE'
 
 % Initialize plot   
 if button == 3
    clf
    axis([0 10 -10 0]); hold on;
    xlabel('{\it x}','FontSize',12);
    ylabel('{\it y}','FontSize',12);
    grid on;
    set(gca,'Yticklabel',10:-1:0);
    plot(0,0,'.','Markersize',14);
    title('\beta\rho\alpha\chi\iota\sigma\tau\o\zeta\chi\rho\o\nu\o\zeta',...
        'FontSize',16);
    text(.5,-8.5,'Left click to pick point ','FontSize',10,'FontWeight','bold'); 
    text(.5,-9,'Right click to clear plots','FontSize',10,'FontWeight','bold');
    text(.5,-9.5,'Middle or SPACE to end','FontSize',10,'FontWeight','bold')
 end;

 % Find the radius and ratation angle for chosen (a,b)
 if button == 1;                 
     
     eq1 = ['r*theta - r*sin(theta) - ',num2str(a),' = 0'];
     eq2 = ['r - r*cos(theta) - ',num2str(-b),' = 0'];
     [r,theta]=solve(eq1,eq2);          
     R=double(r);           % convert symbolic to double
     TH=double(theta);      % convert symbolic to double
     
     % Draw brachistochrone
     angle=linspace(0,TH,n);
     X = R*(angle-sin(angle));
     Y = -R*(1-cos(angle));
     plot(X,Y,'Linewidth',1);
     plot(X(n),Y(n),'.','MarkerSize',14);
     time = num2str(sqrt(R/9.81)*TH);
     text(X(n)-.7,Y(n)-.3,['(',num2str(a),',',num2str(-b),')'],'FontWeight','bold','FontSize',8);
     text(X(n)+.2,Y(n)+.1,['T= ',time],'FontWeight','bold','Color','Red');
 end;
 
 [a,b,button]=ginput(1);     % input final point from mouse click

end;