用VB方法画一圆的外摆线轨迹动画程序

用VB方法画一圆的外摆线轨迹动画程序,第1张

'在窗体上放置控件:Command1、Timer1、Shape1

Private Sub Command1_Click()

Dim P As Single, t As Single, x As Single, y As Single

Me.Scale (-8, -8)-(8, 8)

P = 3.1415926

Me.Cls: Me.AutoRedraw = True

Call Bai(1, 1, 2, 0, x, y): Me.PSet (x, y)

For t = 0 To P * 2 Step P / 90

Call Bai(1, 1, 2, t, x, y)

Me.Line -(x, y)

Next

Shape1.Shape = 1: Shape1.Width = 0.5

Shape1.Height = Shape1.Width

Shape1.FillStyle = 0: Shape1.FillColor = 255

Timer1.Enabled = Not Timer1: Timer1.Interval = 10

If Timer1.Enabled Then Command1.Caption = "停止" Else Command1.Caption = "开始"

End Sub

Private Sub Bai(a As Single, b As Single, d As Single, t As Single, x As Single, y As Single)

x = (a + b) * Cos(t) - b * Cos((a + b) / d) * t

y = (a + b) * Sin(t) - b * Sin((a + b) / b) * t

End Sub

Private Sub Form_Load()

Timer1.Enabled = False: Command1.Caption = "开始"

End Sub

Private Sub Timer1_Timer()

Static t As Single

Dim x As Single, y As Single, P As Single

P = 3.1415926

t = t + P / 90

If t >P * 2 Then t = 0

Call Bai(1, 1, 2, t, x, y)

Shape1.Move x - Shape1.Width * 0.5, y - Shape1.Height * 0.5

End Sub

'我的例子移动的是正方形,要用菱形,你可以将 Shape1 换成 Image,然后加载一个画有菱形的图片文件

%挂摆横梁

plot([-0.20.2],[00],'color','y','linestyle','-',...

'linewidth',10)

%画初始位置的单摆

g=0.098 %重力加速度,可以调节摆的摆速

l1=1

l2=0.8

l3=0.6

theta0=pi/4

x01=l1*sin(theta0)

y01=(-1)*l1*cos(theta0)

x02=l2*sin(theta0)

y02=(-1)*l2*cos(theta0)

x03=l3*sin(theta0)

y03=(-1)*l3*cos(theta0)

axis([-0.75,0.75,-1.25,0])

axis('off') %不显示坐标轴

%创建摆锤

head1=line(x01,y01,'color','r','linestyle','.',...

'erasemode','xor','markersize',40)

head2=line(x02,y02,'color','g','linestyle','.',...

'erasemode','xor','markersize',40)

head3=line(x03,y03,'color','b','linestyle','.',...

'erasemode','xor','markersize',40)

%创建摆杆

body1=line([0x01],[0y01],'color','b','linestyle','-',...

'erasemode','xor')

body2=line([0x02],[0y02],'color','b','linestyle','-',...

'erasemode','xor')

body3=line([0x03],[0y03],'color','b','linestyle','-',...

'erasemode','xor')

%摆的运动

t=0

dt=0.1

while t< 30

    t=t+dt

    theta1=theta0*cos(sqrt(g/l1)*t)

    x1=l1*sin(theta1)

    y1=(-1)*l1*cos(theta1)

    set(head1,'xdata',x1,'ydata',y1)

    set(body1,'xdata',[0x1],'ydata',[0y1])

    

    theta2=theta0*cos(sqrt(g/l2)*t)

    x2=l2*sin(theta2)

    y2=(-1)*l2*cos(theta2)

    set(head2,'xdata',x2,'ydata',y2)

    set(body2,'xdata',[0x2],'ydata',[0y2])

    

    theta3=theta0*cos(sqrt(g/l3)*t)

    x3=l3*sin(theta3)

    y3=(-1)*l3*cos(theta3)

    set(head3,'xdata',x3,'ydata',y3)

    set(body3,'xdata',[0x3],'ydata',[0y3])

      

    drawnow

end

>> t=0:0.001*pi:2*pi

>> x=3*(t-sin(t))

>> y=3*(1-cos(t))

>> polar(x,y)

其中,a=3.

polar函数用于极坐标作图。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11203517.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存