haskell代写: comp1100-assignment2

1. Rectangles (rectangleRaster) [10 marks]

Rasterising a rectangle is relatively straightforward. You must generate a list of pixels with coordinates that lie on the boundary of the rectangle, and having grey shade 1.0 (black). We offer the following hints to achieve this: using list comprehensions you can enumerate the pixels over the range of the xx and yy coordinates of the sides of the rectangle. Be careful for rectangles that have width and/or height of only one pixel, making sure that all pixels on the resulting raster have unique coordinates. This property applies to all shapes: no two pixels in a shape raster can have the same coordinates.

The following Haskell code entered into GHCi produces the resulting picture (remember to use cabal repl to load your assignment code into GHCi):

drawingOf $ updateView (Model [Rectangle (-2,1) (2,-1)] 1 False Nothing)

Adjust the pixel resolution to see a finer-grained raster:

drawingOf $ updateView (Model [Rectangle (-2,1) (2,-1)] 0.1 False Nothing)

2. Lines (lineRaster) [25 marks]

Bresenham’s line algorithm (see references 1) is lauded as a seminal work that shaped the field of computer graphics. It is an efficient algorithm for drawing lines on a raster display. There are many descriptions of Bresenham’s algorithm which you will find by searching online, including pseudocode for the algorithm as well as versions implemented in multiple programming languages (remember to cite all of your sources in your report!). Your task is to implement Bresenham’s algorithm for your shape drawing application.

The following Haskell code entered into GHCi produces the resulting picture:

drawingOf $ updateView (Model [Line (-10,0) (10,5)] 1 False Nothing)

3. Polygons (polyLineRaster) [10 marks]

Once you have your line drawing algorithm working then you can easily use it to draw polygons. A closed polygon, defined by a sequence of vertices, is simply the line segments formed between each successive pair of vertices in the sequence, plus a line segment between the first and last vertices in the sequence.

Here is an example:

drawingOf $ updateView (Model [Polygon [(-3,0),(0,2),(3,0),(0,-2)]] 0.1

4. Circles (circleRaster) [25 marks]

Bresenham’s algorithm for lines is easily extended for circles (see references 2). Again, there are many sources of information online that can be found easily.

drawingOf $ updateView (Model [Circle (0,0) (9,0)] 1 False Nothing)