Pages

Friday 15 February 2013

Functional Programming Code

 
-----------------------------------------------------------------------------
-- |
-- Module      :  MyAnimation
-- Copyright   :  (c) Michael Lashley 2012
-- License     :  NA
-- Maintainer  :  Michael Lashley
-- Stability   :  experimental
-- Portability :  portable
--
-- A animation called "The Impossible".
-- This animation is a series of squares which change scale while spining
-- around a central orbit/gravitational pull.
--
-----------------------------------------------------------------------------
 
module MyAnimation where
 
-- Import libraries functions from the Animation .hs file.
 
import Animation
 
-- The spin function takes two inputs of type int and returns an animation.
-- This function applies functions from the Animation library to each rect object created by the list in the Picture function.
 
spin :: Int -> Int -> Animation
spin i j =
  rotate (spinner k)
   (rotate (ever z)
    (translate (ever (-x/2, -x/2))
     (withGenPaint (cycleSmooth (x/10) [c, yellow, lime, purple, cyan, grey, olive]) (cycleSmooth 7 [0.2, 1])
      (rect (cycleSmooth 10 [x/2, 1.5]) (cycleSmooth 5 [6*x, 20])))))

 where
    x = fromIntegral j
    c = hsl (fromIntegral i * 45) 1 0.5
    z = fromIntegral(i `mod` 2) * 45
    k = -10 + 0.5*fromIntegral i
 
-- The picture function returns and animation.
-- This function calls the Spin function in a list compression with defined variables i and x.
 
picture :: Animation
picture =

 withPaint (ever black)
   (rect (ever 800) (ever 600)) `plus`

 translate (ever (400, 300))
 (combine [spin i x | i <- [0..16], x <- [350, 150..50]])
 
-- "test" is a function that writes/creates a .svg file when called.
-- The test.svg file invokes the Picture function there for starting the animation.
 
test :: IO ()
test = writeFile "test.svg" (svg 800 600 picture)