Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
35
Task/Animate-a-pendulum/ERRE/animate-a-pendulum.erre
Normal file
35
Task/Animate-a-pendulum/ERRE/animate-a-pendulum.erre
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
PROGRAM PENDULUM
|
||||
|
||||
!
|
||||
! for rosettacode.org
|
||||
!
|
||||
|
||||
!$KEY
|
||||
|
||||
!$INCLUDE="PC.LIB"
|
||||
|
||||
PROCEDURE PENDULUM(A,L)
|
||||
PIVOTX=320
|
||||
PIVOTY=0
|
||||
BOBX=PIVOTX+L*500*SIN(a)
|
||||
BOBY=PIVOTY+L*500*COS(a)
|
||||
LINE(PIVOTX,PIVOTY,BOBX,BOBY,6,FALSE)
|
||||
CIRCLE(BOBX+24*SIN(A),BOBY+24*COS(A),27,11)
|
||||
PAUSE(0.01)
|
||||
LINE(PIVOTX,PIVOTY,BOBX,BOBY,0,FALSE)
|
||||
CIRCLE(BOBX+24*SIN(A),BOBY+24*COS(A),27,0)
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
SCREEN(9)
|
||||
THETA=40*p/180 ! initial displacement
|
||||
G=9.81 ! acceleration due to gravity
|
||||
L=0.5 ! length of pendulum in metres
|
||||
LINE(0,0,639,0,5,FALSE)
|
||||
LOOP
|
||||
PENDULUM(THETA,L)
|
||||
ACCEL=-G*SIN(THETA)/L/100
|
||||
SPEED=SPEED+ACCEL/100
|
||||
THETA=THETA+SPEED
|
||||
END LOOP
|
||||
END PROGRAM
|
||||
73
Task/Animate-a-pendulum/Elm/animate-a-pendulum.elm
Normal file
73
Task/Animate-a-pendulum/Elm/animate-a-pendulum.elm
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
import Color exposing (..)
|
||||
import Collage exposing (..)
|
||||
import Element exposing (..)
|
||||
import Html exposing (..)
|
||||
import Time exposing (..)
|
||||
import Html.App exposing (program)
|
||||
|
||||
dt = 0.01
|
||||
scale = 100
|
||||
|
||||
type alias Model =
|
||||
{ angle : Float
|
||||
, angVel : Float
|
||||
, length : Float
|
||||
, gravity : Float
|
||||
}
|
||||
|
||||
type Msg
|
||||
= Tick Time
|
||||
|
||||
init : (Model,Cmd Msg)
|
||||
init =
|
||||
( { angle = 3 * pi / 4
|
||||
, angVel = 0.0
|
||||
, length = 2
|
||||
, gravity = -9.81
|
||||
}
|
||||
, Cmd.none)
|
||||
|
||||
update : Msg -> Model -> (Model, Cmd Msg)
|
||||
update _ model =
|
||||
let
|
||||
angAcc = -1.0 * (model.gravity / model.length) * sin (model.angle)
|
||||
angVel' = model.angVel + angAcc * dt
|
||||
angle' = model.angle + angVel' * dt
|
||||
in
|
||||
( { model
|
||||
| angle = angle'
|
||||
, angVel = angVel'
|
||||
}
|
||||
, Cmd.none )
|
||||
|
||||
view : Model -> Html Msg
|
||||
view model =
|
||||
let
|
||||
endPoint = ( 0, scale * model.length )
|
||||
pendulum =
|
||||
group
|
||||
[ segment ( 0, 0 ) endPoint
|
||||
|> traced { defaultLine | width = 2, color = red }
|
||||
, circle 8
|
||||
|> filled blue
|
||||
, ngon 3 10
|
||||
|> filled green
|
||||
|> rotate (pi/2)
|
||||
|> move endPoint
|
||||
]
|
||||
in
|
||||
toHtml <|
|
||||
collage 700 500
|
||||
[ pendulum |> rotate model.angle ]
|
||||
|
||||
subscriptions : Model -> Sub Msg
|
||||
subscriptions _ =
|
||||
Time.every (dt * second) Tick
|
||||
|
||||
main =
|
||||
program
|
||||
{ init = init
|
||||
, view = view
|
||||
, update = update
|
||||
, subscriptions = subscriptions
|
||||
}
|
||||
68
Task/Animate-a-pendulum/Phix/animate-a-pendulum.phix
Normal file
68
Task/Animate-a-pendulum/Phix/animate-a-pendulum.phix
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
include ..\arwen\arwen.ew
|
||||
include ..\arwen\axtra.ew
|
||||
|
||||
constant main = create(Window, "Animated Pendulum", 0, 0, 20, 20, 625, 690, 0),
|
||||
mainDC = getPrivateDC(main),
|
||||
backDC = c_func(xCreateCompatibleDC, {NULL}), -- the background
|
||||
viewDC = c_func(xCreateCompatibleDC, {NULL}), -- with animation
|
||||
grey = #909090
|
||||
|
||||
constant MainTimer = createTimer()
|
||||
|
||||
integer dw = 0, dh = 0 -- client area width and height
|
||||
atom bmBack, bmView
|
||||
integer bmX = 0, bmY = 0 -- actual size of the bitmaps
|
||||
|
||||
constant dt = 1E-3
|
||||
constant g = 50
|
||||
|
||||
integer sX = 0, sY = 0 -- suspension point of pendulum
|
||||
integer len
|
||||
atom alpha = PI/2,
|
||||
omega = 0,
|
||||
epsilon
|
||||
|
||||
function mainHandler(integer id, integer msg, atom wParam, object lParam)
|
||||
integer eX, eY -- moving end of pendulum
|
||||
if msg=WM_SIZE then
|
||||
{{},{},dw,dh} = getClientRect(main)
|
||||
if dw>bmX or dh>bmY then
|
||||
-- we need bigger bitmaps
|
||||
bmBack = c_func(xCreateCompatibleBitmap, {mainDC, dw, dh})
|
||||
{} = deleteObject(selectObject(backDC,bmBack))
|
||||
-- clear the background
|
||||
setPenColor(grey)
|
||||
drawRectangleh(backDC, True, 0, 0, dw, dh)
|
||||
bmView = c_func(xCreateCompatibleBitmap, {mainDC, dw, dh})
|
||||
{} = deleteObject(selectObject(viewDC,bmView))
|
||||
{bmX,bmY} = {dw,dh}
|
||||
end if
|
||||
-- new suspension point and length
|
||||
sX = floor(dw/2)
|
||||
sY = floor(dh/8)
|
||||
len = sX-20
|
||||
elsif msg=WM_PAINT then
|
||||
-- start with a fresh copy of the background
|
||||
void = c_func(xBitBlt,{viewDC,0,0,dw,dh,backDC,0,0,SRCCOPY})
|
||||
eX = floor(len*sin(alpha)+sX)
|
||||
eY = floor(len*cos(alpha)+sY)
|
||||
drawLinesh(viewDC,{Red,{sX,sY,eX,eY},Yellow})
|
||||
drawEllipseh(viewDC,eX-5,eY-5,eX+5,eY+5)
|
||||
void = c_func(xBitBlt,{mainDC,0,0,dw,dh,viewDC,0,0,SRCCOPY})
|
||||
elsif msg=WM_TIMER then
|
||||
epsilon = -len*sin(alpha)*g
|
||||
omega += dt*epsilon
|
||||
alpha += dt*omega
|
||||
repaintWindow(main)
|
||||
elsif msg=WM_SHOWWINDOW then
|
||||
startTimer(MainTimer,main,33)
|
||||
elsif msg=WM_CHAR
|
||||
and wParam=VK_ESCAPE then
|
||||
closeWindow(main)
|
||||
if id or object(lParam) then end if -- suppress warnings
|
||||
end if
|
||||
return 0
|
||||
end function
|
||||
setHandler({main},routine_id("mainHandler"))
|
||||
|
||||
WinMain(main, SW_NORMAL)
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
import <Utilities/Sequence.sl>;
|
||||
import <Utilities/Conversion.sl>;
|
||||
import <Utilities/Math.sl>;
|
||||
|
||||
//region Types
|
||||
|
||||
Point ::= (x: int(0), y: int(0));
|
||||
Color ::= (red: int(0), green: int(0), blue: int(0));
|
||||
Image ::= (kind: char(1), iColor: Color(0), vert1: Point(0), vert2: Point(0), vert3: Point(0), center: Point(0),
|
||||
radius: int(0), height: int(0), width: int(0), message: char(1), source: char(1));
|
||||
Click ::= (clicked: bool(0), clPoint: Point(0));
|
||||
Input ::= (iClick: Click(0), keys: char(1));
|
||||
|
||||
//endregion
|
||||
|
||||
|
||||
//region Helpers======================================================================
|
||||
//region Constructor-Functions-------------------------------------------------
|
||||
point(a(0), b(0)) := (x: a, y: b);
|
||||
color(r(0), g(0), b(0)) := (red: r, green: g, blue: b);
|
||||
segment(e1(0), e2(0), c(0)) := (kind: "segment", vert1: e1, vert2: e2, iColor: c);
|
||||
disc(ce(0), rad(0), c(0)) := (kind: "disc", center: ce, radius: rad, iColor: c);
|
||||
//endregion----------------------------------------------------------------------
|
||||
|
||||
//region Colors----------------------------------------------------------------
|
||||
dBlack := color(0, 0, 0);
|
||||
dYellow := color(255, 255, 0);
|
||||
//endregion----------------------------------------------------------------------
|
||||
//endregion=============================================================================
|
||||
|
||||
|
||||
//=================Easel=Functions=============================================
|
||||
|
||||
State ::= (angle: float(0), angleVelocity: float(0), angleAccel: float(0));
|
||||
|
||||
initialState := (angle: pi/2, angleVelocity: 0.0, angleAccel: 0.0);
|
||||
|
||||
dt := 0.3;
|
||||
length := 450;
|
||||
|
||||
anchor := point(500, 750);
|
||||
|
||||
newState(I(0), S(0)) :=
|
||||
let
|
||||
newAngle := S.angle + newAngleVelocity * dt;
|
||||
newAngleVelocity := S.angleVelocity + newAngleAccel * dt;
|
||||
newAngleAccel := -9.81 / length * sin(S.angle);
|
||||
in
|
||||
(angle: newAngle, angleVelocity: newAngleVelocity, angleAccel: newAngleAccel);
|
||||
|
||||
sounds(I(0), S(0)) := ["ding"] when I.iClick.clicked else [];
|
||||
|
||||
images(S(0)) :=
|
||||
let
|
||||
pendulum := pendulumLocation(S.angle);
|
||||
in
|
||||
[segment(anchor, pendulum, dBlack),
|
||||
disc(pendulum, 30, dYellow),
|
||||
disc(anchor, 5, dBlack)];
|
||||
|
||||
pendulumLocation(angle) :=
|
||||
let
|
||||
x := anchor.x + round(sin(angle) * length);
|
||||
y := anchor.y - round(cos(angle) * length);
|
||||
in
|
||||
point(x, y);
|
||||
|
||||
//=============End=Easel=Functions=============================================
|
||||
61
Task/Animate-a-pendulum/Sidef/animate-a-pendulum.sidef
Normal file
61
Task/Animate-a-pendulum/Sidef/animate-a-pendulum.sidef
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
require('Tk')
|
||||
|
||||
var root = %s<MainWindow>.new('-title' => 'Pendulum Animation')
|
||||
var canvas = root.Canvas('-width' => 320, '-height' => 200)
|
||||
|
||||
canvas.createLine( 0, 25, 320, 25, '-tags' => <plate>, '-width' => 2, '-fill' => :grey50)
|
||||
canvas.createOval(155, 20, 165, 30, '-tags' => <pivot outline>, '-fill' => :grey50)
|
||||
canvas.createLine( 1, 1, 1, 1, '-tags' => <rod width>, '-width' => 3, '-fill' => :black)
|
||||
canvas.createOval( 1, 1, 2, 2, '-tags' => <bob outline>, '-fill' => :yellow)
|
||||
|
||||
canvas.raise(:pivot)
|
||||
canvas.pack('-fill' => :both, '-expand' => 1)
|
||||
var(θ = 45, Δθ = 0, length = 150, homeX = 160, homeY = 25)
|
||||
|
||||
func show_pendulum() {
|
||||
var angle = θ.deg2rad
|
||||
var x = (homeX + length*sin(angle))
|
||||
var y = (homeY + length*cos(angle))
|
||||
canvas.coords(:rod, homeX, homeY, x, y)
|
||||
canvas.coords(:bob, x - 15, y - 15, x + 15, y + 15)
|
||||
}
|
||||
|
||||
func recompute_angle() {
|
||||
var scaling = 3000/(length**2)
|
||||
|
||||
# first estimate
|
||||
var firstΔΔθ = (-sin(θ.deg2rad) * scaling)
|
||||
var midΔθ = (Δθ + firstΔΔθ)
|
||||
var midθ = ((Δθ + midΔθ)/2 + θ)
|
||||
|
||||
# second estimate
|
||||
var midΔΔθ = (-sin(midθ.deg2rad) * scaling)
|
||||
midΔθ = ((firstΔΔθ + midΔΔθ)/2 + Δθ)
|
||||
midθ = ((Δθ + midΔθ)/2 + θ)
|
||||
|
||||
# again, first
|
||||
midΔΔθ = (-sin(midθ.deg2rad) * scaling)
|
||||
var lastΔθ = (midΔθ + midΔΔθ)
|
||||
var lastθ = ((midΔθ + lastΔθ)/2 + midθ)
|
||||
|
||||
# again, second
|
||||
var lastΔΔθ = (-sin(lastθ.deg2rad) * scaling)
|
||||
lastΔθ = ((midΔΔθ + lastΔΔθ)/2 + midΔθ)
|
||||
lastθ = ((midΔθ + lastΔθ)/2 + midθ)
|
||||
|
||||
# Now put the values back in our globals
|
||||
Δθ = lastΔθ
|
||||
θ = lastθ
|
||||
}
|
||||
|
||||
func animate(Ref id) {
|
||||
recompute_angle()
|
||||
show_pendulum()
|
||||
*id = root.after(15 => { animate(id) })
|
||||
}
|
||||
|
||||
show_pendulum()
|
||||
var after_id = root.after(500 => { animate(\after_id) })
|
||||
|
||||
canvas.bind('<Destroy>' => { after_id.cancel })
|
||||
%S<Tk>.MainLoop()
|
||||
Loading…
Add table
Add a link
Reference in a new issue