Diagrams

Introduction

We’ll use Haskell to draw diagrams. The Diagram type can be displayed directly in the web page.

Coordinate System

Circle

circle :: Double -> Diagram
Test>

Rectangle

rect :: Double -> Double -> Diagram
Test>
Test>

Putting two diagrams together

The <|> operator combines two diagrams into one diagram.

(<|>) :: Diagram -> Diagram -> Diagram
Test>

Notice that the square is on top of the circle. How can we put the circle on top of the square?

Task: “Do not enter” sign

How can we draw the following figure?

Test>

(Hint: The length measurements are 2, 5, and 8.)

Moving

The move function shifts a diagram in space.

move :: Diagram -> Point -> Diagram
Test>
Test>

Combining multiple diagrams

Test>

Instead of using <|>, we can use union to combine diagrams. union takes a list of diagrams.

union :: [Diagram] -> Diagram
Test>

Using list comprehensions

We can use list comprehensions to express diagrams.

Test>

Task: stack of coins

How can we draw this stack of coins using list comprehensions?

Test>

(Hint: The circles’ diameter is 4.)

Task: stairs

Test>

(Hint: We use -5, 1, 2, 5.)

Task: Circles

Test>
Test>
Test>

Other interesting diagrams

Test>
Test>
Test>
Test>

Colors

fill :: Diagram -> Color -> Diagram
yellow :: Color
Test>

Pre-defined colors: black, gray, white, red, green, blue, yellow

Task: “Do not enter” sign with color

How can we draw the following figure?

Test>

Rotation

rotate :: Diagram -> Double -> Diagram
Test>

Task: black star

Test>

(Hint: the size is 10.)

Task: clock face

Test>

Line segment

(>-<) :: Point -> Point -> Diagram
Test>

Task: dandelion

Test>

Other basic building blocks

polygon :: [Point] -> Diagram
Test>
scale :: Diagram -> Double -> Diagram
Test>
textAt :: String -> Point -> Diagram
textFrom :: String -> Point -> Diagram
Test>
Hi there
strokeWidth :: Diagram -> Double -> Diagram
stroke :: Diagram -> Color -> Diagram
Test>
clip :: Point -> Point -> Diagram -> Diagram
rgb :: Double -> Double -> Double -> Color
Test>
Test>
Test>