12/08/2020 Exercise (Week 2)
Exercise (Week 2)
DUE: Wed June 17 15:00:00 Getting Started
Before you begin, make sure that you installed Haskell according to the Haskell Setup instructions. For this and next week’s exercises, you might nd it helpful to have a look at the rst six chapters of Learning Haskell, as the graphics library used is similar.
To get started, please follow the instructions corresponding to your particular Haskell setup.
CSE
Stack
Download the exercise tarball and extract it to a directory on your local machine. This tarball contains a le, called Ex01.hs , wherein you will do all of your programming.
To test your code, run the following shell commands to open a GHCi session:
$ stack repl
Configuring GHCi with the following packages: Ex01
Using main module: 1. Package ‘Ex01’ component exe:Ex01 …
GHCi, version 8.2.2: http://www.haskell.org/ghc/ 😕 for help
[1 of 2] Compiling ShapeGraphics (ShapeGraphics.hs, interpreted)
[2 of 2] Compiling Ex01 (Ex01.hs, interpreted)
Ok, two modules loaded.
*Ex01 ShapeGraphics> writeToFile housePic
Calling as demonstrated above will write the provided picture (in this case ) to a le called ex01.png in the directory in which you invoked GHCi.
writeToFile
housePic
www.cse.unsw.edu.au/~cs3141/20T2/Week 02/exercise.html 1/7
12/08/2020 Exercise (Week 2)
Note that you will only need to submit , so only make changes to that le.
Ex01.hs
As a rst step, look at the de nition of the various data types in Ex01.hs . You can safely ignore the de nition of the function drawPicture . All you need to know is that it takes as rst parameter a oating point value which sets the line width of the picture, and as second parameter a Picture , or a list of values, and returns a .png image of the resulting picture. The function further saves the resulting image to disk.
Part 1: Simple Picture (3 Marks)
Complete the de nition of picture housePic in Ex01.hs such that it is a list of two picture objects: house , a path de ned by the co-ordinates in houseCOs , coloured
green , and with solid line style, and door , a path de ned by the door coordinates, coloured red , and with solid line style. Use drawPicture or writeToFile (as demonstrated above) to check your de nition.
As a next step, de ne a new picture chimneyHouse , which has an a chimney and smoke , so that it looks as follows:
PictureObject
writeToFile
www.cse.unsw.edu.au/~cs3141/20T2/Week 02/exercise.html 2/7
12/08/2020 Exercise (Week 2)
The additional four coordinates needed for the chimney are the following: (615, 325) , (615, 250) , (650, 250) and (650, 363) .
For the smoke, use the four coordinates (635, 240) , , (635, 220) , (625, 210) and the colour grey already pre-de ned in .
Important: Make sure to use exactly the given coordinates; otherwise, automarking will not give you any marks.
Part 2: Moving Objects (3 Marks)
You are given a function which moves a point along a given vector and returns the new point. Use this function to complete the de nition of the function:
(625, 230)
Ex01.hs
www.cse.unsw.edu.au/~cs3141/20T2/Week 02/exercise.html 3/7
12/08/2020 Exercise (Week 2)
movePictureObject :: Vector -> PictureObject -> PictureObject
which moves a picture object. You need to provide a rule for every possible picture object (the pattern matching for path is already part of the given de nition).
The following GHCi Session:
GHCi, version 8.2.2: http://www.haskell.org/ghc/ 😕 for help
[1 of 2] Compiling ShapeGraphics (ShapeGraphics.hs, interpreted)
[2 of 2] Compiling Ex01 (Ex01.hs, interpreted)
Ok, two modules loaded.
*ShapeGraphics> let myRed = red { opacityC = 180 }
*ShapeGraphics> let xy = (Point 400 400)
*ShapeGraphics> let circ = Circle xy 100 myRed Solid SolidFill
*ShapeGraphics> let v = (Vector 100 100)
*ShapeGraphics> writeToFile [circ, movePictureObject v circ]
*ShapeGraphics> :q
www.cse.unsw.edu.au/~cs3141/20T2/Week 02/exercise.html 4/7
12/08/2020 Exercise (Week 2)
Ought to produce the following image in ex01.png :
map :: (a
-> b) -> [a] -> [b]
www.cse.unsw.edu.au/~cs3141/20T2/Week 02/exercise.html 5/7
simpleCirclePic :: Colour -> Float -> Picture
simpleCirclePic col n
n
Hint: Path and polygon are the more complicated cases – circle is pretty straight forward. For both path and polygon, you may nd the pre-de ned function
useful (as discussed in the lecture).
Part 3: Generating a Picture (3 Marks)
Write a function , such that
for a positive oating point number generates the picture
consisting of overlapping circles:
12/08/2020 Exercise (Week 2)
[Circle (Point 400 400) (1
Circle (Point 400 400) (2
Circle (Point 400 400) (3
….
* (400/n)) col Solid SolidFill,
* (400/n)) col Solid SolidFill,
* (400/n)) col Solid SolidFill,
Circle (Point 400 400) ((n-1) * (400/n)) col Solid SolidFill,
Circle (Point 400 400) (n * (400/n)) col Solid SolidFill]
enumFromThenTo start next upperBound
Colour 153 0 153 100
enumFromThenTo
map
[start, next,
start + 2 * (next – start),..]
upperBound
www.cse.unsw.edu.au/~cs3141/20T2/Week 02/exercise.html 6/7
Ex01.hs
To test it, use a colour with low opacity value, for example .
Hint: You can de ne this function using explicit recursion, but it is much easier to use the function we discussed in the lecture, and the function . The
expression
generates the list .
Submission Instructions
Submit only the Haskell module le called
(not the entire project).
up to
12/08/2020 Exercise (Week 2)
You can submit your exercise by typing:
$ give cs3141 Ex01 Ex01.hs
on a CSE terminal, or by using the give web interface. Your le must be named Ex01.hs (case-sensitive!).
A dry-run test will partially autotest your solution at submission time. To get full marks, you will need to perform further testing yourself.
www.cse.unsw.edu.au/~cs3141/20T2/Week 02/exercise.html
7/7