⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 billiard.m

📁 一个有趣的桌面撞球模拟,可录制avi,输出坐标参数及图象
💻 M
字号:
% -------------------------------------------------------
% -------------   (  Billiard Ball  )   -----------------
% -------------                         -----------------
% -------------   By: Fahad Al Mahmood  -----------------
% -------------         5/30/2003       -----------------
% -------------------------------------------------------
% BILLIARD(MANUAL INPUT)
%  This program consists of 4 m-files:
%       i)  (billiard.m):           Which is the main program file.
%      ii)  (billiard_input.m):     Function asking for and saving the user input
%     iii)  (time_to_impact.m):     Function calculating the time the ball
%                                   takes to hit the side of the table
%      iv)  (theta_impact.m):       Function calculating the path angle
%                                   after the ball hits the side of the table.
%
%  This Program Performs the following:
%       (1) Animates the movement of a Billiard Ball on a table that has a
%       surface friction (mu) and coefficient of restitution (e) for the
%       sides and saves it as an AVI file.
%       (2) Outputs series of rows and columns that displays the stream data
%           of (Time),(X),(Y),(V), and (th) to screen.
%       (3) Outputs the initial X-Y coordinates along with the final
%       coordinated when the speed of the ball reaches zero.
%       (4) Plots 4 figures that shows:
%           a- (X vs. Y).
%           b- (Time vs. X).
%           c- (Time vs. Y).
%           d- (Time vs. Velocity).
%       (5) Saves the Above Plots as a (JPEG) file.
%       (6) Outputs series of rows and columns that displays the stream data
%           of (Time),(X),(Y),(V), and (th) to a file.
%
%       Follow the instructions carefully when you run the program
%       Start the program by typing:
%           BILLIARD(1)     (For Screen Input)
%           BILLIARD(0)     (For Input saved on )
%           BILLIARD(-1)    (Demo run for default input)
%
%       **  Copy and Paste the following line and make the changes necessary before copying
%           and pasting the (manual_input=0,billiard) command:
%           clear;clc;width=100;height=100;x0=30;y0=40;v0=100;th=30;mu=0.88;e=0.7;t_int=0.1;FILE='data'
%
%           where:
%                 width:      width of billiard table.
%                 height:     height of billiard table.
%                 x0:         initial x-position of the ball.
%                 y0:         initial y-position of the ball.
%                 v0:         initial velocity of the ball.
%                 th:         ball direction (degrees).
%                 mu:         table surface friction.
%                 e:          table sides coefficient of restitution.
%                 t_int:      program time interval for calculations.
%                 FILE:       output file name (with no extension).
            

function billiard(manual_input)

if manual_input==1
    clear
    clc
    disp('-------------------------------------------------------')
    disp('-------------   (  Billiard Ball  )   -----------------')
    disp('-------------                         -----------------')
    disp('-------------   By: Fahad Al Mahmood  -----------------')
    disp('-------------         5/30/2003       -----------------')
    disp('-------------------------------------------------------')  
    [width,height,x0,y0,v0,th,mu,e,t_int,FILE]=billiard_input      % Calling Sub Program (billiard_input.m)
elseif manual_input==-1
    clear;clc;width=100;height=100;x0=30;y0=40;v0=100;th=30;mu=0.1;e=0.7;t_int=0.1;FILE='data'
elseif manual_input==0
    clc;
else
    error('Input argument has to be -1,0, or 1 !!!')
end

% Defining Constants & Initial Values
g=32.174;                              
t0=0;i=0;j=0;

% Converting Negative Theta to a Positive Theta
if th<0;th=th+360;end

% Saving Initial coordinates, velocity and angle of the Ball 
Xin=x0;
Yin=y0;
Vin=v0
thin=th;

% Defining Output Files Names
DATA=sprintf('%s.txt',FILE);
OUT = fopen(DATA,'w');
fprintf(OUT,'%s\t%s\t%s\t%s\t%s\n','T','X','Y','V','th');
AVI=sprintf('%s.avi',FILE);
mov = avifile(AVI)

%  Screen Output Header
disp('')
disp('')
disp('T         X         Y           V         th')            % Displaying one time header for Text Output Data

%  Ball Path
while v0>0.01
    j=j+1;
    tf=time_to_impact(width,height,th,mu,x0,y0,v0,e);        % Calling function (time_to_impact).
    for t=-0.0000001:t_int:tf
        i=i+1;
        T(i)=t+t0;
        V(i)=-mu*g*t+v0;
        Y(i)=(-mu*g*t^2/2+v0*t)*sin(th*pi/180)+y0;
        X(i)=(-mu*g*t^2/2+v0*t)*cos(th*pi/180)+x0;
        % Plotting the Billiard Table
        figure(1)
        BorX=[0      0 width width  0  ];
        BorY=[height 0 0     height height];
        plot(BorX,BorY);
        hold on;
        axis([-10 10+width -10 20+height]);
        axis equal;
        fill(BorX,BorY,'g')
        title('\bfBilliard Table','FontSize',14)
        % Plotting the Billiard Ball
        plot(X(i),Y(i),'ro','linewidth',5,'MarkerSize',5)
        title(['\bf\mu= ',num2str(mu),' , e= ',num2str(e),' , v0=',num2str(Vin),' , \theta=',num2str(thin)])
        disp([num2str(T(i)),'    ',num2str(X(i)),'    ',num2str(Y(i)),...
                '    ',num2str(V(i)),'    ',num2str(th)])
        Output_Data=[T(i) X(i) Y(i) V(i) th];
        fprintf(OUT,'%6.2f %6.2f %6.2f %6.2f %6.0f \n',Output_Data);
        M(i)=getframe;
        mov = addframe(mov,M(i));
        hold off;
    end
    x0=(-mu*g*tf^2/2+v0*tf)*cos(th*pi/180)+x0;
    y0=(-mu*g*tf^2/2+v0*tf)*sin(th*pi/180)+y0;
    if x0<1e-15;x0=0;end;
    if y0<1e-15;y0=0;end;
    th=theta_impact(th,width,height,x0,y0);                      % Calling function (theta_impact).
    t0=t0+tf;
    v0=V(i)*e;
    if mu==0 & e==1 & j==20                                      % Breaking Infinit Loop
        disp('----------- Infinit Loop! -------------')
        fprintf(OUT,'----------- Infinit Loop! -------------');
        break;
    end
end
mov = close(mov);
fclose(OUT);


%  Graphing Results
figure(2)
subplot(2,2,1),plot(X,Y),xlabel('\bfX'),ylabel('\bfY'),title('\bfX vs. Y','FontSize',12)
axis equal
subplot(2,2,2),plot(T,X,'r'),xlabel('\bfTime'),ylabel('\bfX'),title('\bfX vs. Time','FontSize',12)
subplot(2,2,3),plot(T,Y,'k'),xlabel('\bfTime'),ylabel('\bfY'),title('\bfY vs. Time','FontSize',12)
subplot(2,2,4),plot(T,V,'m'),xlabel('\bfTime'),ylabel('\bfV'),title('\bfV vs. Time','FontSize',12)
print(2,'-djpeg',FILE)
disp(' ');
disp(' ----------------------------------------------------------------- ')
disp(' ------------ The Program Ran Successfully !! -------------------- ')
disp(' ----------------------------------------------------------------- ')
disp(['The Initial Position is at X=',num2str(Xin),' and Y=',num2str(Yin)])
disp(['The Initial Position is at X=',num2str(X(i)),' and Y=',num2str(Y(i))])

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -