Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Types taken from assignment
- type point = int * int // a point ( x , y ) in the plane
- type colour = int * int * int // ( red , green , blue ), 0..255
- type figure =
- | Circle of point * int * colour // defined by center, radius, and colour
- | Rectangle of point * point * colour // defined by corners bottom - left , top - right , and colour
- | Mix of figure * figure // combine figures with mixed colour at overlap
- /// 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)
- let figTest =
- let circleCenter = point (50, 50)
- let circleColour = colour (255, 0, 0)
- let redcircle = Circle (circleCenter, 45, circleColour)
- let boxLowerCorner = point (40,40)
- let boxUpperCorner = point (90, 110)
- let boxColour = colour (0, 0, 255)
- let bluebox = Rectangle (boxLowerCorner, boxUpperCorner, boxColour)
- Mix (redcircle, bluebox)
- /// TESTS
- /// TEST 1: Is figTest a Mix?
- match figTest with
- | Circle _ -> printfn "8i0-1 Failed"
- | Rectangle _ -> printfn "8i0-1 Failed"
- | Mix _ -> printfn "8i0-1 Success"
- /// TEST 2: Does figTest contain a circle and a box?
- match figTest with
- | Mix (f1, f2) ->
- match f1 with
- | 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"
- | _ -> printfn "8i0-2-1 Failed"
- match f2 with
- | 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"
- | _ -> printfn "8i0-2-2 Failed"
- | _ -> printfn "8i0-2 Failed"
- /// colourAt from assignment
- let rec colourAt (x,y) figure =
- match figure with
- | Circle ((cx,cy), r, col) ->
- if (x-cx) * (x-cx) + (y-cy)*(y-cy) <= r*r
- then Some col else None
- | Rectangle ((x0,y0), (x1, y1), col) ->
- if x0 <= x && x <= x1 && y0 <= y && y <= y1
- then Some col else None
- | Mix (f1, f2) ->
- match (colourAt (x,y) f1, colourAt (x,y) f2) with
- | (None, c) -> c
- | (c, None) -> c
- | (Some (r1,g1,b1), Some (r2,g2,b2)) ->
- Some ((r1+r2)/2 , (g1+g2)/2 , (b1+b2)/2)
- /// 8i1: makePicture function
- /// <summary>Turns a figure into a PNG file</summary>
- /// <param name="filename">The filename as a string</param>
- /// <param name="fig">The figure to be drawn</param>
- /// <param name="b">The width of the output file</param>
- /// <param name="h">The height of the output file</param>
- /// <returns>Nothing</returns>
- let makePicture (filename : string) (fig : figure) (b : int) (h : int) : unit =
- let bmp = ImgUtil.mk h b
- for x in [0..b-1] do
- for y in [0..h-1] do
- let colour =
- match (colourAt (x, y) fig) with
- | Some z -> z
- | _ -> colour (128, 128, 128)
- ImgUtil.setPixel (ImgUtil.fromRgb colour) (x, y) bmp
- ImgUtil.toPngFile filename bmp
- /// 8i2: Use makePicture
- makePicture "test.png" figTest 100 150
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement