-- -- Starting code for CPSC 449 Assignment 1 -- -- Generate and output a Mondrian-style image as an SVG tag within an HTML -- document. -- import System.IO import Control.Monad (replicateM) import System.Random (randomRIO, StdGen, randomR, mkStdGen) -- -- The width and height of the image being generated. -- width :: Int width = 1024 height :: Int height = 768 -- -- Generate and return a list of 20000 random floating point numbers between -- 0 and 1. (Increase the 20000 if you ever run out of random values). -- randomList :: Int -> [Float] randomList seed = take 20000 (rl_helper (mkStdGen seed)) rl_helper :: StdGen -> [Float] rl_helper g = fst vg : rl_helper (snd vg) where vg = randomR (0.0, 1.0 :: Float) g -- -- Compute an integer between low and high from a (presumably random) floating -- point number between 0 and 1. -- randomInt :: Int -> Int -> Float -> Int randomInt low high x = round ((fromIntegral (high - low) * x) + fromIntegral low) -- -- Generate the tag for a rectangle with random color. Replace the -- implementation of this function so that it generates all of the tags -- needed for a piece of random Mondrian art. -- -- Parameters: -- x, y: The upper left corner of the region -- w, h: The width and height of the region -- r:s:t:rs: A list of random floating point values between 0 and 1 -- -- Returns: -- [Float]: The remaining, unused random values -- String: The SVG tags that draw the image -- mondrian :: Int -> Int -> Int -> Int -> [Float] -> ([Float], String) mondrian x y w h (r:s:t:rs) = (rs, "\n") -- -- The main program which generates and outputs mondrian.html. -- main :: IO () main = do -- Right now, the program will generate a different sequence of random -- numbers each time it is run. If you want the same sequence each time -- use "let seed = 0" instead of "seed <- randomRIO (0, 100000 :: Int)" --let seed = 0 seed <- randomRIO (0, 100000 :: Int) let randomValues = randomList seed let prefix = "\n" ++ "" image = snd (mondrian 0 0 width height randomValues) suffix = "\n" writeFile "mondrian.html" (prefix ++ image ++ suffix)