Animate a pendulum: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(13 intermediate revisions by 7 users not shown)
Line 1:
[[Category:Animation]]
{{task|Temporal media}}
{{requires|Graphics}}
Line 18 ⟶ 19:
 
pendulums.ads:
<syntaxhighlight lang=Ada"ada">generic
type Float_Type is digits <>;
Gravitation : Float_Type;
Line 39 ⟶ 40:
 
pendulums.adb:
<syntaxhighlight lang=Ada"ada">with Ada.Numerics.Generic_Elementary_Functions;
package body Pendulums is
package Math is new Ada.Numerics.Generic_Elementary_Functions (Float_Type);
Line 79 ⟶ 80:
 
example main.adb:
<syntaxhighlight lang=Ada"ada">with Ada.Text_IO;
with Ada.Calendar;
with Pendulums;
Line 142 ⟶ 143:
=={{header|Amazing Hopper}}==
{{Trans|FreeBASIC}}
<syntaxhighlight lang="text">
#include <flow.h>
#include <flow-term.h>
Line 265 ⟶ 266:
You can simulate a pseudo graphical mode in an Ubuntu Linux terminal by adding the following lines:
</pre>
<syntaxhighlight lang=Amazing"amazing Hopperhopper">
SYS("gsettings set org.gnome.Terminal.Legacy.Profile:/org/gnome/terminal/legacy/profiles:/:.../ font 'Ubuntu Mono 1'")
 
Line 279 ⟶ 280:
And substituting the holding coordinates of the pendulum:
</pre>
<syntaxhighlight lang=Amazing"amazing Hopperhopper">
 
// in "Animate a Pendulum"
Line 299 ⟶ 300:
This version doesn't use an complex physics calculation - I found a faster way.
{{libheader|GDIP}}
<syntaxhighlight lang=AutoHotkey"autohotkey">SetBatchlines,-1
;settings
SizeGUI:={w:650,h:400} ;Guisize
Line 343 ⟶ 344:
{{trans|Commodore BASIC}}
Two shapes are used to draw and undraw the pendulum. Undrawing and drawing is done on the page that is not being displayed to make the animation flicker free. Animation code is compacted and hoisted to the beginning of the program. Variables are defined for all non-zero values.
<syntaxhighlight lang="gwbasic"> 0 ON NOT T GOTO 9: FOR Q = 0 TO T STEP 0:BX = PX + L * S * SIN (F):BY = PY - L * S * COS (F): HCOLOR= 0: FOR I = 0 TO N(P): DRAW T + (I = N(P)) AT X(P,I),Y(P,I): NEXT I:N(P) = 0: HCOLOR= C
1 FOR X = PX TO BX STEP (BX - PX) / Z:Y = PY + (X - PX) * (BY - PY) / (BX - PX): DRAW T AT X,Y:X(P,N(P)) = X:Y(P,N(P)) = Y:N(P) = N(P) + 1: NEXT X
2 HCOLOR= T: DRAW B AT BX,BY:X(P,N(P)) = BX:Y(P,N(P)) = BY:A = PEEK (R + P):P = NOT P: POKE U,W + W * P:A = G * SIN (F) / L / H:V = V + A / Z:F = F + V: NEXT Q
Line 366 ⟶ 367:
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> MODE 8
*FLOAT 64
VDU 23,23,4;0;0;0; : REM Set line thickness
Line 397 ⟶ 398:
 
==={{header|Commodore BASIC}}===
<syntaxhighlight lang="commodorebasic">10 GOSUB 1000
20 THETA = π/2
30 G = 9.81
Line 423 ⟶ 424:
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">Const PI = 3.141592920
Dim As Double theta, g, l, accel, speed, px, py, bx, by
theta = PI/2
Line 447 ⟶ 448:
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang=IS"is-BASICbasic">100 PROGRAM "Pendulum.bas"
110 LET THETA=RAD(50):LET G=9.81:LET L=.5
120 CALL INIC
Line 505 ⟶ 506:
=={{header|C}}==
{{libheader|GLUT}}
<syntaxhighlight lang="c">#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
Line 588 ⟶ 589:
{{libheader|GDI (System.Drawing)}}
 
<syntaxhighlight lang="csharp">
using System;
using System.Drawing;
Line 648 ⟶ 649:
{{libheader|wxWidgets}}
File wxPendulumDlg.hpp
<syntaxhighlight lang="cpp">
#ifndef __wxPendulumDlg_h__
#define __wxPendulumDlg_h__
Line 717 ⟶ 718:
</syntaxhighlight>
File wxPendulumDlg.cpp
<syntaxhighlight lang="cpp">
// ---------------------
/// @author Martin Ettl
Line 840 ⟶ 841:
 
{{libheader|Swing}} {{libheader|AWT}}
<syntaxhighlight lang="clojure">
(ns pendulum
(:import
Line 914 ⟶ 915:
Pressing the spacebar adds a pendulum.
 
<syntaxhighlight lang="lisp">(defvar *frame-rate* 30)
(defvar *damping* 0.99 "Deceleration factor.")
 
Line 964 ⟶ 965:
{{libheader| Vcl.ExtCtrls}}
{{Trans|C#}}
<syntaxhighlight lang=Delphi"delphi">
unit main;
 
Line 1,068 ⟶ 1,069:
(This logic is more general than necessary; it is designed to be suitable for a larger application as well.)
 
<syntaxhighlight lang="e">#!/usr/bin/env rune
pragma.syntax("0.9")
 
Line 1,161 ⟶ 1,162:
[https://easylang.online/apps/pendulum.html Run it]
 
<syntaxhighlight lang="text">ang = 45
<lang>on animate
on animate
clear
move 50 50
circle 1
x = 50 + 40 * sin ang
y = 50 -+ 40 * cos ang
line x y
circle 56
vel += sin ang / 5
ang += vel
.
ang = 45</syntaxhighlight>
 
=={{header|Elm}}==
<syntaxhighlight lang="elm">import Color exposing (..)
import Collage exposing (..)
import Element exposing (..)
Line 1,252 ⟶ 1,254:
 
=={{header|ERRE}}==
<syntaxhighlight lang=ERRE"erre">
PROGRAM PENDULUM
 
Line 1,324 ⟶ 1,326:
===DOS32 version===
{{works with|Euphoria|3.1.1}}
<syntaxhighlight lang="euphoria">include graphics.e
include misc.e
 
Line 1,384 ⟶ 1,386:
=={{header|F_Sharp|F#}}==
A nice application of F#'s support for units of measure.
<syntaxhighlight lang="fsharp">open System
open System.Drawing
open System.Windows.Forms
Line 1,469 ⟶ 1,471:
=={{header|Factor}}==
Approximation of the pendulum for small swings : theta = theta0 * cos(omega0 * t)
<syntaxhighlight lang="factor">USING: accessors alarms arrays calendar colors.constants kernel
locals math math.constants math.functions math.rectangles
math.vectors opengl sequences system ui ui.gadgets ui.render ;
Line 1,512 ⟶ 1,514:
 
=={{header|FBSL}}==
<syntaxhighlight lang="qbasic">#INCLUDE <Include\Windows.inc>
 
FBSLSETTEXT(ME, "Pendulum")
Line 1,574 ⟶ 1,576:
=={{header|Fortran}}==
Uses system commands (gfortran) to clear the screen. An initial starting angle is allowed between 90 (to the right) and -90 degrees (to the left). It checks for incorrect inputs.
<syntaxhighlight lang="fortran">
!Implemented by Anant Dixit (October, 2014)
program animated_pendulum
Line 1,935 ⟶ 1,937:
=={{header|Groovy}}==
Straight translation of Java solution groovified by removing explicit definitions and converting casts to Groovy as style where needed.
<syntaxhighlight lang="groovy">
import java.awt.*;
import javax.swing.*;
Line 1,989 ⟶ 1,991:
}
</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
void local fn BuildWindow
window 1, @"Animated Pendulum in FutureBasic", ( 0, 0, 640, 400 )
WindowSetBackgroundColor( 1, fn ColorBlack )
WindowSetMinSize( 1, fn CGSizeMake( 640, 400 ) )
WindowSetMaxSize( 1, fn CGSizeMake( 640, 400 ) )
end fn
 
local fn AnimatedPendulum
block double theta, gravity, length, accel, speed, weight, tempo, px, py, bx, by
block ColorRef color = fn ColorWithRGB( 0.164, 0.793, 0.075, 1.0 )
theta = pi/2.0 // Denominator of 2.0 = 180-degree swing, < 2.0 narrows inscribed arc, > 2.0 widens it.
gravity = 9.90 // Adjusts effect of gravity on swing. Smaller values slow arc swing.
length = 0.95 // Tweak for length of pendulum arm
speed = 0 // Zero this or you get a propellor!
px = 320 // Pivot horizontal center x point (half window width)
py = 30 // Pivot y center y point from top
weight = 42 // Diameter of pendulum weight
tempo = 75 // Smaller value increases pendulum tempo, larger value slows it.
timerbegin, 0.02, YES
bx = px + length * 300 * sin(theta) // Pendulum bottom x point
by = py - length * 300 * cos(theta) // Pendulum bottom y point
cls
pen 6.0, color
line px, py to bx, by
oval fill bx -weight/2, by -weight/2, weight, weight, color // Traveling weight
pen 4.0
oval fill 313, 20, 16, 16, fn ColorGray // Top center point
accel = gravity * sin(theta) / length / tempo
speed += accel / tempo
theta += speed
timerEnd
end fn
 
void local fn DoDialog( ev as long, tag as long, wnd as long )
select ( ev )
case _windowWillClose : end
end select
end fn
 
on dialog fn DoDialog
 
fn BuildWindow
fn AnimatedPendulum
 
HandleEvents
</syntaxhighlight>
[[File:Animated_Pendulum_FutureBasic2.gif]]
 
=={{header|Go}}==
Using {{libheader|GXUI}} from [https://github.com/google/gxui Github]
<syntaxhighlight lang="go">package main
 
import (
Line 2,111 ⟶ 2,164:
=={{header|Haskell}}==
{{libheader|HGL}}
<syntaxhighlight lang="haskell">import Graphics.HGL.Draw.Monad (Graphic, )
import Graphics.HGL.Draw.Picture
import Graphics.HGL.Utils
Line 2,146 ⟶ 2,199:
=== Alternative solution ===
{{libheader|Gloss}}
<syntaxhighlight lang="haskell">import Graphics.Gloss
 
-- Initial conditions
Line 2,201 ⟶ 2,254:
[http://www.HicEst.com/DIFFEQ.htm DIFFEQ] and the callback procedure pendulum numerically integrate the pendulum equation.
The display window can be resized during the run, but for window width not equal to 2*height the pendulum rod becomes a rubber band instead:
<syntaxhighlight lang=HicEst"hicest">REAL :: msec=10, Lrod=1, dBob=0.03, g=9.81, Theta(2), dTheta(2)
BobMargins = ALIAS(ls, rs, ts, bs) ! box margins to draw the bob
 
Line 2,241 ⟶ 2,294:
{{trans|Scheme}}
 
<syntaxhighlight lang=Unicon"unicon">
import gui
$include "guih.icn"
Line 2,339 ⟶ 2,392:
=={{header|J}}==
Works for '''J6'''
<syntaxhighlight lang="j">require 'gl2 trig'
coinsert 'jgl2'
 
Line 2,383 ⟶ 2,436:
pend_run'' NB. run animation</syntaxhighlight>
Updated for changes in '''J8'''
<syntaxhighlight lang="j">require 'gl2 trig'
coinsert 'jgl2'
Line 2,440 ⟶ 2,493:
=={{header|Java}}==
{{libheader|Swing}} {{libheader|AWT}}
<syntaxhighlight lang="java">import java.awt.*;
import javax.swing.*;
 
Line 2,497 ⟶ 2,550:
{{trans|E}} (plus gratuitous motion blur)
 
<syntaxhighlight lang="javascript"><html><head>
<title>Pendulum</title>
</head><body style="background: gray;">
Line 2,563 ⟶ 2,616:
Also we'll use a dimensionless formulation of the problem (taking unit value for the mass, the length and so on).
 
<syntaxhighlight lang="javascript"><svg height="100%" width="100%" viewBox="-2 0 4 4" xmlns="http://www.w3.org/2000/svg">
<line id="string" x1="0" y1="0" x2="1" y2="0" stroke="grey" stroke-width="0.05" />
<circle id="ball" cx="0" cy="0" r="0.1" fill="black" />
Line 2,607 ⟶ 2,660:
 
=={{header|Julia}}==
Differential equation based solution using the Luxor graphics library.<syntaxhighlight lang="julia">using Luxor
using Colors
using BoundaryValueDiffEq
Line 2,652 ⟶ 2,705:
animate(muv, [Scene(muv, backdrop),
Scene(muv, frame, easingfunction=easeinoutcubic)],
creategif=true, pathname=giffilename)</syntaxhighlight>
{{out}}
</syntaxhighlight>
[[File:Pendulum animation.gif|320px]]
<pre>
</pre>
 
=={{header|Kotlin}}==
Conversion of Java snippet.
<syntaxhighlight lang="scala">import java.awt.*
import java.util.concurrent.*
import javax.swing.*
Line 2,705 ⟶ 2,761:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">nomainwin
WindowWidth = 400
WindowHeight = 300
Line 2,749 ⟶ 2,805:
 
=={{header|Lingo}}==
<syntaxhighlight lang=Lingo"lingo">global RODLEN, GRAVITY, DT
global velocity, acceleration, angle, posX, posY
Line 2,795 ⟶ 2,851:
=={{header|Logo}}==
{{works with|UCB Logo}}
<syntaxhighlight lang="logo">make "angle 45
make "L 1
make "bob 10
Line 2,827 ⟶ 2,883:
=={{header|Lua}}==
Needs L&Ouml;VE 2D Engine
<syntaxhighlight lang="lua">
function degToRad( d )
return d * 0.01745329251
Line 2,859 ⟶ 2,915:
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang=M2000"m2000 Interpreterinterpreter">
Module Pendulum {
back()
Line 2,910 ⟶ 2,966:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">freqtmax = 8; length = freq^(-1/2)10;
g = 9.8;
Animate[Graphics[
l = 1;
List[{Line[{{0, 0}, length {Sin[T], -Cos[T]}} /. {T -> (Pi/6) Cos[2 Pi freq t]}], PointSize[Large],
pendulum = Module[
Point[{length {Sin[T], -Cos[T]}} /. {T -> (Pi/6) Cos[2 Pi freq t]}]}],
{g, l},
PlotRange -> {{-0.3, 0.3}, {-0.5, 0}}], {t, 0, 1}, AnimationRate -> 0.07]</syntaxhighlight>
ParametricNDSolve[
[[File:mmapendulum.gif]]
{
y''[t] + g/l Sin[y[t]] == 0,
y[0] == 0, y'[0] == 1
},
{y},
{t, 0, tmax},
{g, l}
]
];
Animate[
Graphics[
Rotate[
{Line[{{0, 0}, {0, -1}}], Disk[{0, -1}, .1]},
Evaluate[y[g, l] /. pendulum][t],
{0, 0}
],
PlotRange -> {{-l, l}, {-l - .5, 0}}
],
{t, 0, tmax},
AnimationRate -> 1
]</syntaxhighlight>
 
=={{header|MATLAB}}==
pendulum.m
<syntaxhighlight lang=MATLAB"matlab">%This is a numerical simulation of a pendulum with a massless pivot arm.
 
%% User Defined Parameters
Line 2,996 ⟶ 3,073:
 
Conversion from C with some modifications: changing some variable names, adding a display function to make the program work with "freeGlut", choosing another initial angle, etc.
<syntaxhighlight lang=Nim"nim"># Pendulum simulation.
 
import math
Line 3,109 ⟶ 3,186:
 
This version uses the same equations but replace OpenGL by Gtk3 with the “gintro” bindings.
<syntaxhighlight lang=Nim"nim"># Pendulum simulation.
 
import math
Line 3,237 ⟶ 3,314:
=={{header|ooRexx}}==
ooRexx does not have a portable GUI, but this version is similar to the Ada version and just prints out the coordinates of the end of the pendulum.
<syntaxhighlight lang=ooRexx"oorexx">
pendulum = .pendulum~new(10, 30)
 
Line 3,279 ⟶ 3,356:
Inspired by the E and Ruby versions.
 
<syntaxhighlight lang="oz">declare
[QTk] = {Link ['x-oz://system/wp/QTk.ozf']}
 
Line 3,370 ⟶ 3,447:
This does not have the window resizing handling that Tcl does.
 
<syntaxhighlight lang="perl">
use strict;
use warnings;
Line 3,442 ⟶ 3,519:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/animate_pendulum2.htm here].
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\animate_pendulum.exw
Line 3,554 ⟶ 3,631:
=={{header|PicoLisp}}==
A minimalist solution. The pendulum consists of the center point '+', and the swinging xterm cursor.
<syntaxhighlight lang=PicoLisp"picolisp">(load "@lib/math.l")
 
(de pendulum (X Y Len)
Line 3,570 ⟶ 3,647:
(+ X (*/ Len (sin Angle) 1.0)) ) ) ) )</syntaxhighlight>
Test (hit any key to stop):
<syntaxhighlight lang=PicoLisp"picolisp">(pendulum 40 1 36)</syntaxhighlight>
 
=={{header|Portugol}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang=Portugol"portugol">
programa {
inclua biblioteca Matematica --> math // math library
Line 3,625 ⟶ 3,702:
=={{header|Prolog}}==
SWI-Prolog has a graphic interface XPCE.
<syntaxhighlight lang=Prolog"prolog">:- use_module(library(pce)).
 
pendulum :-
Line 3,709 ⟶ 3,786:
=={{header|PureBasic}}==
If the code was part of a larger application it could be improved by specifying constants for the locations of image elements.
<syntaxhighlight lang=PureBasic"purebasic">Procedure handleError(x, msg.s)
If Not x
MessageRequester("Error", msg)
Line 3,821 ⟶ 3,898:
 
{{trans|C}}
<syntaxhighlight lang="python">import pygame, sys
from pygame.locals import *
from math import sin, cos, radians
Line 3,905 ⟶ 3,982:
 
===Python: using tkinter===
<syntaxhighlight lang="python">
''' Python 3.6.5 code using Tkinter graphical user interface.'''
 
Line 3,997 ⟶ 4,074:
 
=={{header|QB64}}==
<syntaxhighlight lang="qbasic">'declare and initialize variables
CONST PI = 3.141592
 
Line 4,096 ⟶ 4,173:
 
=={{header|R}}==
<syntaxhighlight lang=R"r">library(DescTools)
 
pendulum<-function(length=5,radius=1,circle.color="white",bg.color="white"){
Line 4,121 ⟶ 4,198:
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
 
Line 4,148 ⟶ 4,225:
Handles window resizing, modifies pendulum length and period as window height changes. May need to tweek $ppi scaling to get good looking animation.
 
<syntaxhighlight lang=perl6"raku" line>use SDL2::Raw;
use Cairo;
 
Line 4,245 ⟶ 4,322:
this version is similar to the '''Ada''' version and just
<br>displays the coordinates of the end of the pendulum.
<syntaxhighlight lang="rexx">/*REXX program displays the (x, y) coördinates (at the end of a swinging pendulum). */
parse arg cycles Plength theta . /*obtain optional argument from the CL.*/
if cycles=='' | cycles=="," then cycles= 60 /*Not specified? Then use the default.*/
Line 4,346 ⟶ 4,423:
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project : Animate a pendulum
 
Line 4,458 ⟶ 4,535:
The RLaB script that solves the problem is
 
<syntaxhighlight lang=RLaB"rlab">
//
// example: solve ODE for pendulum
Line 4,532 ⟶ 4,609:
the Tcl pendulum swings noticibly faster.
 
<syntaxhighlight lang="ruby">require 'tk'
 
$root = TkRoot.new("title" => "Pendulum Animation")
Line 4,597 ⟶ 4,674:
 
==={{libheader|Shoes}}===
<syntaxhighlight lang="ruby">Shoes.app(:width => 320, :height => 200) do
@centerX = 160
@centerY = 25
Line 4,656 ⟶ 4,733:
 
==={{libheader|Ruby/Gosu}}===
<syntaxhighlight lang="ruby">#!/bin/ruby
 
begin; require 'rubygems'; rescue; end
Line 4,768 ⟶ 4,845:
 
{{libheader|piston_window}}
<syntaxhighlight lang=Rust"rust">
// using version 0.107.0 of piston_window
use piston_window::{clear, ellipse, line_from_to, PistonWindow, WindowSettings};
Line 4,830 ⟶ 4,907:
=={{header|Scala}}==
{{libheader|Scala}}
<syntaxhighlight lang=Scala"scala">import java.awt.Color
import java.util.concurrent.{Executors, TimeUnit}
 
Line 4,893 ⟶ 4,970:
This is a direct translation of the Ruby/Tk example into Scheme + PS/Tk.
 
<syntaxhighlight lang="scheme">#!r6rs
 
;;; R6RS implementation of Pendulum Animation
Line 4,964 ⟶ 5,041:
(tk/after 500 animate)
(tk-event-loop tk)))
</syntaxhighlight>
 
Another version using gauche scheme:
 
<syntaxhighlight lang="scheme">
#!/usr/bin/env gosh
#| -*- mode: scheme; coding: utf-8; -*- |#
(use gl)
(use gl.glut)
(use gl.simple.viewer)
(use math.const)
(define (deg->rad degree) (* (/ degree 180) pi))
(define (rad->deg radians) (* (/ radians pi) 180))
(define (main args)
(glut-init args)
(let* ((φ (deg->rad 179)) (l 0.5) (bob 0.02) (q (make <glu-quadric>))
(draw-pendulum (lambda()
(gl-push-matrix*
(gl-scale 4 4 4)
(gl-translate 0 l 0)
(gl-rotate (rad->deg φ) 0 0 1)
(gl-begin GL_LINES)
(gl-vertex 0 0)
(gl-vertex 0 (- l))
(gl-end)
(gl-translate 0 (- l) 0)
(glu-sphere q bob 10 10))))
(g 9.81)
(φ̇ 0)
(euler-step (lambda(h)
(inc! φ̇ (* (- (* (/ g l) (sin φ))) h))
(inc! φ (* φ̇ h)))))
(simple-viewer-display
(lambda ()
;; I hope sync to VBLANK aka VSYNC works and the display has ~60Hz
(euler-step 1/60)
(draw-pendulum)
(glut-post-redisplay))))
(simple-viewer-window 'pendulum)
(glut-full-screen)
(simple-viewer-run :rescue-errors #f))
 
</syntaxhighlight>
 
=={{header|Scilab}}==
The animation is displayed on a graphic window, and won't stop until it shows all positions calculated unless the user abort the execution on Scilab console.
<syntaxhighlight lang="text">//Input variables (Assumptions: massless pivot, no energy loss)
bob_mass=10;
g=-9.81;
Line 5,057 ⟶ 5,176:
{{libheader|EaselSL}}
Using the [https://github.com/bethune-bryant/Easel Easel Engine for SequenceL] <br>
<syntaxhighlight lang="sequencel">import <Utilities/Sequence.sl>;
import <Utilities/Conversion.sl>;
import <Utilities/Math.sl>;
Line 5,131 ⟶ 5,250:
=={{header|Sidef}}==
{{trans|Perl}}
<syntaxhighlight lang="ruby">require('Tk')
 
var root = %s<MainWindow>.new('-title' => 'Pendulum Animation')
Line 5,194 ⟶ 5,313:
 
=={{header|smart BASIC}}==
<syntaxhighlight lang="smart BASICbasic">'Pendulum
'By Dutchman
' --- constants
Line 5,237 ⟶ 5,356:
{{works with|Tcl|8.5}}
==={{libheader|Tk}}===
<syntaxhighlight lang="tcl">package require Tcl 8.5
package require Tk
 
Line 5,320 ⟶ 5,439:
=={{header|VBScript}}==
Well, VbScript does'nt have a graphics mode so this is a wobbly textmode pandulum. It should be called from cscript.
<syntaxhighlight lang="vb">
option explicit
 
Line 5,371 ⟶ 5,490:
{{libheader|DOME}}
{{libheader|Wren-dynamic}}
<syntaxhighlight lang=ecmascript"wren">import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
Line 5,421 ⟶ 5,540:
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
 
proc Ball(X0, Y0, R, C); \Draw a filled circle
Line 5,459 ⟶ 5,578:
 
=={{header|Yabasic}}==
<syntaxhighlight lang=Yabasic"yabasic">clear screen
open window 400, 300
window origin "cc"
Line 5,486 ⟶ 5,605:
 
exit</syntaxhighlight>
 
=={{header|Zig}}==
{{libheader|Raylib}}
{{works with|Zig|0.11.0dev}} {{works with|Raylib|4.6dev}}
{{trans|Nim}}
<syntaxhighlight lang="zig">const math = @import("std").math;
const c = @cImport({
@cInclude("raylib.h");
});</syntaxhighlight>
<syntaxhighlight lang="zig">pub fn main() void {
c.SetConfigFlags(c.FLAG_VSYNC_HINT);
c.InitWindow(640, 320, "Pendulum");
defer c.CloseWindow();
 
// Simulation constants.
const g = 9.81; // Gravity (should be positive).
const length = 5.0; // Pendulum length.
const theta0 = math.pi / 3.0; // Initial angle for which omega = 0.
 
const e = g * length * (1 - @cos(theta0)); // Total energy = potential energy when starting.
 
// Simulation variables.
var theta: f32 = theta0; // Current angle.
var omega: f32 = 0; // Angular velocity = derivative of theta.
var accel: f32 = -g / length * @sin(theta0); // Angular acceleration = derivative of omega.
 
c.SetTargetFPS(60);
 
while (!c.WindowShouldClose()) // Detect window close button or ESC key
{
const half_width = @as(f32, @floatFromInt(c.GetScreenWidth())) / 2;
const pivot = c.Vector2{ .x = half_width, .y = 0 };
 
// Compute the position of the mass.
const mass = c.Vector2{
.x = 300 * @sin(theta) + pivot.x,
.y = 300 * @cos(theta),
};
 
{
c.BeginDrawing();
defer c.EndDrawing();
 
c.ClearBackground(c.RAYWHITE);
 
c.DrawLineV(pivot, mass, c.GRAY);
c.DrawCircleV(mass, 20, c.GRAY);
}
 
// Update theta and omega.
const dt = c.GetFrameTime();
theta += (omega + dt * accel / 2) * dt;
omega += accel * dt;
 
// If, due to computation errors, potential energy is greater than total energy,
// reset theta to ±theta0 and omega to 0.
if (length * g * (1 - @cos(theta)) >= e) {
theta = math.sign(theta) * theta0;
omega = 0;
}
accel = -g / length * @sin(theta);
}
}</syntaxhighlight>
 
=={{header|ZX Spectrum Basic}}==
{{trans|ERRE}}
In a real Spectrum it is too slow. Use the BasinC emulator/editor at maximum speed for realistic animation.
<syntaxhighlight lang="zxbasic">10 OVER 1: CLS
20 LET theta=1
30 LET g=9.81
Line 5,507 ⟶ 5,689:
1010 CIRCLE bobx,boby,3
1020 RETURN</syntaxhighlight>
 
 
{{omit from|LFE}}
Line 5,513 ⟶ 5,696:
{{omit from|PHP}}
{{omit from|SQL PL|It does not handle GUI}}
 
[[Category:Animation]]
9,485

edits