import SOEGraphics
import Shape
import Draw 

-- #1
-- use :s +I to make the return result visible when
-- run from Hugs command line.

same :: IO Bool
same = do s1 <- getLine
          s2 <- getLine
          let b = s1 == s2
          putStrLn (if b then "yes" else "no")
          return b

-- #2

average :: IO ()
average =
   do countLine <- getLine
      let count = read countLine
      valueLines <- getNLines count
      let values = [read vl | vl <- valueLines ]
      let answer = (fromInt (sum values)) / (fromInt count)
      putStrLn ("Average = " ++ (show answer))

 where
  getNLines :: Int -> IO [String]
  getNLines 0 = return []
  getNLines n = do line <- getLine
                   lines <- getNLines (n-1)
                   return (line:lines)

  -- a cuter alternative (which could be inlined into average)
  getNLines' :: Int -> IO [String]
  getNLines' n = sequence (replicate n getLine)

-- #3

while :: IO Bool -> IO () -> IO ()
while test oper = do res <- test
                     if res 
                       then do oper
                               while test oper
                       else return () 


a = while same
         (putStrLn " - not different yet")

-- Note that the following doesn't do what you might hope, because
-- the two bindings of c are completely independent.

echoLine =
 do c <- getChar
    putChar c
    while (return (c /= '\n'))  
          (do c <- getChar
              putChar c)

-- #4

me :: IO ()
me = runGraphics (
     do w <- openWindow "me" (1000,1000)
        sequence_ [drawInWindow w (withColor c g) | (c,g) <- cgs ]
        spaceClose w
     )
     where 
     cgs = [(Red, polygon [(50,900),(150,900),(550,100),(450,100)]),
	    (Green, shearEllipse (800,900) (900,900) (500,100)),
	    (Blue, polygon [(300,450),(300,550),(700,550),(700,450)]),
	    (Cyan, polygon [(315,465),(315,535),(685,535),(685,465)])]


-- #5

rtrans :: Point -> Vertex
rtrans (x,y) = (pixelToInch (x - xWin2),
	        pixelToInch (yWin2 - y))
 where xWin2 = xWin `div` 2
       yWin2 = yWin `div` 2

showArea :: IO ()
showArea = 
  runGraphics (
  do w <- openWindow "showArea" (xWin,yWin)
     p1 <- getLBP w
     p2 <- getLBP w
     drawInWindow w (withColor Blue (line p1 p2))
     p3 <- getLBP w
     drawInWindow w (withColor Blue (line p2 p3))
     p4 <- getLBP w
     drawInWindow w (withColor Blue (line p3 p4))
     drawInWindow w (withColor Blue (line p4 p1))
     let side = sqrt (area (Polygon [rtrans p1, rtrans p2, rtrans p3, rtrans p4]))
     drawInWindow w (withColor Red (shapeToGraphic (square side)))
     spaceClose w     
  )  


