Simple Animations Today’s Topics Reading Assignment Simple animations

Slides:



Advertisements
Similar presentations
Cse536 Functional Programming 1 1/13/2015 Lecture #13, Nov. 08, 2004 Today’s Topics –Simple animations –Buffered graphics –Animations in Haskell –Complex.
Advertisements

COP 2800 Lake Sumter State College Mark Wilson, Instructor.
This terms course Last term we both worked on learning 2 things –Processing –The concepts of graphics etc. This term will focus more on the basic concepts.
Haskell Chapter 7. Topics  Defining data types  Exporting types  Type parameters  Derived instances  Type synonyms  Either  Type classes  Not.
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.
Cse536 Functional Programming 1 6/10/2015 Lecture #6, Oct 13, 2004 Reading Assignments –Read chapter 5 of the Text Polymorphic and Higher-Order Functions.
Cse536 Functional Programming 1 6/10/2015 Lecture 10, Oct. 29, 2004 Special Rescheduled Friday Lecture Today’s Topics – Pictures – Low level graphics bit-maps:
Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying.
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”
Advanced Programming Handout 8 Drawing Regions (SOE Chapter 10)
Chapter 4 Shapes II: Drawing Shapes. Recall the Shape Datatype data Shape = Rectangle Side Side | Ellipse Radius Radius | RtTriangle Side Side | Polygon.
Chapter 10 Drawing Regions. Pictures  Drawing Pictures Pictures are composed of Regions o Regions are composed of shapes Pictures add Color data Picture.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
Advanced Programming Handout 4. Introductions  Me: Benjamin C. Pierce (known as Benjamin, or, if you prefer, Dr. Pierce, but not Ben or Professor) 
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.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
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.
FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language.
Advanced Programming Handout 5 Recursive Data Types (SOE Chapter 7)
Chapter 12 Qualified Types. Motivation  What should the principal type of (+) be? Int -> Int -> Int-- too specific a -> a -> a-- too general  It seems.
Introduction to Matlab. Entering Commands Constants and Functions >> pi ans = >> eps ans = e-016 >> sin(pi/2) ans = 1 >> log(1000) ans =
1 Python Chapter 2 © Samuel Marateck, After you install the compiler, an icon labeled IDLE (Python GUI) will appear on the screen. If you click.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
2 What is pyGame? A set of Python modules to make it easier to write games. –home page: –documentation:
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
Higher Computing Computer Systems S. McCrossan 1 Higher Grade Computing Studies 1. Data Representation Data Representation – Why do we use binary? simplicity,
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Modules and Decomposition UW CSE 190p Summer 2012 download examples from the calendar.
Loops & Graphics IP 10 Mr. Mellesmoen Recall Earlier we wrote a program listing numbers from 1 – 24 i=1 start: TextWindow.WriteLine(i) i=i+1 If.
Digital Media Dr. Jim Rowan ITEC So far… We have compared bitmapped graphics and vector graphics We have discussed bitmapped images, some file formats.
MA/CS 375 Fall 2002 Lecture 3. Example 2 A is a matrix with 3 rows and 2 columns.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
CSCE 121: Introduction to Program Design and Concepts, Honors J. Michael Moore Spring 2015 Set 14: Plotting Functions and Data 1.
TeachScheme, ReachJava Adelphi University Friday morning July 16, 2010.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
CS321 Functional Programming 2 © JAS CS321 Functional Programming 2 Dr John A Sharp 10 credits Tuesday 10amRobert Recorde Room Thursday 11amRobert.
Parallel Programming Basics  Things we need to consider:  Control  Synchronization  Communication  Parallel programming languages offer different.
CS COMPUTER GRAPHICS LABORATORY. LIST OF EXPERIMENTS 1.Implementation of Bresenhams Algorithm – Line, Circle, Ellipse. 2.Implementation of Line,
MA/CS 375 Fall 2003 Lecture 3. .* Multiplication We can use the.* operator to perform multiplication entry by entry of two matrices:
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
Resources & Bitmaps Adding clip art to your application.
Advanced Java Screen Update Techniques SD’98 - Session 4406 Ted Faison Faison Computing Inc.
Reference: What is it? A multimedia python library – Window Management – Graphics geometric shapes bitmaps (sprites) – Input Mouse Keyboard.
Haskell Chapter 7.
Pixels, Colors and Shapes
Matlab Introduction.
INTERACTIVE TRANSPARENCY BUILDING A Character IN ANIMATION
AFP - Lecture 2 Domain Specific Embedded Languages
Lecture 2 Domain Specific Embedded Languages
Digital Media Dr. Jim Rowan ITEC 2110.
CS 100: Roadmap to Computing
Topics Graphical User Interfaces Using the tkinter Module
ECE498AL Spring 2010 Lecture 4: CUDA Threads – Part 2
Reactive Animations Todays Topics
Class 6 using the math library
Terminal-Based Programs
CS297 Graphics with Java and OpenGL
PROGRAMMING IN HASKELL
Animation Translation.
Presentation transcript:

Simple Animations Today’s Topics Reading Assignment Simple animations Buffered graphics Animations in Haskell Complex animations Lifting primitives to animations Behaviors Type classes, animations, and Behaviors Time translation Reading Assignment Haskell School of Expression Read chapter 13 - A Module of Simple Animations

Animations An animation is a “moving” graphic. Sometimes we say a time dependent graphic, since where it “moves” to is dependent upon time. To create the illusion of “movement” we need draw frames with a different picture each frame. A frame rate of about 30 frames a second is optimal less than 15-20 appears to flicker greater than 30 gives no apparent improvement To draw a frame we need to erase the old frame before drawing the new frame. All our drawings have been accumulative (we never erase anything, just draw “over” what’s already there). There exist several strategies for frame drawing.

Buffered graphics Display devices display the information stored in the video memory. Buffered graphics use two sets of memory, instantaneously switching from one memory to the other, so quickly that the flicker effect is unobservable. HI H I T H E R THERE H I This video memory free for writing while the other is displayed

Haskell interface to buffered graphics timeGetTime timegetTime :: IO Word32 Returns the current time. This time has no real bearing on anything tangible. It is just a big number, and measures the time in milliseconds. The “difference” between successive calls accurately measures elapsed time. setGraphic setGraphic :: Window -> Graphic -> IO() Writes the graphic into the “free” video graphic buffer. At the next frame “tick” what’s in the “free” video buffer will be drawn, and the current buffer will become the free buffer. Usual tick rate = 30 times per second

Interface to the richer window interface. Old interface: openWindow :: String -> Point -> IO Window e.g. openWindow “title” (width,height) Richer interface: openWindowEx :: String -> Maybe Point Maybe Point -> (Graphic -> DrawFun) -> Maybe word32 -> IO Window openWindowEx “title” (Just(x,y)) -- upper left corner (Just(width,height)) drawFun (Just 30) -- refresh rate

Animations in Haskell type Animation a = Time -> a type Time = Float rubberBall :: Animation Shape rubberBall t = Ellipse (sin t) (cos t) animate :: String -> Animation Graphic -> IO () main1 :: IO () main1 = animate "Animated Shape" (withColor Blue . shapeToGraphic . rubberBall)

Example Shape pulses from this to this

The animate function animate :: String -> Animation Graphic -> IO () animate title anim = runGraphics ( do w <- openWindowEx title (Just (0,0)) (Just Win,yWin)) drawBufferedGraphic t0 <- timeGetTime let loop = do t <- timeGetTime let word32ToInt = fromInteger . toInteger let ft = intToFloat (word32ToInt(t-t0))/1000 setGraphic w (anim ft) spaceCloseEx w loop loop )

Complex Animations revolvingBallB :: Behavior Picture revolvingBallB = let ball = shape (ell 0.2 0.2) in reg red (translate (sin time, cos time) ball) planets :: Animation Picture planets t = let p1 = Region Red (Shape (rubberBall t)) p2 = Region Yellow (revolvingBall t) in p1 `Over` p2 tellTime :: Animation String tellTime t = "The time is: " ++ show t

Telling Time main2 = animate "Animated Text” tellTime (return . text (100,200)) The time changes as time advances

Revolving Circle regionToGraphic :: Region -> Graphic regionToGraphic = drawRegion . regionToGRegion main3 :: IO () main3 = animate "Animated Region" (withColor Yellow . regionToGraphic . revolvingBall) Ball rotates

Animating Pictures main4 :: IO () main4 = animate "Animated Picture" Case analysis over structure of region. Use the primitives `overGraphic` & emptyGraphic picToGraphic :: Picture -> Graphic picToGraphic (Region c r) = withColor c (regionToGraphic r) picToGraphic (p1 `Over` p2) = picToGraphic p1 `overGraphic` picToGraphic p2 picToGraphic (Text v str) = (text (trans v) str) picToGraphic EmptyPic = emptyGraphic main4 :: IO () main4 = animate "Animated Picture" (picToGraphic . planets)

Lifting primitives to animations Its useful to define “time varying” primitives, like Picture type Animation a = Time -> a type Anim = Animation Picture type Time = Float First an Anim which doesn’t really vary emptyA :: Anim emptyA t = EmptyPic Combining time varying pictures overA :: Anim -> Anim -> Anim overA a1 a2 t = a1 t `Over` a2 t overManyA :: [Anim] -> Anim overManyA = foldr overA emptyA Recall Anim = Animation Picture = Time -> Picture hence the time parameter t

Time Translation timeTransA :: (Time -> Time) -> Animation a -> Animation a or timeTransA :: Animation Time -> timeTransA f a t = a (f t) timeTransA f a = a . f timeTransA (2*) anim -- runs twice as fast timeTransA (5+) anim -- runs 5 seconds behind

Ball rotates around screen Example revolvingBallB :: Behavior Picture revolvingBallB = let ball = shape (ell 0.2 0.2) in reg red (translate (sin time, cos time) ball) main5 :: IO () main5 = animateB "Revolving Ball Behavior" revolvingBallB Ball rotates around screen

Type Classes and Animations “Polymorphism captures similar structure over different values, while type classes capture similar operations over different structure.” Capture the similar operations on different things which vary over time with a Haskell Class. First define a new type: newtype Behavior a = Beh (Time -> a) newtype like data in Haskell doesn’t require the overhead that ordinary data definitions require since there is only 1 constructor function.

Lifting ordinary functions to Behavior’s 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)) lift3 :: (a -> b -> c -> d) -> (Behavior a -> Behavior b -> Behavior c -> Behavior d) lift3 g (Beh a) (Beh b) (Beh c) = Beh (\t -> g (a t) (b t) (c t))

Making Behavior Instances instance Eq (Behavior a) where a1 == a2 = error "Can't compare animations." instance Show (Behavior a) where showsPrec n a1 = error "Can't coerce animation to String." The instances for Eq and Show are bogus, but are necessary in order to define the Num class which requires Eq and Show instance Num a => Num (Behavior a) where (+) = lift2 (+); (*) = lift2 (*) negate = lift1 negate; abs = lift1 abs signum = lift1 signum fromInteger = lift0 . fromInteger

More Instances instance Fractional a => Fractional (Behavior a) where (/) = lift2 (/) fromRational = lift0 . fromRational 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 asin = lift1 asin; acos = lift1 acos atan = lift1 atan sinh = lift1 sinh; cosh = lift1 cosh tanh = lift1 tanh asinh = lift1 asinh; acosh = lift1 acosh atanh = lift1 atanh

Time A New Class time :: Behavior Time time = Beh (\t -> t) class Ani a where empty :: a over :: a -> a -> a

Instances for Our types instance Ani [a] where empty = [] over = (++) data Fun a = Fun (a->a) instance Ani (Fun a) where empty = Fun id Fun a `over` Fun b = Fun (a . b) instance Ani Picture where empty = EmptyPic over = Over instance Ani a => Ani (Behavior a) where empty = lift0 empty over = lift2 over What type is “empty” here?

Things that can turn class Turnable a where turn :: Float -> a -> a instance Turnable Picture where turn theta (Region c r) = Region c (turn theta r) -- turn on Regions turn theta (p1 `Over` p2) = turn theta p1 `Over` turn theta p2 turn theta EmptyPic = EmptyPic instance Turnable a => Turnable (Behavior a) where turn theta (Beh b) = Beh(turn theta . b)

Turning Shapes type Coordinate = (Float,Float) rotate :: Float -> Coordinate -> Coordinate rotate theta (x,y) = (x*c + y*s, y*c - x*s) where (s,c) = (sin theta,cos theta) instance Turnable Shape where turn theta (Polygon ps) = Polygon (map (rotate theta) ps) -- lots of missing cases here for -- turn theta (Rectangle s1 s2) = -- turn theta (Ellipse r1 r2) = -- turn theta (RtTriangle s1 s2) =

Turning Regions instance Turnable Region where turn theta (Shape sh) = Shape (turn theta sh) -- lots of missing cases here for -- turn theta (Translate (u,v) r) = -- turn theta (Scale (u,v) r) = -- turn theta (Complement r) = -- turn theta (r1 `Union` r2) = -- turn theta (r1 `Intersect` r2) = -- turn theta Empty = Empty A final example. See the text pages 209-212 main7 in today’s Haskell code.