A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
41
Task/Animate-a-pendulum/Factor/animate-a-pendulum.factor
Normal file
41
Task/Animate-a-pendulum/Factor/animate-a-pendulum.factor
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
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 ;
|
||||
IN: pendulum
|
||||
|
||||
CONSTANT: g 9.81
|
||||
CONSTANT: l 20
|
||||
CONSTANT: theta0 0.5
|
||||
|
||||
: current-time ( -- time ) nano-count -9 10^ * ;
|
||||
|
||||
: T0 ( -- T0 ) 2 pi l g / sqrt * * ;
|
||||
: omega0 ( -- omega0 ) 2 pi * T0 / ;
|
||||
: theta ( -- theta ) current-time omega0 * cos theta0 * ;
|
||||
|
||||
: relative-xy ( theta l -- xy )
|
||||
[ [ sin ] [ cos ] bi ]
|
||||
[ [ * ] curry ] bi* bi@ 2array ;
|
||||
: theta-to-xy ( origin theta l -- xy ) relative-xy v+ ;
|
||||
|
||||
TUPLE: pendulum-gadget < gadget alarm ;
|
||||
|
||||
: O ( gadget -- origin ) rect-bounds [ drop ] [ first 2 / ] bi* 0 2array ;
|
||||
: window-l ( gadget -- l ) rect-bounds [ drop ] [ second ] bi* ;
|
||||
: gadget-xy ( gadget -- xy ) [ O ] [ drop theta ] [ window-l ] tri theta-to-xy ;
|
||||
|
||||
M: pendulum-gadget draw-gadget*
|
||||
COLOR: black gl-color
|
||||
[ O ] [ gadget-xy ] bi gl-line ;
|
||||
|
||||
M:: pendulum-gadget graft* ( gadget -- )
|
||||
[ gadget relayout-1 ]
|
||||
20 milliseconds every gadget (>>alarm) ;
|
||||
M: pendulum-gadget ungraft* alarm>> cancel-alarm ;
|
||||
|
||||
: <pendulum-gadget> ( -- gadget )
|
||||
pendulum-gadget new
|
||||
{ 500 500 } >>pref-dim ;
|
||||
: pendulum-main ( -- )
|
||||
[ <pendulum-gadget> "pendulum" open-window ] with-ui ;
|
||||
MAIN: pendulum-main
|
||||
33
Task/Animate-a-pendulum/HicEst/animate-a-pendulum-1.hicest
Normal file
33
Task/Animate-a-pendulum/HicEst/animate-a-pendulum-1.hicest
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
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
|
||||
|
||||
|
||||
Theta = (1, 0) ! initial angle and velocity
|
||||
start_t = TIME()
|
||||
|
||||
DO i = 1, 1E100 ! "forever"
|
||||
end_t = TIME() ! to integrate in real-time sections:
|
||||
DIFFEQ(Callback="pendulum", T=end_t, Y=Theta, DY=dTheta, T0=start_t)
|
||||
xBob = (SIN(Theta(1)) + 1) / 2
|
||||
yBob = COS(Theta(1)) - dBob
|
||||
|
||||
! create or clear window and draw pendulum bob at (xBob, yBob):
|
||||
WINDOW(WIN=wh, LeftSpace=0, RightSpace=0, TopSpace=0, BottomSpace=0, Up=999)
|
||||
BobMargins = (xBob-dBob, 1-xBob-dBob, yBob-dBob, 1-yBob-dBob)
|
||||
WINDOW(WIN=wh, LeftSpace=ls, RightSpace=rs, TopSpace=ts, BottomSpace=bs)
|
||||
WRITE(WIN=wh, DeCoRation='EL=4, BC=4') ! flooded red ellipse as bob
|
||||
|
||||
! draw the rod hanging from the center of the window:
|
||||
WINDOW(WIN=wh, LeftSpace=0.5, TopSpace=0, RightSpace=rs+dBob)
|
||||
WRITE(WIN=wh, DeCoRation='LI=0 0; 1 1, FC=4.02') ! red pendulum rod
|
||||
|
||||
SYSTEM(WAIT=msec)
|
||||
start_t = end_t
|
||||
ENDDO
|
||||
|
||||
END
|
||||
|
||||
SUBROUTINE pendulum ! Theta" = - (g/Lrod) * SIN(Theta)
|
||||
dTheta(1) = Theta(2) ! Theta' = Theta(2) substitution
|
||||
dTheta(2) = -g/Lrod*SIN(Theta(1)) ! Theta" = Theta(2)' = -g/Lrod*SIN(Theta(1))
|
||||
END
|
||||
93
Task/Animate-a-pendulum/HicEst/animate-a-pendulum-2.hicest
Normal file
93
Task/Animate-a-pendulum/HicEst/animate-a-pendulum-2.hicest
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
import gui
|
||||
$include "guih.icn"
|
||||
|
||||
# some constants to define the display and pendulum
|
||||
$define HEIGHT 400
|
||||
$define WIDTH 500
|
||||
$define STRING_LENGTH 200
|
||||
$define HOME_X 250
|
||||
$define HOME_Y 21
|
||||
$define SIZE 30
|
||||
$define START_ANGLE 80
|
||||
|
||||
class WindowApp : Dialog ()
|
||||
|
||||
# draw the pendulum on given context_window, at position (x,y)
|
||||
method draw_pendulum (x, y)
|
||||
# reference to current screen area to draw on
|
||||
cw := Clone(self.cwin)
|
||||
|
||||
# clear screen
|
||||
WAttrib (cw, "bg=grey")
|
||||
EraseRectangle (cw, 0, 0, WIDTH, HEIGHT)
|
||||
|
||||
# draw the display
|
||||
WAttrib (cw, "fg=dark gray")
|
||||
DrawLine (cw, 10, 20, WIDTH-20, 20)
|
||||
WAttrib (cw, "fg=black")
|
||||
DrawLine (cw, HOME_X, HOME_Y, x, y)
|
||||
FillCircle (cw, x, y, SIZE+2)
|
||||
WAttrib (cw, "fg=yellow")
|
||||
FillCircle (cw, x, y, SIZE)
|
||||
|
||||
# free reference to screen area
|
||||
Uncouple (cw)
|
||||
end
|
||||
|
||||
# find the average of given two arguments
|
||||
method avg (a, b)
|
||||
return (a + b) / 2
|
||||
end
|
||||
|
||||
# this method gets called by the ticker
|
||||
# it computes the next position of the pendulum and
|
||||
# requests a redraw
|
||||
method tick ()
|
||||
static x, y
|
||||
static theta := START_ANGLE
|
||||
static d_theta := 0
|
||||
# update x,y of pendulum
|
||||
scaling := 3000.0 / (STRING_LENGTH * STRING_LENGTH)
|
||||
# -- first estimate
|
||||
first_dd_theta := -(sin (dtor (theta)) * scaling)
|
||||
mid_d_theta := d_theta + first_dd_theta
|
||||
mid_theta := theta + avg (d_theta, mid_d_theta)
|
||||
# -- second estimate
|
||||
mid_dd_theta := - (sin (dtor (mid_theta)) * scaling)
|
||||
mid_d_theta_2 := d_theta + avg (first_dd_theta, mid_dd_theta)
|
||||
mid_theta_2 := theta + avg (d_theta, mid_d_theta_2)
|
||||
# -- again first
|
||||
mid_dd_theta_2 := -(sin (dtor (mid_theta_2)) * scaling)
|
||||
last_d_theta := mid_d_theta_2 + mid_dd_theta_2
|
||||
last_theta := mid_theta_2 + avg (mid_d_theta_2, last_d_theta)
|
||||
# -- again second
|
||||
last_dd_theta := - (sin (dtor (last_theta)) * scaling)
|
||||
last_d_theta_2 := mid_d_theta_2 + avg (mid_dd_theta_2, last_dd_theta)
|
||||
last_theta_2 := mid_theta_2 + avg (mid_d_theta_2, last_d_theta_2)
|
||||
# -- update stored angles
|
||||
d_theta := last_d_theta_2
|
||||
theta := last_theta_2
|
||||
# -- update x, y
|
||||
pendulum_angle := dtor (theta)
|
||||
x := HOME_X + STRING_LENGTH * sin (pendulum_angle)
|
||||
y := HOME_Y + STRING_LENGTH * cos (pendulum_angle)
|
||||
|
||||
# draw pendulum
|
||||
draw_pendulum (x, y)
|
||||
end
|
||||
|
||||
# set up the window
|
||||
method component_setup ()
|
||||
# some cosmetic settings for the window
|
||||
attrib("size="||WIDTH||","||HEIGHT, "bg=light gray", "label=Pendulum")
|
||||
# make sure we respond to window close event
|
||||
connect (self, "dispose", CLOSE_BUTTON_EVENT)
|
||||
# start the ticker, to update the display periodically
|
||||
self.set_ticker (20)
|
||||
end
|
||||
end
|
||||
|
||||
procedure main ()
|
||||
w := WindowApp ()
|
||||
w.show_modal ()
|
||||
end
|
||||
44
Task/Animate-a-pendulum/J/animate-a-pendulum.j
Normal file
44
Task/Animate-a-pendulum/J/animate-a-pendulum.j
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
require 'gl2 trig'
|
||||
coinsert 'jgl2'
|
||||
|
||||
DT =: %30 NB. seconds
|
||||
ANGLE=: 0.25p1 NB. radians
|
||||
L =: 1 NB. metres
|
||||
G =: 9.80665 NB. ms_2
|
||||
VEL =: 0 NB. ms_1
|
||||
|
||||
PEND=: noun define
|
||||
pc pend;pn "Pendulum";
|
||||
xywh 0 0 320 200;cc isi isigraph rightmove bottommove;
|
||||
pas 0 0;pcenter;
|
||||
rem form end;
|
||||
)
|
||||
|
||||
pend_run =: verb def ' wd PEND,'';pshow;timer '',":DT * 1000 '
|
||||
pend_close =: verb def ' wd ''timer 0; pclose'' '
|
||||
pend_isi_paint=: verb def ' drawPendulum ANGLE '
|
||||
|
||||
sys_timer_z_=: verb define
|
||||
recalcAngle ''
|
||||
wd 'psel pend; setinvalid isi'
|
||||
)
|
||||
|
||||
recalcAngle=: verb define
|
||||
accel=. - (G % L) * sin ANGLE
|
||||
VEL =: VEL + accel * DT
|
||||
ANGLE=: ANGLE + VEL * DT
|
||||
)
|
||||
|
||||
drawPendulum=: verb define
|
||||
width=. {. glqwh''
|
||||
ps=. (-: width) , 40
|
||||
pe=. ps + 280 <.@* (cos , sin) 0.5p1 + y NB. adjust orientation
|
||||
glrgb 91 91 91
|
||||
glbrush''
|
||||
gllines ps , pe
|
||||
glellipse (,~ ps - -:) 40 15
|
||||
glellipse (,~ pe - -:) 20 20
|
||||
glrect 0 0 ,width, 40
|
||||
)
|
||||
|
||||
pend_run'' NB. run animation
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
nomainwin
|
||||
WindowWidth = 400
|
||||
WindowHeight = 300
|
||||
|
||||
open "Pendulum" for graphics_nsb_nf as #main
|
||||
#main "down;fill white; flush"
|
||||
#main "color black"
|
||||
#main "trapclose [quit.main]"
|
||||
|
||||
Angle = asn(1)
|
||||
DeltaT = 0.1
|
||||
PendLength = 150
|
||||
FixX = int(WindowWidth / 2)
|
||||
FixY = 40
|
||||
|
||||
timer 30, [swing]
|
||||
|
||||
wait
|
||||
|
||||
[swing]
|
||||
|
||||
#main "cls"
|
||||
#main "discard"
|
||||
|
||||
PlumbobX = FixX + int(sin(Angle) * PendLength)
|
||||
PlumbobY = FixY + int(cos(Angle) * PendLength)
|
||||
AngAccel = -9.81 / PendLength * sin(Angle)
|
||||
AngVelocity = AngVelocity + AngAccel * DeltaT
|
||||
Angle = Angle + AngVelocity * DeltaT
|
||||
|
||||
#main "backcolor black"
|
||||
#main "place ";FixX;" ";FixY
|
||||
#main "circlefilled 3"
|
||||
#main "line ";FixX;" ";FixY;" ";PlumbobX;" ";PlumbobY
|
||||
#main "backcolor red"
|
||||
#main "circlefilled 10"
|
||||
|
||||
wait
|
||||
|
||||
[quit.main]
|
||||
close #main
|
||||
end
|
||||
29
Task/Animate-a-pendulum/Logo/animate-a-pendulum.logo
Normal file
29
Task/Animate-a-pendulum/Logo/animate-a-pendulum.logo
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
make "angle 45
|
||||
make "L 1
|
||||
make "bob 10
|
||||
|
||||
to draw.pendulum
|
||||
clearscreen
|
||||
seth :angle+180 ; down on screen is 180
|
||||
forward :L*100-:bob
|
||||
penup
|
||||
forward :bob
|
||||
pendown
|
||||
arc 360 :bob
|
||||
end
|
||||
|
||||
make "G 9.80665
|
||||
make "dt 1/30
|
||||
make "acc 0
|
||||
make "vel 0
|
||||
|
||||
to step.pendulum
|
||||
make "acc -:G / :L * sin :angle
|
||||
make "vel :vel + :acc * :dt
|
||||
make "angle :angle + :vel * :dt
|
||||
wait :dt*60
|
||||
draw.pendulum
|
||||
end
|
||||
|
||||
hideturtle
|
||||
until [key?] [step.pendulum]
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
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],
|
||||
Point[{length {Sin[T], -Cos[T]}} /. {T -> (Pi/6) Cos[2 Pi freq t]}]}],
|
||||
PlotRange -> {{-0.3, 0.3}, {-0.5, 0}}], {t, 0, 1}, AnimationRate -> 0.07]
|
||||
Loading…
Add table
Add a link
Reference in a new issue