Diagrams: Declarative Vector Graphics in Haskell Brent Yorgey NY - - PowerPoint PPT Presentation

diagrams declarative vector graphics in haskell
SMART_READER_LITE
LIVE PREVIEW

Diagrams: Declarative Vector Graphics in Haskell Brent Yorgey NY - - PowerPoint PPT Presentation

Diagrams: Declarative Vector Graphics in Haskell Brent Yorgey NY Haskell Users Group November 25, 2013 Part I: Demo! Part II: Lessons for EDSL design Take home Domain analysis is hard! Take home Domain analysis is hard! Be in it for


slide-1
SLIDE 1

Diagrams: Declarative Vector Graphics in Haskell

Brent Yorgey NY Haskell Users’ Group November 25, 2013

slide-2
SLIDE 2

Part I: Demo!

slide-3
SLIDE 3

Part II: Lessons for EDSL design

slide-4
SLIDE 4

Take home

Domain analysis is hard!

slide-5
SLIDE 5

Take home

Domain analysis is hard! Be in it for the long haul.

slide-6
SLIDE 6

History

slide-7
SLIDE 7

April 2008. Wanted: declarative, programmatic drawing.

slide-8
SLIDE 8

PGF/TikZ

slide-9
SLIDE 9

“How hard could it be?”

slide-10
SLIDE 10

After two weeks of feverish hacking, diagrams was born!

slide-11
SLIDE 11

After two weeks of feverish hacking, diagrams was born! It sucked.

slide-12
SLIDE 12
slide-13
SLIDE 13
slide-14
SLIDE 14

Paths

slide-15
SLIDE 15

What is a path?

slide-16
SLIDE 16

type Path = [Point ]

slide-17
SLIDE 17

Problem 1

?

slide-18
SLIDE 18
slide-19
SLIDE 19

Problem 2

type Path = [(P2, CurveSpec)] ?

slide-20
SLIDE 20

Affine spaces

slide-21
SLIDE 21

Find the bug

type Point = (Double, Double) type Vector = (Double, Double) instance (Num a, Num b) ⇒ Num (a, b) where . . . parallelogram :: Point → Point → Point → Point parallelogram p1 p2 p3 = p1 − p3 − p2

slide-22
SLIDE 22

Affine spaces for programmers

Confusing points and vectors is a type error!

slide-23
SLIDE 23

Affine spaces

slide-24
SLIDE 24

translate (p1 − p2) ≡ translate p1 − translate p2

slide-25
SLIDE 25

translate (p1 − p2) ≡ translate p1 − translate p2 Translations apply to points but not to vectors!

slide-26
SLIDE 26

(ˆ + ˆ) :: Vector → Vector → Vector (. + ˆ) :: Point → Vector → Point (. − .) :: Point → Point → Vector

slide-27
SLIDE 27

. . . Paths Again

slide-28
SLIDE 28

type Path = [Vector ]

slide-29
SLIDE 29

type Path = [Segment ]

slide-30
SLIDE 30
slide-31
SLIDE 31

type Path = ([Segment ], Bool) ?

slide-32
SLIDE 32
slide-33
SLIDE 33

( , True)?

slide-34
SLIDE 34
slide-35
SLIDE 35

( , True)?

slide-36
SLIDE 36

Our solution

data Offset c v where OffsetOpen :: Offset Open v OffsetClosed :: v → Offset Closed v data Segment c v = Linear (Offset c v) | Cubic v v (Offset c v)

slide-37
SLIDE 37

data Trail′ l v where Line :: [Segment Closed v ] → Trail′ Line v Loop :: [Segment Closed v ] → Segment Open v → Trail′ Loop v glueLine :: Trail′ Line v → Trail′ Loop v closeLine :: Trail′ Line v → Trail′ Loop v cutLine :: Trail′ Loop v → Trail′ Line v

slide-38
SLIDE 38

Problem 3

slide-39
SLIDE 39

Problem 3

type Trail = [Segment ] . . . type Path = [(Point, Trail)]

slide-40
SLIDE 40

Our solution

data Located a = Loc {loc :: Point (V a), unLoc :: a} newtype Path v = Path [Located (Trail v)]