SHARE
TWEET

Untitled

a guest Nov 21st, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// Types taken from assignment
  2. type point = int * int // a point ( x , y ) in the plane
  3. type colour = int * int * int // ( red , green , blue ), 0..255
  4. type figure =
  5.   | Circle of point * int * colour // defined by center, radius, and colour
  6.   | Rectangle of point * point * colour // defined by corners bottom - left , top - right , and colour
  7.   | Mix of figure * figure // combine figures with mixed colour at overlap
  8.  
  9. /// 8i0: Create a figure of a circle with a center at (50,50) and radius 45, and a box with the corners (40,40) and (90,110)
  10. let figTest =
  11.   let circleCenter = point (50, 50)
  12.   let circleColour = colour (255, 0, 0)
  13.   let redcircle = Circle (circleCenter, 45, circleColour)
  14.   let boxLowerCorner = point (40,40)
  15.   let boxUpperCorner = point (90, 110)
  16.   let boxColour = colour (0, 0, 255)
  17.   let bluebox = Rectangle (boxLowerCorner, boxUpperCorner, boxColour)
  18.   Mix (redcircle, bluebox)
  19.  
  20. /// TESTS
  21. /// TEST 1: Is figTest a Mix?
  22. match figTest with
  23.  | Circle _ -> printfn "8i0-1 Failed"
  24.  | Rectangle _ -> printfn "8i0-1 Failed"
  25.  | Mix _ -> printfn "8i0-1 Success"
  26.  
  27. /// TEST 2: Does figTest contain a circle and a box?
  28. match figTest with
  29. | Mix (f1, f2) ->
  30.   match f1 with
  31.     | Circle (center, r, colour) -> if center = (50,50) && r = 45 && colour = (255,0,0) then printfn "8i0-2-1 Success" else printfn "8i0-2-1 Failed"
  32.     | _ -> printfn "8i0-2-1 Failed"
  33.   match f2 with
  34.     | Rectangle (p1, p2, colour) -> if p1 = (40,40) && p2 = (90, 110) && colour = (0,0,255) then printfn "8i0-2-1 Success" else printfn "8i0-2-1 Failed"
  35.     | _ -> printfn "8i0-2-2 Failed"
  36. | _ -> printfn "8i0-2 Failed"
  37.  
  38. /// colourAt from assignment
  39. let rec colourAt (x,y) figure =
  40.   match figure with
  41.   | Circle ((cx,cy), r, col) ->
  42.       if (x-cx) * (x-cx) + (y-cy)*(y-cy) <= r*r
  43.       then Some col else None
  44.   | Rectangle ((x0,y0), (x1, y1), col) ->
  45.       if x0 <= x && x <= x1 && y0 <= y && y <= y1
  46.       then Some col else None
  47.   | Mix (f1, f2) ->
  48.       match (colourAt (x,y) f1, colourAt (x,y) f2) with
  49.       | (None, c) -> c
  50.       | (c, None) -> c
  51.       | (Some (r1,g1,b1), Some (r2,g2,b2)) ->
  52.           Some ((r1+r2)/2 , (g1+g2)/2 , (b1+b2)/2)
  53.  
  54. /// 8i1: makePicture function
  55. /// <summary>Turns a figure into a PNG file</summary>
  56. /// <param name="filename">The filename as a string</param>
  57. /// <param name="fig">The figure to be drawn</param>
  58. /// <param name="b">The width of the output file</param>
  59. /// <param name="h">The height of the output file</param>
  60. /// <returns>Nothing</returns>
  61. let makePicture (filename : string) (fig : figure) (b : int) (h : int) : unit =
  62.   let bmp = ImgUtil.mk h b
  63.   for x in [0..b-1] do
  64.     for y in [0..h-1] do
  65.       let colour =
  66.         match (colourAt (x, y) fig) with
  67.         | Some z -> z
  68.         | _ -> colour (128, 128, 128)
  69.       ImgUtil.setPixel (ImgUtil.fromRgb colour) (x, y) bmp
  70.   ImgUtil.toPngFile filename bmp
  71.  
  72. /// 8i2: Use makePicture
  73. makePicture "test.png" figTest 100 150
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top