
One good way of making an animation is by simulating a physical system and illustrating the variables in that system using a dynamically changing graphical display.

The classic such physical system is a simple gravity pendulum.



Task

Create a simple physical model of a pendulum and animate it.




 
programa {
    inclua biblioteca Matematica --> math // math library
    inclua biblioteca Util --> u // util library 
    inclua biblioteca Graficos --> g // graphics library
    inclua biblioteca Teclado --> t // keyboard library
 
    real accel, bx, by
    real theta	= math.PI * 0.5
    real g	= 9.81
    real l	= 1.0
    real speed	= 0.0
    real px	= 320.0
    real py	= 10.0
    inteiro w	= 10 // circle width and height (radius)
 
    // main entry
    funcao inicio() {
 
    g.iniciar_modo_grafico(verdadeiro)
        g.definir_dimensoes_janela(640, 400)
 
        // while ESC key not pressed
        enquanto (nao t.tecla_pressionada(t.TECLA_ESC)) {
            bx = px + l * 300.0 * math.seno(theta)
            by = py - l * 300.0 * math.cosseno(theta)
 
            g.definir_cor(g.COR_PRETO)
            g.limpar()
 
            g.definir_cor(g.COR_BRANCO)
            g.desenhar_linha(px, py, bx, by)
            g.desenhar_elipse(bx - w, by - w, w * 2, w * 2, verdadeiro)
 
            accel = g * math.seno(theta) / l / 100.0
            speed = speed + accel / 100.0
            theta = theta + speed
 
            g.desenhar_texto(0, 370, "Pendulum")
            g.desenhar_texto(0, 385, "Press ESC to quit")
 
            g.renderizar()
            u.aguarde(10)
        }
    }
}
 
 