-- CS-191 Functional Programming I - Coursework 2
-- Authors: Haskell B Curry (000001) and Ulrich Berger (000002)
-- Submission date: 15 March 2010
{-
You may use this file by copying it to your working directory 
and importing it by writing

import CW2_123

at the beginning of your file.

Alternatively, you may do Questions 1,2,3 on your own 
as a voluntary exercise. 
-}

module CW2_123   where

type Point   = (Float,Float)
type Bitmap  = Point -> Bool
type Picture = [[Bool]]
type Setting = (Point,Point,Int,Int) 

black, white :: Char
black = '@'
white = '.'

set0 :: Setting
set0 = ((-1,-1),(1,1),10,8)

discBm :: Bitmap
discBm (x,y) = x^2 + y^2 <= 1

discPic :: Picture
discPic = 
      [[False,False,False,False,False,True, False,False,False,False,False],
       [False,False,True, True, True, True, True, True, True, False,False],
       [False,True, True, True, True, True, True, True, True, True, False],
       [False,True, True, True, True, True, True, True, True, True, False],
       [True, True, True, True, True, True, True, True, True, True, True],
       [False,True, True, True, True, True, True, True, True, True, False],
       [False,True, True, True, True, True, True, True, True, True, False],
       [False,False,True, True, True, True, True, True, True, False,False],
       [False,False,False,False,False,True, False,False,False,False,False]]


-- Question 1

takePicture :: Setting -> Bitmap -> Picture
takePicture ((xbl,ybl),(xtr,ytr),n,m) bm = [ row j | j <- [0..m] ]

  where

    row :: Int -> [Bool]
    row j = [ bm (point i j) | i <- [0..n] ]

    point :: Int -> Int -> Point
    point i j = (xbl+(fromIntegral i)*d,ytr-(fromIntegral j)*e)

    d = (xtr-xbl)/(fromIntegral n)
    e = (ytr-ybl)/(fromIntegral m)
    
{- Test:   
*Main> takePicture set0 discBm == discPic
True
-}


-- Question 2

showPicture :: Picture -> String
showPicture pic = concat ['\n' : map bool2Char bs | bs <- pic]

 where 

   bool2Char :: Bool -> Char
   bool2Char True  = black
   bool2Char False = white

   
-- Question 3

pp :: Picture -> IO ()   -- print picture
pp = putStrLn . showPicture

pb :: Setting -> Bitmap -> IO ()
pb set bm = pp (takePicture set bm) 

{- Test:
*CW2_123> pp discPic

.....@.....
..@@@@@@@..
.@@@@@@@@@.
.@@@@@@@@@.
@@@@@@@@@@@
.@@@@@@@@@.
.@@@@@@@@@.
..@@@@@@@..
.....@.....
*CW2_123> pb set0 discBm

.....@.....
..@@@@@@@..
.@@@@@@@@@.
.@@@@@@@@@.
@@@@@@@@@@@
.@@@@@@@@@.
.@@@@@@@@@.
..@@@@@@@..
.....@.....

Please do NOT include such tests in your submission.
-}
