Animate a pendulum: Difference between revisions

no edit summary
No edit summary
Line 2,003:
# Callback to stop the animation cleanly when the GUI goes away
bind .c <Destroy> {after cancel $animation}</lang>
 
=={{header|XPL0}}==
<lang XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations
 
proc Ball(X0, Y0, R, C); \Draw a filled circle
int X0, Y0, R, C; \center coordinates, radius, color
int X, Y;
for Y:= -R to R do
for X:= -R to R do
if X*X + Y*Y <= R*R then Point(X+X0, Y+Y0, C);
 
 
def L = 2.0, \pendulum arm length (meters)
G = 9.81, \acceleration due to gravity (meters/second^2)
Pi = 3.14,
DT = 1.0/72.0; \delta time = screen refresh rate (seconds)
def X0=640/2, Y0=480/2; \anchor point = center coordinate
real S, V, A, T; \arc length, velocity, acceleration, theta angle
int X, Y; \ball coordinates
 
[SetVid($101); \set 640x480x8 graphic display mode
T:= Pi*0.75; V:= 0.0; \starting angle and velocity
S:= T*L;
repeat A:= -G*Sin(T);
V:= V + A*DT;
S:= S + V*DT;
T:= S/L;
X:= X0 + fix(L*100.0*Sin(T)); \100 scales to fit screen
Y:= Y0 + fix(L*100.0*Cos(T));
Move(X0, Y0); Line(X, Y, 7); \draw pendulum
Ball(X, Y, 10, $E\yellow\);
while port($3DA) & $08 do []; \wait for vertical retrace to go away
repeat until port($3DA) & $08; \wait for vertical retrace signal
Move(X0, Y0); Line(X, Y, 0); \erase pendulum
Ball(X, Y, 10, 0\black\);
until KeyHit; \keystroke terminates program
SetVid(3); \restore normal text screen
]</lang>
 
{{omit from|PARI/GP}}
772

edits