FUNKTSIONAALNE PROGRAMMEERIMINE. Animatsioonid

Similar documents
Reaktiivsed pildid. Piltide definitsioon:

Simple Animations. Today s Topics. Reading Assignment

What is reactive programming. Basic Concepts of Reactivity. Behaviours. Functional Reactive Programming

Introduction to Programming and 4Algorithms Abstract Types. Uwe R. Zimmer - The Australian National University

Introduction to MATLAB

A. Matrix-wise and element-wise operations

1 The number systems in Haskell 98

Matlab Workshop I. Niloufer Mackey and Lixin Shen

LAB 1 General MATLAB Information 1

Introduction to MATLAB

Basic types and definitions. Chapter 3 of Thompson

Type system. Type theory. Haskell type system. EDAN40: Functional Programming Types and Type Classes (revisited)

EDAF40. 2nd June :00-19:00. WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read.

FUNCTIONAL PROGRAMMING NO.9 TYPE AND CLASS. Tatsuya Hagino

Python. Olmo Zavala R. Python Exercises. Center of Atmospheric Sciences, UNAM. August 24, 2016

Built-in Types of Data

Script started on Thu 25 Aug :00:40 PM CDT

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

Informatics 1 Functional Programming Lectures 13 and 14 Monday 11 and Tuesday 12 November Type Classes. Don Sannella University of Edinburgh

Introduction to GNU-Octave

Abstract Types, Algebraic Types, and Type Classes

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

Introduction to Programming: Lecture 10

Introduction to Matlab

Introduction to Matlab. Matlab variable types. Matlab variable types. Matlab variable types. Notes. Eugeniy E. Mikhailov. Lecture 02. Notes.

S III. Case Study: TI Calculator Numerics

Higher Order Functions in Haskell

Functional Programming TDA 452, DIT 142

CIS 194: Homework 4. Due Wednesday, February 18, What is a Number?

Milleks tüübid? Mida teeb järgmine programmijupp? x 1 := "Pii siinus on : "; x 2 := ; printx 2 ; print(sin(x 1 ));

Standard prelude. Appendix A. A.1 Classes

ArcGIS Enterprise Building Raster Analytics Workflows. Mike Muller, Jie Zhang

User-Defined Algebraic Data Types

Data Parallel Execution Model

Computational Physics

Introduction to the Image Analyst Extension. Mike Muller, Vinay Viswambharan

General MATLAB Information 1

ffl text :: location. ffl getkey :: Point - String - Graphic creates a Graphic consisting of a String at a given screen Window - IO Char waits for the

Computer Science 121. Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans

Functional Programming TDA 452, DIT 142

Lecture Notes on Haskell Programming

Package cgraph. March 11, 2019

Starting MATLAB To logon onto a Temple workstation at the Tech Center, follow the directions below.

MATLAB Workshop Dr. M. T. Mustafa Department of Mathematical Sciences. Introductory remarks

Type Classes in Haskell Tom Schrijvers. Leuven Haskell User Group

Computing and Statistical Data Analysis Lecture 3

Arithmetic and Logic Blocks

Tabelid <TABLE> Koostanud: Merike Hein

The Official Guide to Easy Graphing. ParaGraph. Ver Congratulations Graphing just got easier.

Haskell An Introduction

Programming for Engineers in Python. Recitation 2

Single row numeric functions

How to Design Programs Languages

Advanced features of Functional Programming (Haskell)

What s New in Imagery in ArcGIS. Presented by: Christopher Patterson Date: September 12, 2017

A general introduction to Functional Programming using Haskell

Preparing for FRP. Shapes, Regions, and Drawing

CS 320: Concepts of Programming Languages

A tour of the Haskell Prelude

Automating Distributed Raster Analysis using the Image Server REST API. Jie Zhang Zikang Zhou Demo Theater 2 - Oasis 1

A General Introduction to Matlab

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

High Level Scripting. Gino Tosti University & INFN Perugia. 06/09/2010 SciNeGhe Data Analysis Tutorial

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

Chap 6 Function Define a function, which can reuse a piece of code, just with a few different values.

Distributed Image Analysis Using the ArcGIS API for Python

GRAPH 4.4. Megha K. Raman APRIL 22, 2015

Package Brobdingnag. R topics documented: March 19, 2018

Haskell Overview II (2A) Young Won Lim 8/23/16

Funcons. reusable components of language specifications. Peter D. Mosses. Swansea University (emeritus) TU Delft (visitor)

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CSC324 Principles of Programming Languages

An Introduction to GeoGebra

Chapter 1 MATLAB Preliminaries

Haskell Overview III (3A) Young Won Lim 10/4/16

Computing Fundamentals

Introduction to Scientific and Engineering Computing, BIL108E. Karaman

Taipei Embedded Outreach OpenCL DSP Profile Proposals

An introduction to R WS 2013/2014

Mentor Graphics Predefined Packages

Version default Titre : Opérateur FORMULE Date : 07/04/2018 Page : 1/5 Responsable : COURTOIS Mathieu Clé : U Révision : 0b486e897e73

Raster Analysis and Image Processing in ArcGIS Enterprise

# Import constants pi2, pip2, pip4 (2*pi, pi/2, pi/4). use Math::Trig ':pi';

Highly Optimized Mathematical Functions for the Itanium Processor

Ebooks Chemical Engineering

Functional Programming Mid-term exam Tuesday 3/10/2017

Notes on Complex Numbers for Computer Scientists

Haskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes. Haskell Overloading (3) Haskell Overloading (2)

Programming for Engineers in Python. Recitation 3 Functions

(IUCAA, Pune) kaustubh[at]iucaa[dot]ernet[dot]in.

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Scientific Computing Lecture 1: MATLAB fundamentals

CSc 372. Comparative Programming Languages. 18 : Haskell Type Classes. Department of Computer Science University of Arizona

Objectives. You will learn how to process data in ABAP

Haskell Making Our Own Types and Typeclasses

PyCUDA. Continued...

CSE 3302 Programming Languages Lecture 8: Functional Programming

Overloading, Type Classes, and Algebraic Datatypes

CSE399: Advanced Programming. Handout 2

CSE 591: GPU Programming. Programmer Interface. Klaus Mueller. Computer Science Department Stony Brook University

Transcription:

Animatsioon on liikuv, ehk ajast sõltuv, graafika Liikumise illusioon saavutatakse sellega, et teatud ajavahemike järel (optimaalseks peetakse 30 freimi sekundis) joonistatakse erineva pildiga uus freim Freimide joonistamiseks kasutatakse puhverdatud graafikat, kus on paralleelselt kasutusel kaks videomälu puhvrit selajal kui ühes puhvris olevat pilti kuvatakse ekraanile, toimub teise puhvrisse uue pildi joonistamine; ning kui on aeg uus freim ekraanile kuvada, siis vahetatakse puhvrite rollid ära VARMO VENE 1

Puhverdatud graafika interfeis Haskellis: openwindowex :: Title -> Maybe Point -> Maybe Size -> RedrawMode -> Maybe Time -> IO Window drawgraphic :: RedrawMode drawbufferedgraphic :: RedrawMode getwindowtick :: Window -> IO () timegettime :: IO Word32 setgraphic :: Window -> Graphic -> IO () VARMO VENE 2

Haskellis: 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) VARMO VENE 3

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 VARMO VENE 4

Keerulisemaid animatsioone: revolvingball :: Animation Region revolvingball t = let ball = Shape (Ellipse 0.2 0.2) 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 VARMO VENE 5

Keerulisemaid animatsioone (järg): main2 :: IO () main2 = animate "Animated Text" (text (100,200). telltime) regiontographic :: Region -> Graphic regiontographic = drawregion. regiontogregion main3 :: IO () main3 = animate "Animated Region" (withcolor Yellow. regiontographic. revolvingball) VARMO VENE 6

Piltide animeerimine: pictographic :: Picture -> Graphic pictographic (Region c r) = withcolor c (regiontographic r) pictographic (p1 Over p2) = pictographic p1 overgraphic pictographic p2 pictographic EmptyPic = emptygraphic main4 :: IO () main4 = animate "Animated Picture" (pictographic. planets) VARMO VENE 7

Animeerivate piltide konstrueerimine: emptya :: Animation Picture emptya t = EmptyPic overa :: Animation Picture -> Animation Picture overa a1 a2 t = a1 t Over a2 t -> Animation Picture overmanya :: [Animation Picture] -> Animation Picture overmanya = foldr overa emptya Operatsioonid animatsioonidel on analoogsed baastüübi operatsioonidega VARMO VENE 8

Operatsioonide sarnasuse baastüüpidel ja animatsioonidel saame abstraheerida tüübiklasside abil Tehnilistel põhjustel ei luba Haskell tüübisünonüümi Animate tüübklassi esindajaks deklareerida; seetõttu defineerime uue tüübi, mida lubab Käitumised : newtype Behavior a = Beh (Time -> a) animateb :: String -> Behavior Picture -> IO () animateb s (Beh pf) = animate s (pictographic. pf) VARMO VENE 9

Tavaliste funktsioonide kergitamine käitumistele: 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)) VARMO VENE 10

Tavaliste funktsioonide kergitamine käitumistele (järg): lift3 :: (a -> b -> c -> d) -> (Behavior a -> Behavior b -> Behavior c lift3 g (Beh a) (Beh b) (Beh c) = Beh (\t -> g (a t) (b t) (c t)) -> Behavior d) VARMO VENE 11

Käitumiste isendidefinitsioonid: instance Eq (Behavior a) where a1 == a2 = error "Can t compare behaviors." instance Show (Behavior a) where showsprec n a1 = error "<< Behavior >>" instance Num a => Num (Behavior a) where (+) = lift2 (+); (*) = lift2 (*) negate = lift1 negate abs = lift1 abs signum = lift1 signum frominteger = lift0. frominteger VARMO VENE 12

Käitumiste isendidefinitsioonid (järg): 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 VARMO VENE 13

Nüüd saame kasutada arvulisi käitumisi!! Näide: time :: Behavior Time time = Beh (\t -> t) time + 5 ==> (lift2 (+)) (Beh (\t -> t)) (Beh (\t -> 5)) ==> (\ (Beh a) (Beh b) -> Beh (\t -> a t + b t)) (Beh (\t -> t)) (Beh (\t -> 5)) ==> Beh (\t -> (\t -> t) t + (\t -> 5) t ) ==> Beh (\t -> t + 5) VARMO VENE 14

Käitumiste konstrueerimine: class Combine a where empty :: a over :: a -> a -> a instance Combine [a] where empty = [] over = (++) newtype Fun a = Fun (a->a) instance Combine (Fun a) where empty = Fun id Fun a over Fun b = Fun (a. b) VARMO VENE 15

Käitumiste konstrueerimine näide: m :: Behavior Picture m = let a = lift0 (empty over p) in a over empty p :: Picture p = empty NB! Siin on tüübideklaratsioonid kohustuslikud, kuna vastasel korral ei ole võimalik ülelaadimist lahendada VARMO VENE 16

Käitumiste konstrueerimine: reg shape ell red = lift2 Region = lift1 Shape = lift2 Ellipse = lift0 Red yellow = lift0 Yellow translate (Beh a1, Beh a2) (Beh r) = Beh (\t -> Translate (a1 t, a2 t) (r t)) Siin pole vaja tüüpe deklareerida, kuna tüübid on üheselt määratud VARMO VENE 17

Käitumiste konstrueerimine: 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 VARMO VENE 18

Käitumiste konstrueerimine: (>*) :: Ord a => Behavior a -> Behavior a -> Behavior Bool (>*) = lift2 (>) 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 flash :: Behavior Color flash = cond (sin time >* 0) red yellow VARMO VENE 19

Aja teisendused: timetrans :: Behavior Time -> Behavior a -> Behavior a timetrans (Beh f) (Beh a) = Beh (a. f) timetrans (2*time) anim timetrans (5+time) anim over anim VARMO VENE 20

Aja teisendused: flashingball :: Behavior Picture flashingball = let ball = shape (ell 0.2 0.2) in reg (timetrans (8*time) flash) (translate (sin time, cos time) ball) main6 :: IO () main6 = animateb "Flashing Ball" flashingball VARMO VENE 21

Aja teisendused: revolvingballs :: Behavior Picture revolvingballs = overmany [ timetrans (lift0 (t*pi/4) + time) flashingball t <- [0..7]] main7 :: IO () main7 = animateb "Lots of Flashing Balls" revolvingballs VARMO VENE 22

Objektide pööramine: class Turnable a where turn :: Float -> a -> a instance Turnable Picture where turn theta (Region c r) = Region c (turn theta r) 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) VARMO VENE 23

Objektide pööramine: 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) -- ülejäänud variandid puudu instance Turnable Region where turn theta (Shape sh) = Shape (turn theta sh) -- ülejäänud variandid puudu VARMO VENE 24