packages feed

SVG2Q-0.3: Point.hs

module Point where

import Helpers (replace)
import Debug.Trace (trace)

data Point = Point {
     x :: Float
     , y :: Float
     }
     deriving (Show, Eq)

-- reads a string and returns a list of points. 
-- If the string contains non-parsable
-- characters, read will throw an "no parse"-exception. 
-- if the count of numbers in the string
-- is odd, we will break down with an exception showing the supplied string.
getPoints :: Maybe String -> [Point]
getPoints Nothing = trace "Warning: getPoints got Nothing. \
                            \Returning an empty list." []
getPoints (Just s) = 
  let 
    sl = words $ replace s "," " "
    readPoints [] pl = pl
    readPoints xs pl = 
        readPoints (drop 2 xs) (pl ++ [Point (read (head xs)) (read (xs !! 1))])
  in
    if odd (length sl)
       then error ("odd count of numbers, how am i supposed to\
                   \ make points? got string: "++s)
       else readPoints sl []


-- reflects a point relative to another point 
reflect :: Point -> Point -> Point
reflect (Point x y) (Point cx cy) = 
        let dx = cx -x
            dy = cy -y
        in
            (Point (cx+dx) (cy+dy))