COMP3141: Software System Design and Implementation

Exercise (Week 2)

COMP3141: Software System Design and Implementation

Term 2, 2023

Help Sessions
Moodle – Lecture Recordings
Setting up Haskell
Practice Problems
Code and Notes

Code and Notes

Assigned Reading
Practice Problems
Code and Notes

Code and Notes

Exercise (Week 2)

Table of Contents

1. Getting Started
2. Part 1: Simple Picture (3 Marks)
3. Part 2: Moving Objects (3 Marks)
4. Part 3: Generating a Picture (3 Marks)
5. Submission Instructions

DUE: Thu June 15 10:59:59

1 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 find it helpful to have a look at the first six chapters of
Learning Haskell, as the ShapeGraphics library you’ll be using is quite similar to the graphics library presented there.
Or, you know, just dig in.

To get started, follow the instructions below.

Download the exercise zipfile and extract it to a directory in your home directory at CSE.
This zip file contains a file, 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:

newclass starting new subshell for class COMP3141…
$ cabal repl
Resolving dependencies…
Build profile: -w ghc-8.8.4 -O1
In order, the following will be built (use -v for more details):
– Ex01-1.0 (exe:Test) (first run)
Configuring executable ‘Test’ for Ex01-1.0..
Warning: Packages using ‘cabal-version: >= 1.10’ must specify the
‘default-language’ field for each component (e.g. Haskell98 or Haskell2010).
If a component uses different languages in different modules then list the
other ones in the ‘other-languages’ field.
Preprocessing executable ‘Test’ for Ex01-1.0..
GHCi, version 8.8.4: https://www.haskell.org/ghc/ 😕 for help
[1 of 3] Compiling ShapeGraphics ( ShapeGraphics.hs, interpreted )
[2 of 3] Compiling Ex01 ( Ex01.hs, interpreted )
[3 of 3] Compiling Main ( Test.hs, interpreted )
Ok, three modules loaded.
*ShapeGraphics> :l Ex01

: warning: [-Wmissing-home-modules]
These modules are needed for compilation but not listed in your .cabal file’s other-modules:
ShapeGraphics
[1 of 2] Compiling ShapeGraphics ( ShapeGraphics.hs, interpreted )
[2 of 2] Compiling Ex01 ( Ex01.hs, interpreted )
Ok, two modules loaded.
*Ex01> writeToFile housePic

(Ignore the -Wmissing-home-modules warning if you get one.)

Calling writeToFile as demonstrated above will write the provided picture to a file called ex01.png in the directory in which you invoked GHCi.
Note that you will only need to submit Ex01.hs, so only make changes to that file.

Download the exercise zipfile and extract it to a directory on your local machine.
This zip file contains a file, 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 writeToFile as demonstrated above will write the provided picture (in this case housePic) to a file called ex01.png in the directory in which you invoked GHCi.
Note that you will only need to submit Ex01.hs, so only make changes to that file.

As a first step, read through the comments in Ex01.hs, and look at the definition of the
various data types used there.

You can safely ignore the definition of the function drawPicture.
All you need to know is that it takes as first parameter a floating point value which sets
the line width of the picture, and as second parameter a Picture (a list of
PictureObject values), and returns a .png image of the picture.
The function writeToFile further saves the resulting image to disk as ex01.png.

2 Part 1: Simple Picture (3 Marks)

Your first task is to complete the definition of four functions, housePic, merge, window and chimneyHouse.

Complete the definition of picture housePic in Ex01.hs such that it is a list of
two picture objects: house and door. The house object is the outline of a house, given as a Path.
The house should be coloured green, and have a solid line style. The Path making up the outline
consists of multiple points: the coordinates of these points are given in the lists houseCOx and houseCOy.
Since the ShapeGraphics library expects the coordinates in a different format, you will have to define
a conversion function merge :: [Float] -> [Float] -> [Point] which converts the two lists of coordinates
into the expected type.

The door object should be a path defined by the x,y coordinate pairs given in doorCOs, coloured red, and
with solid line style.

Use writeToFile housePic (as demonstrated with the example session above) to test your definition.

As a next step, define a new picture chimneyHouse, which additionally has a chimney and a window, so
that it looks as follows:

The additional four coordinates needed for the chimney are provided in chimneyCOs. You will have to
combine these with the house outline.

The window should be a rectangle with the coordinates given in
windowCOs, with Solid border, filled with the cyan colour defined
in Ex01.hs.

Important: A complete solution of Part 1 must have working definitions of
housePic, merge, window and chimneyHouse. Make sure to use exactly the given coordinates;
otherwise, automarking will not give you any marks.

3 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 definition of the function:

movePictureObject :: Vector -> PictureObject -> PictureObject

which moves a picture object. You need to provide a rule for every possible PictureObject
(the pattern matching for Path is already part of the given definition).

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]

ought to produce the following image in ex01.png:

Hint: Path and Polygon are the more complicated cases. Don’t forget that you can use Hoogle! Haskell might have pre-defined function that help with your task.

4 Part 3: Generating a Picture (3 Marks)

Write a function simpleCirclePic :: Colour -> Float -> Picture,
such that simpleCirclePic col n for a positive floating point number n generates the
picture consisting of overlapping circles:

[Circle (Point 400 400) (1 * (400/n)) col Solid SolidFill,
Circle (Point 400 400) (2 * (400/n)) col Solid SolidFill,
Circle (Point 400 400) (3 * (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]

To test it, 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 myPurp = Colour 153 0 153 100
*ShapeGraphics> writeToFile $ simpleCirclePic myPurp 5

ought to produce the following image in ex01.png:

Hint: You can define this function using explicit recursion, but it is much easier to use the map function
we discussed in the lecture, and the function enumFromThenTo. The expression enumFromThenTo start next upperBound
generates the list [start, next, start + 2 * (next – start),..] up to upperBound.

5 Submission Instructions

While connected/using a CSE machine, type the following to submit your work from the directory within the Ex01 folder:

$ give cs3141 ex01 Ex01.hs

Alternatively, you can use the give web interface.

Your submission should work on ghc version on the CSE lab system,
which is ghc 8.8.4. Using a different ghc version on your home
machine is fine and unlikely to lead to problems. When you submit, a
subset of the marking test suite will run on the ghc version
mentioned above. The first time, this may take a while as dependencies
are installed. If you see some of them failing, you may want to go
back to the drawing board and submit again. give allows you to
resubmit as many times as you want before the deadline; only the last
submission counts.

Note that you will submit only the Haskell module file called
Ex01.hs (and not the entire project).

All work submitted for assessment must be entirely your own. Unacknowledged
copying of material, in whole or part, is a serious offence. Before submitting
any work you should read and understand the UNSW Plagiarism Policy.

2023-06-09 Fri 07:44
Announcements RSS

Back to top