%% 《理论力学(第3版)》（李俊峰，张雄编著）第7页：例1-1
%% 有一杆分别沿着墙和地面滑动，以杆上M点到底部距离为参数绘制运动轨迹。
%% 输入参数
L = 1;      % 杆长
b = 0.3*L;  % M点到底部距离
mode = 1;   % 是否生成视频文件，1，生成，0，不生成
name = 'Example_1_1';  %视频文件名

%% 绘制

a = L-b;
u = 0:L/100:L;  % 杆右端点x坐标

if mode == 1
    aviobj = VideoWriter(name);  % 新建视频文件
    open(aviobj);                % 打开文件
end

xy_M=zeros(2,length(u));  %  初始化M点坐标
for ii = 1:1:length(u)
    plot([0,1.05*L],[0,0],'k','linewidth',2);  % 绘制x轴
    axis('equal');
    hold on;
    
    plot([0,0],[0,1.05*L],'k','linewidth',2);  % 绘制y轴
    set(gca,'xtick',[],'ytick',[],'xcolor','w','ycolor','w')

    arrow([1.05*L,0],[1,0],0.03*L,2,0,'k');   % 绘制箭头
    arrow([0,1.05*L],[0,1],0.03*L,2,0,'k');

    %  在y轴上绘制一系列平行的短斜线（表示固定墙壁）
    temp1 = 0.05*L:0.05*L:0.75*L;
    for jj = 1:1:length(temp1)
        plot([0,-0.04*L],[temp1(jj),temp1(jj)-0.04*L],'k','linewidth',1);
    end
    
    v = sqrt(L^2-u(ii)^2);  % 杆左端点y坐标
    plot([u(ii),0],[0,v],'k','linewidth',4);   % 绘制杆
    

    % 绘制M点
    temp1=[-u(ii),v];
    temp1=temp1/norm(temp1);
    x=u(ii)+temp1(1)*b;   % M点的x坐标
    y=temp1(2)*b;         % M点的y坐标
    scatter(x,y,50,'o','MarkerEdgeColor','k','MarkerFaceColor','k','LineWidth',1);
    xy_M(:,ii)=[x;y];     % 存储x,y坐标
    
    plot(xy_M(1,1:ii),xy_M(2,1:ii),'k','linewidth',1);  %  绘制M点的轨迹
       
    % 标注坐标轴
    text(-0.08*L,1.1*L,'\ity','FontName','Times New Roman','FontSize',25)
    text(1.05*L,-0.05*L,'\itx','FontName','Times New Roman','FontSize',25)
    
    hold off;
    pause(0.01);  % 暂停0.01s，便于观察
    
    % 输出视频
    if mode == 1
        currFrame = getframe;
        writeVideo(aviobj,currFrame);
    end
end

if mode == 1
    close(aviobj);   % 关闭视频文件
end


%% 函数定义

% 功能：绘制箭头
% 输入参数
%    xy： 箭头坐标，1行2列向量
%    fx： 箭头方向，表示箭头末端到箭头尖端的方向，1行2列向量
%    sz： 箭头大小，即箭头三角形底边长度
%    sc： 箭头长宽比，（箭头为等腰三角形）箭头三角形腰长与底边长度之比
%    mode： 模式， 模式为0表示箭头底边位于给定的箭头坐标处，模式为1表示箭头尖端位于给定的箭头坐标处
%    color： 箭头颜色， 与MATLAB绘图颜色代号相同，如'k'表示黑色
function arrow(xy,fx,sz,sc,mode,color)
    sc = sqrt(sc^2-1/4);
    theta = atan2(fx(2),fx(1));
    
    xy1 = [0,sz/2];
    xy2 = [0,-sz/2];
    xy3 = [sc*sz,0];
    
    [temp1,temp2] = rotation(xy1(1),xy1(2),theta);
    xy1 = [temp1,temp2];
    
    [temp1,temp2] = rotation(xy2(1),xy2(2),theta);
    xy2 = [temp1,temp2];
    
    [temp1,temp2] = rotation(xy3(1),xy3(2),theta);
    xy3 = [temp1,temp2];
    
    if mode==0   
        xy1=[xy1(1)+xy(1),xy1(2)+xy(2)];
        xy2=[xy2(1)+xy(1),xy2(2)+xy(2)];
        xy3=[xy3(1)+xy(1),xy3(2)+xy(2)];
    else
        xy1=[xy1(1)+xy(1)-xy3(1),xy1(2)+xy(2)-xy3(2)];
        xy2=[xy2(1)+xy(1)-xy3(1),xy2(2)+xy(2)-xy3(2)];
        xy3=[xy(1),xy(2)];    
    end
    
    if nargin == 5
        fill([xy1(1),xy2(1),xy3(1)],[xy1(2),xy2(2),xy3(2)],'k');
    else
        fill([xy1(1),xy2(1),xy3(1)],[xy1(2),xy2(2),xy3(2)],color);
    end
end

% 功能： 将矢量[x,y]逆时针旋转theta角
% 输入参数
%    x,y： 旋转前坐标，1行n列向量
%    theta： 旋转角度
% 输出参数
%    x_new, y_new： 旋转后坐标，1行n列向量
function [x_new,y_new] = rotation(x,y,theta)
    R = [cos(theta),-sin(theta)
         sin(theta), cos(theta)];
    temp = R*[x;y];
    x_new = temp(1,:);
    y_new = temp(2,:);
end