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

Slides:



Advertisements
Similar presentations
Chapter 16 Graphical User Interfaces
Advertisements

Getting Input in Python Assumes assignment statement, typecasts.
Lecture 2 Concepts, Terms and Definitions. Display Devices They are divided into a lot of small squares called pixels (“PICture ELements”). Each pixel.
Polynomial Functions of Higher Degree
1 Functions and Applications
Graphics Shapes. Setup for using graphics You have to import the graphics library You can use either “import graphics” or “from graphics import *” or.
Chapter 16 Graphical User Interfaces John Keyser’s Modifications of Slides by Bjarne Stroustrup
Graphics Programming. Introduction GOAL: Build the Indexer Client Event-driven vs. Sequential programs Terminology – Top-level windows are called “frame.
Variables and Operators
Chapter 9 Color, Sound and Graphics
Graphics Programming. In this class we will cover: Drawing lines, rectangles, and ellipses Copying an area Drawing an image.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
Chapter 8 Counting Principles: Further Probability Topics Section 8.5 Probability Distributions; Expected Value.
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.
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.
Computer Systems Week 8: 3-bit – The Display Amanda Oddie.
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.
Sketchify Tutorial Graphics and Animation in Sketchify sketchify.sf.net Željko Obrenović
Advanced Programming Handout 10 A Module of Simple Animations (SOE Chapter 13)
Cse536 Functional Programming 1 6/30/2015 Lecture #16, Nov. 29, 2004 Todays Topics – Files, Channels, and Handles –IO exception handling –First Class Channels.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
PictureBox, Timer, Resources. Resources An easy and effective way to add pictures (photos, graphics) to your programs Using Resources guarantees that.
Informationsteknologi Tuesday, November 6, 2007Computer Graphics - Class 41 Today’s class Input and interaction.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
OpenGL Brian Farrimond Robina Hetherington. What is OpenGL A specification of a set of functions for drawing graphics –Names of functions –What information.
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.
G RAPHICS P ROGRAMMING Lecture 3 - Simple Animation - Simple 3D Drawing.
CIS 205—Web Design & Development Flash Chapter 1 Getting Started with Adobe Flash CS3.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
XTIMELINE Use as a database for timelines Several formats to view stories Text & pictures can be added.
Raster-scan system In addition to the central processing unit a special purpose processor called the video controller or display controller is used to.
Chapter 2 Getting Started: Drawing Figures. The Framebuffer Lecture 2 Fri, Aug 29, 2003.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
Objectives Differentiate between raster scan display and random scan display.
Lecture 5: Interaction 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 711,  ex 2271 
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
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.
File Management Organizing files and folders. In this tutorial you will learn how to:  Create folders  Name files and folders  Organize your files.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
School of Computer Science & Information Technology G6DICP - Lecture 17 GUI (Graphical User Interface) Programming.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera Real Light Synthetic Light Source.
Program 2 due 02/01  Be sure to document your program  program level doc  your name  what the program does  each function  describe the arguments.
1 A first OpenGL program Brian Farrimond Robina Hetherington.
Part I. Drawing an Image using Lines (Airplane, Missiles, Stars) Part II. Animating Objects in the image using timers Part II. Animating objects using.
1 Graphics CSCI 343, Fall 2015 Lecture 6 Viewing, Animation, User Interface.
Lecture 26 Virtual Machine Monitors. Virtual Machines Goal: run an guest OS over an host OS Who has done this? Why might it be useful? Examples: Vmware,
CRE Programming Club - Class 5 Robert Eckstein and Robert Heard.
V 1.0 Programming III. Graphical possibilities Simple graphics (shapes)
GAM666 – Introduction To Game Programming ● DirectDraw, the 2D component of DirectX, uses the term “surface” to mean an area of memory in which pixel data.
Lecture 5: 11/5/1435 Computer Animation Lecturer/ Kawther Abas CS- 375 Graphics and Human Computer Interaction.
Fall 2000M.B. Ibáñez Lecture 26 I/O Systems II. Fall 2000M.B. Ibáñez Application I/O Interface I/O system calls encapsulate device behaviors in generic.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
CS 115 Lecture 7 Graphics – coordinate systems, Text, Entry, aliases Taken from notes by Dr. Neil Moore.
Computer Graphics CC416 Lecture 02: Overview of Graphics Systems: Raster & Random Displays – Chapter 2 Dr. Manal Helal – Fall 2014.
CS 115 Lecture 6 Graphics Taken from notes by Dr. Neil Moore.
CPT 450 Computer Graphics 12th Lecture – Animation.
Computer Graphics Lecture 3 Computer Graphics Hardware
Introduction to Computing
An Introduction to Spritesheet Animation
INTERACTIVE TRANSPARENCY BUILDING A Character IN ANIMATION
Computer Programming I
CS 115 Lecture Graphics II – coordinate systems, Text, Entry, aliases
CS 115 Lecture Graphics II – coordinate systems, Text, Entry, aliases
Topics Graphical User Interfaces Using the tkinter Module
Simple Animations Today’s Topics Reading Assignment Simple animations
Translate 5 squares left and 4 squares up.
Presentation transcript:

Cse536 Functional Programming 1 1/13/2015 Lecture #13, Nov. 08, 2004 Today’s Topics –Simple animations –Buffered graphics –Animations in Haskell –Complex animations –Lifting primitives to animations –Behaviors –Type classes, animations, and Behaviors –Time translation Reading Assignment –Read chapter 13 - A Module of Simple Animations

Cse536 Functional Programming 2 1/13/2015 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 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.

Cse536 Functional Programming 3 1/13/2015 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 HI HITHERE THERE This video memory free for writing while the other is displayed

Cse536 Functional Programming 4 1/13/2015 Haskell interface to buffered graphics getWindowTick –getWindowTick :: Window -> IO() –Every window has an internal timer. getWindowTick “waits” for the next “tick” (since the last call to getWindowTick ) before it returns. If the next “tick” has already occurred it returns immediately. 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

Cse536 Functional Programming 5 1/13/2015 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 Interface to the richer window interface.

Cse536 Functional Programming 6 1/13/2015 Animations in Haskell type Animation a = Time -> a type Time = Float rubberBall :: Animation Shape rubberBall t = Ellipse (sin t) (cos t) animate :: String -> Animation a -> (a -> IO Graphic) -> IO () main1 = animate "Animation of a Shape" rubberBall (return. withColor Blue. shapeToGraphic)

Cse536 Functional Programming 7 1/13/2015 Example Shape pulses from this to this

Cse536 Functional Programming 8 1/13/2015 The animate function animate :: String -> Animation a -> (a -> IO Graphic) -> IO () animate title anim toGraphic = 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 gr <- toGraphic (anim ft) setGraphic w gr getWindowTick w loop loop)

Cse536 Functional Programming 9 1/13/2015 Complex Animations revolvingBall :: Animation Region revolvingBall t = let ball = Shape (Ellipse ) in Translate (sin t, cos t) 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

Cse536 Functional Programming 10 1/13/2015 Telling Time main2 = animate "Animated Text” tellTime (return. text (100,200)) The time changes as time advances

Cse536 Functional Programming 11 1/13/2015 Revolving Circle regionToGraphic :: Region -> Graphic regionToGraphic = drawRegion. regionToGRegion main3 = animate "Animated Region" revolvingBall (\r -> return (withColor Yellow (regionToGraphic r))) Ball rotates

Cse536 Functional Programming 12 1/13/2015 Animating Pictures 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 = animate "Animated Picture" planets (return.picToGraphic) Case analysis over structure of region. Use the primitives `overGraphic` & emptyGraphic

Cse536 Functional Programming 13 1/13/2015 Lifting primitives to animations Its useful to define “time varying” primitives, like Picture type Anim = Animation Picture 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

Cse536 Functional Programming 14 1/13/2015 Time Translation timeTransA :: (Time -> Time) -> Animation a -> Animation a or timeTransA :: Animation Time -> Animation a -> Animation a timeTransA f a t = a (f t) or timeTransA f a = a. f timeTransA (2*) anim -- runs twice as fast timeTransA (5+) anim -- runs 5 seconds behind

Cse536 Functional Programming 15 1/13/2015 Example rBall :: Anim rBall t = let ball = Shape (Ellipse ) in Region Red (Translate (sin t, cos t) ball) rBalls :: Anim rBalls = overManyA [ timeTransA ((t*pi/4)+) rBall | t <- [0..7]] main5 = animate "Lots of Balls" rBalls (return. picToGraphic) Each ball rotates pi/4 seconds behind the one in front of it

Cse536 Functional Programming 16 1/13/2015 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.

Cse536 Functional Programming 17 1/13/2015 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))

Cse536 Functional Programming 18 1/13/2015 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

Cse536 Functional Programming 19 1/13/2015 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

Cse536 Functional Programming 20 1/13/2015 Time time :: Behavior Time time = Beh (\t -> t) A New Class class Ani a where empty :: a over :: a -> a -> a

Cse536 Functional Programming 21 1/13/2015 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?

Cse536 Functional Programming 22 1/13/2015 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)

Cse536 Functional Programming 23 1/13/2015 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) =

Cse536 Functional Programming 24 1/13/2015 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 main7 in today’s Haskell code.