Chapter 13 A Module of Simple Animations. Motivation  In the abstract, an animation is a continuous, time-varying image.  But in practice, it is a sequence.

Slides:



Advertisements
Similar presentations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Advertisements

Methods Java 5.1 A quick overview of methods
Microsoft® Small Basic
Cse536 Functional Programming 1 1/13/2015 Lecture #13, Nov. 08, 2004 Today’s Topics –Simple animations –Buffered graphics –Animations in Haskell –Complex.
Building Java Programs
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 13 Fall 2010.
Chapter 8 A Module of Regions. The Region Data Type  A region represents an area on the two-dimensional Cartesian plane.  It is represented by a tree-like.
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
Copyright © 2009 Pearson Education, Inc. CHAPTER 6: The Trigonometric Functions 6.1The Trigonometric Functions of Acute Angles 6.2Applications of Right.
Chapter 19 An Imperative Robot Language. Motivation  In the previous chapter, monads were introduced.  In particular, state monads were described as.
Graphs Transformation of Sine and Cosine
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
Chapter 3 Simple Graphics. Side Effects and Haskell  All the programs we have seen so far have no “side-effects.” That is, programs are executed only.
Cse536 Functional Programming 1 6/11/2015 Lecture #5, Oct 11, 2004 Reading Assignments –Finish Chapter 3, Simple Graphics –Read Chapter 4, Shapes II, Drawing.
Cse536 Functional Programming 1 6/12/2015 Lecture #11, Nov. 01, 2004 Special Guest lecture by Tom Harke Today’s Topics –The Haskell Class system –Instance.
Chapter 17 Rendering Reactive Animations. Motivation  In the previous chapter we learned just enough about advanced IO and concurrency to develop a “renderer”
Chapter 4 Shapes II: Drawing Shapes. Recall the Shape Datatype data Shape = Rectangle Side Side | Ellipse Radius Radius | RtTriangle Side Side | Polygon.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
Lec 5 Feb 10 Goals: analysis of algorithms (continued) O notation summation formulas maximum subsequence sum problem (Chapter 2) three algorithms image.
Advanced Programming Handout 10 A Module of Simple Animations (SOE Chapter 13)
Chapter 15 A Module of Reactive Animations. Motivation  The design of animations in Chapter 13 is elegant, and in fact has the feel of a small domain-specific.
Chapter 5 Polymorphic and Higher-Order Functions.
Cse536 Functional Programming 1 6/30/2015 Lecture #16, Nov. 29, 2004 Todays Topics – Files, Channels, and Handles –IO exception handling –First Class Channels.
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
Chapter 12 Qualified Types. Motivation  What should the principal type of (+) be? Int -> Int -> Int-- too specific a -> a -> a-- too general  It seems.
Chapter 6 Color Image Processing Chapter 6 Color Image Processing.
Cse536 Functional Programming 1 7/16/2015 Lecture #15, Nov. 15, 2004 Todays Topics – Simple Animations - Review –Reactive animations –Vocabulary – Examples.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Operators, Functions and Modules1 Pattern Matching & Recursion.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Introduction to Flash. Topics What is Flash? What can you do with it? Simple animation Complex interactive web application, such as an online store. Starting.
Case Study - Fractions Timothy Budd Oregon State University.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
Prepared by: Elsy Torres Shajida Berry Siobhan Westby.
Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with.
Overview of the Haskell 98 Programming Language
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
More Design Patterns From: Shalloway & Trott, Design Patterns Explained, 2 nd ed.
OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.
1 © 2010 Pearson Education, Inc. All rights reserved © 2010 Pearson Education, Inc. All rights reserved Chapter 5 Analytic Trigonometry.
FRACTIONS & SHAPES BY:. How many of these are colored red? * out of *.
Computer Science 112 Fundamentals of Programming II.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
8 Copyright © Cengage Learning. All rights reserved. Analytic Trigonometry.
1 © 2011 Pearson Education, Inc. All rights reserved 1 © 2010 Pearson Education, Inc. All rights reserved © 2011 Pearson Education, Inc. All rights reserved.
Haskell. GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Introduction to Python Developed by Dutch programmer Guido van Rossum Named after Monty Python Open source development project Simple, readable language.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Types CSCE 314 Spring 2016.
Haskell.
AFP - Lecture 2 Domain Specific Embedded Languages
Lecture 2 Domain Specific Embedded Languages
Maths Games ©Copyright
Can perform actions and provide communication
Lecture 18 Infinite Streams and
Can perform actions and provide communication
(early slides assume map/filter)
Type & Typeclass Syntax in function
Color Image Processing
CHAPTER 3: String And Numeric Data In Python
Reactive Animations Todays Topics
Simple Animations Today’s Topics Reading Assignment Simple animations
Chapter 1 Vocabulary Sections1-1,1-2,1-3
Presentation transcript:

Chapter 13 A Module of Simple Animations

Motivation  In the abstract, an animation is a continuous, time-varying image.  But in practice, it is a sequence of static images displayed in succession so rapidly that it looks continuous.  Our goal is to present to the programmer an abstract view that hides the practical details.  In addition, we will generalize animations to be continuous, time-varying quantities of any value, not just images.

Representing Animations  As usual, we will use our most powerful tool – a function – to represent animations: type Animation a = Time -> a type Time = Float  Examples: rubberBall :: Animation Shape rubberBall t = Ellipse (sin t) (cos t) revolvingBall :: Animation Region revolvingBall t =let ball = Shape (Ellipse ) in Translate (sin t, cos t) ball planets :: Animation Picture planets t = letp1 = Region Red (Shape (rubberBall t)) p2 = Region Yellow (revolvingBall t) in p1 `Over` p2 tellTime :: Animation String tellTime t = "The time is: " ++ show t

An Animator  Suppose we had a function: animate :: String -> Animation Graphic -> IO ( )  We could then execute (i.e. display) the previous animations. For example: main1 :: IO ( ) main1 = animate "Animated Shape“ (withColor Blue. shapeToGraphic. rubberBall) main2 :: IO ( ) main2 = animate "Animated Text“ (text (100,200). tellTime)

Definition of “animate” animate :: String -> Animation Graphic -> IO ( ) animate title anim = runGraphics ( do w <- openWindowEx title (Just (0,0)) (Just (xWin,yWin)) drawBufferedGraphic (Just 30) t0 <- timeGetTime let loop = do t <- timeGetTime let ft = intToFloat (word32ToInt (t-t0)) / 1000 setGraphic w (anim ft) getWindowTick w loop loop ) See text for details.

Common Operations  We can define many operations on animations based on the underlying type. For example, for Pictures: emptyA :: Animation Picture emptyA t = EmptyPic overA :: Animation Picture -> Animation Picture -> Animation Picture overA a1 a2 t = a1 t `Over` a2 t overManyA :: [Animation Picture] -> Animation Picture overManyA = foldr overA emptyA  We can do a similar thing for Shapes, etc.  Also, for numeric animations, we could define functions like addA, multA, and so on.  In both case, however, there is a better solution: use type classes. But first: newtype Behavior a = Beh (Time -> a)

Numeric Animations instance Num a => Num (Behavior a) where (+) = lift2 (+) (*) = lift2 (*) negate = lift1 negate abs = lift1 abs signum = lift1 signum fromInteger = lift0. fromInteger instance Fractional a => Fractional (Behavior a) where (/) = lift2 (/) fromRational = lift0. fromRational Where the lifting functions are defined by: lift0 :: a -> Behavior a lift0 x = Beh (\t -> x) lift1 :: (a -> b) -> (Behavior a -> Behavior b) lift1 f (Beh a) = Beh (\t -> f (a t)) lift2 :: (a -> b -> c) -> (Behavior a -> Behavior b -> Behavior c) lift2 g (Beh a) (Beh b) = Beh (\t -> g (a t) (b t)) instance Floating a => Floating (Behavior a) where pi = lift0 pi sqrt = lift1 sqrt exp = lift1 exp log = lift1 log sin = lift1 sin cos = lift1 cos tan = lift1 tan etc.

Type Class Magic  Furthermore, define time by: time :: Behavior Time time = Beh (\t -> t)  For example, consider “time + 42”: time + 42  unfold overloadings for time, (+), and 42 (lift2 (+)) (Beh (\t -> t)) (Beh (\t -> 42))  unfold lift2 (\ (Beh a) (Beh b) -> Beh (\t -> a t + b t) ) (Beh (\t -> t)) (Beh (\t -> 42))  unfold anonymous function Beh (\t -> (\t -> t) t + (\t -> 42) t )  unfold two anonymous functions Beh (\t -> t + 42)  The magic of type classes!!

New Type Classes  In addition to using existing type classes such as Num, we can define new ones. For example: class Combine a where empty:: a over :: a -> a -> a instance Combine Picture where empty = EmptyPic over = Over instance Combine a => Combine (Behavior a) where empty = lift0 empty over = lift2 over overMany :: Combine a => [a] -> a overMany = foldr over empty

Hiding More Detail  We have not yet hidden all the “practical” detail – in particular, time itself.  But through more aggressive lifting: reg = lift2 Region shape = lift1 Shape ell = lift2 Ellipse red = lift0 Red yellow = lift0 Yellow translate (Beh a1, Beh a2) (Beh r) -- note complexity here = Beh (\t -> Translate (a1 t, a2 t) (r t)) we can redefine the red revolving ball as follows: revolvingBallB :: Behavior Picture revolvingBallB = let ball = shape (ell ) in reg red (translate (sin time, cos time) ball)

More Liftings  Comparison operators: (>*) :: Ord a => Behavior a -> Behavior a -> Behavior Bool (>*) = lift2 (>)  Conditional behaviors: ifFun :: Bool -> a -> a -> a ifFun p c a = if p then c else a cond :: Behavior Bool -> Behavior a -> Behavior a -> Behavior a cond = lift3 ifFun  For example, a flashing color: flash :: Behavior Color flash = cond (sin time >* 0) red yellow

Time Travel  A function for translating a behavior through time: timeTrans :: Behavior Time -> Behavior a -> Behavior a timeTrans (Beh f) (Beh a) = Beh (a. f)  For example: timeTrans (2*time) anim-- double speed timeTrans (5+time) anim `over` anim-- one anim 5 sec behind another timeTrans (negate time) anim-- go backwards  Any kind of animation can be time transformed: flashingBall :: Behavior Picture flashingBall = let ball = shape (ell ) in reg (timeTrans (8*time) flash) (translate (sin time, cos time) ball)

Final Example revolvingBalls :: Behavior Picture revolvingBalls = overMany [ timeTrans (time + t*pi/4) flashingBall | t <- map lift0 [0..7] ] See the text for one other example: a kaleidoscope program.