2007年3月25日 星期日

作業4-2

A=[10,0]; %設定兩連桿之位置
B=[0,0];
cx=15; %設定彈簧位置
cy=0;
dx=10; %連桿右中心之位置
dy=0;
d=4; %連桿厚度

AB=(B(1)+j*B(2))-(A(1)+j*A(2)); %計算連桿長度
D=abs(AB); %取絕對值
th=angle(AB); %計算連桿的水平夾角
t=linspace(pi/2,2.5*pi,20); %分割角度
Cout=d/2*exp(j*t'); %計算外圓半徑
Cin=Cout/2; %計算內圓半徑
P=[0;Cin;Cout(1:10); %設定圖形各點於水平狀態的座標
D+Cout(11:20);
D+Cin;D+Cout(20);Cout(1)];
xx=real(P); %取x項
yy=imag(P); %取y項
x=xx*cos(th)-yy*sin(th)+A(1); %計算各點之x座標
y=xx*sin(th)+yy*cos(th)+A(2); %計算各點之y座標

axis([-20,20,-12,12]);
axis equal;grid on; % 設定格點,刻度

h1=line(x,y,'erasemode','xor','color','r','linewidth',2); % 繪出連桿
h2=line([cx,dx],[cy,dy],'erasemode','xor','color','m','linewidth',2,'linestyle','-.'); % 繪出彈簧
x1=get(h1,'xdata');y1=get(h1,'ydata'); %初始狀態

for theta=0:1:360; %繪出移動路徑(整個圖形繞一圈)

x2=x1*cosd(theta)-y1*sind(theta); %轉動公式
y2=x1*sind(theta)+y1*cosd(theta);

dxp=dx*cosd(theta)-dy*sind(theta);
dyp=dx*sind(theta)+dy*cosd(theta);

set(h1,'xdata',x2,'ydata',y2); %將轉換出的值存入,並繪出
set(h2,'xdata',[cx,dxp],'ydata',[cy,dyp]);
pause(0.01);

end; %跳出迴圈



以下為執行結果

沒有留言: