Animate a pendulum: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
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">on animate
clear
move 50 50
Line 1,175 ⟶ 1,176:
 
=={{header|Elm}}==
<syntaxhighlight lang="elm">import Color exposing (..)
import Collage exposing (..)
import Element exposing (..)
Line 1,252 ⟶ 1,253:
 
=={{header|ERRE}}==
<syntaxhighlight lang=ERRE"erre">
PROGRAM PENDULUM
 
Line 1,324 ⟶ 1,325:
===DOS32 version===
{{works with|Euphoria|3.1.1}}
<syntaxhighlight lang="euphoria">include graphics.e
include misc.e
 
Line 1,384 ⟶ 1,385:
=={{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,470:
=={{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,513:
 
=={{header|FBSL}}==
<syntaxhighlight lang="qbasic">#INCLUDE <Include\Windows.inc>
 
FBSLSETTEXT(ME, "Pendulum")
Line 1,574 ⟶ 1,575:
=={{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,936:
=={{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,992 ⟶ 1,993:
=={{header|Go}}==
Using {{libheader|GXUI}} from [https://github.com/google/gxui Github]
<syntaxhighlight lang="go">package main
 
import (
Line 2,111 ⟶ 2,112:
=={{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,147:
=== Alternative solution ===
{{libheader|Gloss}}
<syntaxhighlight lang="haskell">import Graphics.Gloss
 
-- Initial conditions
Line 2,201 ⟶ 2,202:
[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,242:
{{trans|Scheme}}
 
<syntaxhighlight lang=Unicon"unicon">
import gui
$include "guih.icn"
Line 2,339 ⟶ 2,340:
=={{header|J}}==
Works for '''J6'''
<syntaxhighlight lang="j">require 'gl2 trig'
coinsert 'jgl2'
 
Line 2,383 ⟶ 2,384:
pend_run'' NB. run animation</syntaxhighlight>
Updated for changes in '''J8'''
<syntaxhighlight lang="j">require 'gl2 trig'
coinsert 'jgl2'
Line 2,440 ⟶ 2,441:
=={{header|Java}}==
{{libheader|Swing}} {{libheader|AWT}}
<syntaxhighlight lang="java">import java.awt.*;
import javax.swing.*;
 
Line 2,497 ⟶ 2,498:
{{trans|E}} (plus gratuitous motion blur)
 
<syntaxhighlight lang="javascript"><html><head>
<title>Pendulum</title>
</head><body style="background: gray;">
Line 2,563 ⟶ 2,564:
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,608:
 
=={{header|Julia}}==
Differential equation based solution using the Luxor graphics library.<syntaxhighlight lang="julia">using Luxor
using Colors
using BoundaryValueDiffEq
Line 2,657 ⟶ 2,658:
=={{header|Kotlin}}==
Conversion of Java snippet.
<syntaxhighlight lang="scala">import java.awt.*
import java.util.concurrent.*
import javax.swing.*
Line 2,705 ⟶ 2,706:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">nomainwin
WindowWidth = 400
WindowHeight = 300
Line 2,749 ⟶ 2,750:
 
=={{header|Lingo}}==
<syntaxhighlight lang=Lingo"lingo">global RODLEN, GRAVITY, DT
global velocity, acceleration, angle, posX, posY
Line 2,795 ⟶ 2,796:
=={{header|Logo}}==
{{works with|UCB Logo}}
<syntaxhighlight lang="logo">make "angle 45
make "L 1
make "bob 10
Line 2,827 ⟶ 2,828:
=={{header|Lua}}==
Needs L&Ouml;VE 2D Engine
<syntaxhighlight lang="lua">
function degToRad( d )
return d * 0.01745329251
Line 2,859 ⟶ 2,860:
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang=M2000"m2000 Interpreterinterpreter">
Module Pendulum {
back()
Line 2,910 ⟶ 2,911:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">freq = 8; length = freq^(-1/2);
Animate[Graphics[
List[{Line[{{0, 0}, length {Sin[T], -Cos[T]}} /. {T -> (Pi/6) Cos[2 Pi freq t]}], PointSize[Large],
Line 2,919 ⟶ 2,920:
=={{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 ⟶ 2,997:
 
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,110:
 
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,238:
=={{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,280:
Inspired by the E and Ruby versions.
 
<syntaxhighlight lang="oz">declare
[QTk] = {Link ['x-oz://system/wp/QTk.ozf']}
 
Line 3,370 ⟶ 3,371:
This does not have the window resizing handling that Tcl does.
 
<syntaxhighlight lang="perl">
use strict;
use warnings;
Line 3,442 ⟶ 3,443:
{{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,555:
=={{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,571:
(+ 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,626:
=={{header|Prolog}}==
SWI-Prolog has a graphic interface XPCE.
<syntaxhighlight lang=Prolog"prolog">:- use_module(library(pce)).
 
pendulum :-
Line 3,709 ⟶ 3,710:
=={{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,822:
 
{{trans|C}}
<syntaxhighlight lang="python">import pygame, sys
from pygame.locals import *
from math import sin, cos, radians
Line 3,905 ⟶ 3,906:
 
===Python: using tkinter===
<syntaxhighlight lang="python">
''' Python 3.6.5 code using Tkinter graphical user interface.'''
 
Line 3,997 ⟶ 3,998:
 
=={{header|QB64}}==
<syntaxhighlight lang="qbasic">'declare and initialize variables
CONST PI = 3.141592
 
Line 4,096 ⟶ 4,097:
 
=={{header|R}}==
<syntaxhighlight lang=R"r">library(DescTools)
 
pendulum<-function(length=5,radius=1,circle.color="white",bg.color="white"){
Line 4,121 ⟶ 4,122:
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
 
Line 4,148 ⟶ 4,149:
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,246:
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,347:
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project : Animate a pendulum
 
Line 4,458 ⟶ 4,459:
The RLaB script that solves the problem is
 
<syntaxhighlight lang=RLaB"rlab">
//
// example: solve ODE for pendulum
Line 4,532 ⟶ 4,533:
the Tcl pendulum swings noticibly faster.
 
<syntaxhighlight lang="ruby">require 'tk'
 
$root = TkRoot.new("title" => "Pendulum Animation")
Line 4,597 ⟶ 4,598:
 
==={{libheader|Shoes}}===
<syntaxhighlight lang="ruby">Shoes.app(:width => 320, :height => 200) do
@centerX = 160
@centerY = 25
Line 4,656 ⟶ 4,657:
 
==={{libheader|Ruby/Gosu}}===
<syntaxhighlight lang="ruby">#!/bin/ruby
 
begin; require 'rubygems'; rescue; end
Line 4,768 ⟶ 4,769:
 
{{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,831:
=={{header|Scala}}==
{{libheader|Scala}}
<syntaxhighlight lang=Scala"scala">import java.awt.Color
import java.util.concurrent.{Executors, TimeUnit}
 
Line 4,893 ⟶ 4,894:
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,968 ⟶ 4,969:
=={{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,058:
{{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,132:
=={{header|Sidef}}==
{{trans|Perl}}
<syntaxhighlight lang="ruby">require('Tk')
 
var root = %s<MainWindow>.new('-title' => 'Pendulum Animation')
Line 5,194 ⟶ 5,195:
 
=={{header|smart BASIC}}==
<syntaxhighlight lang="smart BASICbasic">'Pendulum
'By Dutchman
' --- constants
Line 5,237 ⟶ 5,238:
{{works with|Tcl|8.5}}
==={{libheader|Tk}}===
<syntaxhighlight lang="tcl">package require Tcl 8.5
package require Tk
 
Line 5,320 ⟶ 5,321:
=={{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,372:
{{libheader|DOME}}
{{libheader|Wren-dynamic}}
<syntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
Line 5,421 ⟶ 5,422:
 
=={{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,460:
 
=={{header|Yabasic}}==
<syntaxhighlight lang=Yabasic"yabasic">clear screen
open window 400, 300
window origin "cc"
Line 5,490 ⟶ 5,491:
{{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,508:
1010 CIRCLE bobx,boby,3
1020 RETURN</syntaxhighlight>
 
 
{{omit from|LFE}}
Line 5,513 ⟶ 5,515:
{{omit from|PHP}}
{{omit from|SQL PL|It does not handle GUI}}
 
[[Category:Animation]]
10,333

edits