--
-- 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
--
-- Other constants used during the generation of the image
--
split_low = 120 :: Int
split_penalty = 1.5 :: Float
fill_prob = 0.25 :: Float
--
-- 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:rest: 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 _ _ 0 _ rvals = (rvals, "")
mondrian _ _ _ 0 rvals = (rvals, "")
mondrian x y w h (r:s:rest)
| w > width `div` 2 &&
h > height `div` 2 = b_split x y w h (r:s:rest)
| w > width `div` 2 = h_split x y w h (r:s:rest)
| h > height `div` 2 = v_split x y w h (r:s:rest)
| hs && vs = b_split x y w h rest
| hs = h_split x y w h rest
| vs = v_split x y w h rest
| otherwise = (s:rest, "