Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
28
Task/Animate-a-pendulum/Haskell/animate-a-pendulum-1.hs
Normal file
28
Task/Animate-a-pendulum/Haskell/animate-a-pendulum-1.hs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import Graphics.HGL.Draw.Monad (Graphic, )
|
||||
import Graphics.HGL.Draw.Picture
|
||||
import Graphics.HGL.Utils
|
||||
import Graphics.HGL.Window
|
||||
import Graphics.HGL.Run
|
||||
|
||||
import Control.Exception (bracket, )
|
||||
import Control.Arrow
|
||||
|
||||
toInt = fromIntegral.round
|
||||
|
||||
pendulum = runGraphics $
|
||||
bracket
|
||||
(openWindowEx "Pendulum animation task" Nothing (600,400) DoubleBuffered (Just 30))
|
||||
closeWindow
|
||||
(\w -> mapM_ ((\ g -> setGraphic w g >> getWindowTick w).
|
||||
(\ (x, y) -> overGraphic (line (300, 0) (x, y))
|
||||
(ellipse (x - 12, y + 12) (x + 12, y - 12)) )) pts)
|
||||
where
|
||||
dt = 1/30
|
||||
t = - pi/4
|
||||
l = 1
|
||||
g = 9.812
|
||||
nextAVT (a,v,t) = (a', v', t + v' * dt) where
|
||||
a' = - (g / l) * sin t
|
||||
v' = v + a' * dt
|
||||
pts = map (\(_,t,_) -> (toInt.(300+).(300*).cos &&& toInt. (300*).sin) (pi/2+0.6*t) )
|
||||
$ iterate nextAVT (- (g / l) * sin t, t, 0)
|
||||
51
Task/Animate-a-pendulum/Haskell/animate-a-pendulum-2.hs
Normal file
51
Task/Animate-a-pendulum/Haskell/animate-a-pendulum-2.hs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import Graphics.Gloss
|
||||
|
||||
-- Initial conditions
|
||||
g_ = (-9.8) :: Float --Gravity acceleration
|
||||
v_0 = 0 :: Float --Initial tangential speed
|
||||
a_0 = 0 / 180 * pi :: Float --Initial angle
|
||||
dt = 0.01 :: Float --Time step
|
||||
t_f = 15 :: Float --Final time for data logging
|
||||
l_ = 200 :: Float --Rod length
|
||||
|
||||
-- Define a type to represent the pendulum:
|
||||
type Pendulum = (Float, Float, Float) -- (rod length, tangential speed, angle)
|
||||
|
||||
-- Pendulum's initial state
|
||||
initialstate :: Pendulum
|
||||
initialstate = (l_, v_0, a_0)
|
||||
|
||||
-- Step funtion: update pendulum to new position
|
||||
movePendulum :: Float -> Pendulum -> Pendulum
|
||||
movePendulum dt (l,v,a) = ( l , v_2 , a + v_2 / l * dt*10 )
|
||||
where v_2 = v + g_ * (cos a) * dt
|
||||
|
||||
-- Convert from Pendulum to [Picture] for display
|
||||
renderPendulum :: Pendulum -> [Picture]
|
||||
renderPendulum (l,v,a) = map (uncurry Translate newOrigin)
|
||||
[ Line [ ( 0 , 0 ) , ( l * (cos a), l * (sin a) ) ]
|
||||
, polygon [ ( 0 , 0 ) , ( -5 , 8.66 ) , ( 5 , 8.66 ) ]
|
||||
, Translate ( l * (cos a)) (l * (sin a)) (circleSolid (0.04*l_))
|
||||
, Translate (-1.1*l) (-1.3*l) (Scale 0.1 0.1 (Text currSpeed))
|
||||
, Translate (-1.1*l) (-1.3*l + 20) (Scale 0.1 0.1 (Text currAngle))
|
||||
]
|
||||
where currSpeed = "Speed (pixels/s) = " ++ (show v)
|
||||
currAngle = "Angle (deg) = " ++ (show ( 90 + a / pi * 180 ) )
|
||||
|
||||
-- New origin to beter display the animation
|
||||
newOrigin = (0, l_ / 2)
|
||||
|
||||
-- Calcule a proper window size (for angles between 0 and -pi)
|
||||
windowSize :: (Int, Int)
|
||||
windowSize = ( 300 + 2 * round (snd newOrigin)
|
||||
, 200 + 2 * round (snd newOrigin) )
|
||||
|
||||
-- Run simulation
|
||||
main :: IO ()
|
||||
main = do --plotOnGNU
|
||||
simulate window background fps initialstate render update
|
||||
where window = InWindow "Animate a pendulum" windowSize (40, 40)
|
||||
background = white
|
||||
fps = round (1/dt)
|
||||
render xs = pictures $ renderPendulum xs
|
||||
update _ = movePendulum
|
||||
Loading…
Add table
Add a link
Reference in a new issue