--Informatics Functional Programming  Competition 2011

--by Chimwemwe Kadzuwa
--s1104012
   

------------------------
{--SIERPINSKI MADNESS--}
------------------------


--Typing renderST creates ST.svg file viewable in a browser
renderST = writeFile "ST.svg" (begintag++entireTri++endtag)

--Beginning and end of SVG code
begintag = "<svg  xmlns=\"http://www.w3.org/2000/svg\"" ++"\n"++"xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 700 700\">"++"\n"
endtag = "\n"++"</svg>"	



----------------------
--TRIANGLE
----------------------
--entireTri:tri associated with background and animations
--background:black background
--tri:simple information about triangle
--postioning:important information for reproducing and positioning triangle
--colourTransfrom:animating colour changes
--movementTransfrom:animating movement


entireTri = background++tri++positioning++colourTransform
background = "<rect width=\"1000\" height=\"1000\" style=\"fill:black\" transform=\"translate(-80,0)\"/>"
tri = unlines ["\n","<g id=\"tri\" transform=\"scale(1.2) translate(80,80)\">",movementTranform,baseTri,"</g>","\n"]
positioning = unlines ["<use xlink:href=\"#tri\" x=\"0\" y=\"0\"></use>","<use xlink:href=\"#tri\" x=\"383\" y=\"0\"></use>",
					  "<use xlink:href=\"#tri\" x=\"191\" y=\"-326\"></use>"]

colourTransform = "<animate attributeName=\"fill\" values=\"yellow;yellow;blue;blue;red;red;yellow\" dur=\"10.5s\" repeatCount=\"indefinite\"/>"
movementTranform =unlines["<animateTransform attributeName=\"transform\" attributeType=\"XML\" type=\"scale\" from=\"0\" to=\"1\"" ++
						" begin=\"0s\" dur=\"4\" fill=\"freeze\" additive=\"sum\" repeatCount=\"indefinite\"/>",
						"<animateTransform attributeName=\"transform\" attributeType=\"XML\" type=\"scale\" from=\"4\" to=\"0\"" 
						++" begin=\"4\" dur=\"4\" fill=\"freeze\"  additive=\"sum\" repeatCount=\"indefinite\"/>"] 


------------------------
--TRIANGLE CONSTRUCTION
------------------------
--baseTri:Outcome of reusing triangle and incrementally building larger triangles
--base:Section where 3 basic triangles that are reused are explicitly defined 
--build:Function that reuses triangles to build larger ones
		--grouping:function for group enclosure i.e wrapping group tags round a group of triangles
		--reuse:function for reusing already define groups triangles
		--reuse':function for reusing already define groups triangles
		
baseTri = (build 1 4 1 base)
base = unlines["<g id=\"base0\">","<polygon points=\"0,500 10,482.679 20,500\"/>",
	   "<polygon points=\"10,482.679 20,465.358 30,482.679\"/>","<polygon points=\"20,500 30,482.679 40,500\"/>"]

build :: Int -> Int -> Int -> String -> String
build n n1 n2 string = if n == n1 then string++"</g>" else 
					   let x =  unlines[grouping n string,reuse n n2,reuse' n n2] in (build (n+1) n1 (n2*2) x) 

grouping :: Int -> String -> String
reuse,reuse' :: Int -> Int -> String

grouping n str = "\n"++"<g id=\"base"++show n++"\">"++str++"</g>"
reuse n n2 = "<use xlink:href=\"#base"++ show (n-1)++"\""++" x=\""++show (2*side*n2)++"\" y=\"0\"></use>"
reuse' n n2  = "<use xlink:href=\"#base"++show (n-1)++"\""++" x=\""++(show (side*n2))++"\" y=\""++(show (height*2*n2))++"\"></use>" 

------------------------
--TRIANGLE CONSTANTS
------------------------
--side:length of side for smallest equilateral triangles
--height: height(approx) for smallest equilateral triangles

side = 20
height = -17 