home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / MacHaskell 2.2 / progs / demo / X11 / animation / r_behaviour.hs < prev    next >
Encoding:
Text File  |  1994-09-27  |  5.6 KB  |  159 lines  |  [TEXT/YHS2]

  1. {-**********************************************************************
  2.   MODULE R_BEHAVIOUR
  3.  
  4.     This module defines the basic Behaviours available to manipulate
  5.   Movies. These functions can either be used directly, or used to
  6.   easily create personnalized Behaviours (see R_Defaults).
  7.     There are the Behaviours that affect one Movie, which are mov,movto
  8.   circ_mov,scale,scale_rel,rot,flip and set_color. These change some 
  9.   aspect of the movie over time.
  10.     There are functions that combine several movies into one, namely
  11.   bseq,bSeq,bpar and bPar.
  12.     Some functions modify the Behaviours. These are do, rpt and forever.
  13.   They put limits on how long the Behaviour is. 
  14.     Finally, there are the functions that apply the Behaviours to a Movie.
  15.   These are apply and while. Apply applies a Behaviour to a Movie until
  16.   it runs out of Movie or Behaviour. While takes a conditional and
  17.   applies the Behaviour to it until that condition is fullfilled.
  18.  
  19. ***********************************************************************-}
  20.  
  21. module R_Behaviour (mov,movto,circ_mov,scale,scale_rel,rot,flipb,
  22.             set_color,
  23.                bseq,bSeq,bpar,bPar,
  24.             do,rpt,forever,
  25.             apply,while )  where
  26.  
  27. import R_Ptypes
  28. import R_Utility
  29. import R_Picture
  30.  
  31.   -- takes a Pic to Pic and makes an infinite list Behaviour out of it        
  32. makeb1 :: (Pic->Pic) -> Behaviour
  33. makeb1 f = f : makeb1 f
  34.  
  35.   -- takes a movie and flips it around the x-origin using flip_Pic
  36. flipb :: Behaviour
  37. flipb = makeb1 flip_Pic
  38.  
  39.   -- twist makes twist_Pic into a Behaviour, rotating the image by rotunit
  40. twist' :: Behaviour
  41. twist' = makeb1 twist_Pic'
  42.  
  43.   -- makeb2 makes a Behaviour out of a function that takes a list and a 
  44.   -- function and outputs a Behaviour.
  45. makeb2 :: (a->Pic->Pic) -> [a] -> Behaviour
  46. makeb2 f [] = []
  47. makeb2 f (v:vs) = f v : makeb2 f vs
  48.  
  49.   -- mov takes a list of Vec's and applies each Pic-to-Pic in the Behaviour
  50.   -- list to its corresponding Vec, and gives back a new Behaviour
  51. mov :: [Vec] ->Behaviour
  52. mov = makeb2 mov_Pic
  53.  
  54.   -- movto creates a list of Pic-to-Pic Behaviours that move each Pic to its 
  55.   -- corresponding Vec
  56. movto :: [Vec] -> Behaviour
  57. movto = makeb2 movto_Pic
  58.  
  59.   -- produces a Behaviour that produces movement in a circle, taking
  60.   -- the radius and the increment as arguments.
  61. circ_mov :: Float -> Float -> Behaviour
  62. circ_mov r inc = mov (map (vmin' (head vs')) vs')
  63.                     where vs = [ (r*(cos theta),r*(sin theta)) |
  64.                                theta <- gen inc 0.0  ]
  65.                           vmin' x y = vmin y x
  66.                           vs' = map vftov vs
  67.  
  68. gen :: Float -> Float -> [Float]
  69. gen b c = c : (gen b (c+b) )
  70.  
  71.  
  72.   -- scale outputs a list of Pic-to-Pic's that scale according to its 
  73.   -- corresponding Int in the input list
  74. scale :: [Int] -> Behaviour
  75. scale = makeb2 scale_Pic
  76.  
  77.   -- scale_rel does the same thing, but centers on the lower-left corner of
  78.   -- the image
  79. scale_rel :: Vec -> [Int] -> Behaviour
  80. scale_rel v = makeb2 (scale_rel_Pic v)
  81.  
  82.   -- twist outputs a list of Behaviours that rotate each pick by its 
  83.   -- corresponding Float in the list
  84. twist :: [Float] -> Behaviour
  85. twist = makeb2 twist_Pic
  86.  
  87.   -- set_color takes a list of Colors, and returns a list of Pic-to-Pic's
  88.   -- that change to the corresponding color in the list
  89. set_color :: [Color] -> Behaviour
  90. set_color = makeb2 set_Color_Pic
  91.  
  92.   -- makeb3 takes a function with two inputs, and two input lists and
  93.   -- returns a behaviour made up of functions with inputs fromt the lists
  94. makeb3 :: (a->b->Pic->Pic) -> [a] -> [b] -> Behaviour
  95. makeb3 f [] (p:ps) = []
  96. makeb3 f (v:vs) [] = []
  97. makeb3 f (v:vs) (p:ps) = f v p : makeb3 f vs ps
  98.  
  99.   -- rot produces behaviours rotating by the Float, around the point
  100.   -- of the Vec, both provided by lists.
  101. rot :: [Vec] -> [Float] -> Behaviour
  102. rot = makeb3 rot_Pic
  103.  
  104.   -- bseq takes two Behaviours and combines them into one, in sequence. 
  105.   -- It first applies all of the first Behaviour, then all of the second
  106. bseq :: Behaviour -> Behaviour -> Behaviour
  107. bseq ps [] = []
  108. bseq [] qs = []
  109. bseq ps qs = ps ++ (mapc (last ps) qs)
  110.  
  111.   -- bSeq takes a list of Behaviour and makes them into one Behaviour, in
  112.   -- sequence.
  113. bSeq :: [Behaviour] -> Behaviour
  114. bSeq = reduce bseq
  115.  
  116.   -- bpar takes two behaviours and applies them both simultaneously,
  117.   -- producing a list of Pic-to-Pic's, each one made up of a function
  118.   -- from the first list combined with a function from the second list
  119. bpar :: Behaviour -> Behaviour -> Behaviour
  120. bpar [] (q:qs) = []
  121. bpar (p:ps) [] = []
  122. bpar (p:ps) (q:qs) = (p.q):(bpar ps qs)
  123.  
  124.   -- bPar takes a list of Behaviours and makes them all into one Behaviour,
  125.   -- in paralell
  126. bPar :: [Behaviour] -> Behaviour
  127. bPar = reduce bpar
  128.  
  129.   -- takes the first n POic-to-Pics in a Behaviour and returns that Behaviour 
  130. do :: Int -> Behaviour -> Behaviour
  131. do n f = take n f
  132.  
  133.   -- applies bseq to the list of behaviours, so that the nth element of
  134.   -- the returned list has n-1 behaviours in it, applied in sequence
  135. rpt :: Int -> Behaviour -> Behaviour
  136. rpt n f = replicate n bseq [] f
  137.  
  138.   -- takes the behaviour and applies all the behaviours up the nth element
  139.   -- to the nth element, in an infinite list
  140. forever :: Behaviour -> Behaviour
  141. forever f = bseq f (forever f)
  142.  
  143.   -- takes a behaviour, applies each from to a Pic in a Movie and returns
  144.   -- the new Movie
  145. apply :: Behaviour -> Movie -> Movie
  146. apply [] ms = []
  147. apply fs [] = []
  148. apply (f:fs) (m:ms) = (f m):(apply fs ms)
  149.  
  150.   -- applies the Behaviour to the Movie until the condition is fullfilled,
  151.   -- then returns the movie to that point
  152. while :: (Pic -> Bool) -> Behaviour -> Movie -> Movie
  153. while c [] ms = []
  154. while c fs [] = []
  155. while c (f:fs) (m:ms) = if (c m) then ( (f m):(while c fs ms))
  156.                         else []
  157.  
  158.  
  159.